nushell/src/commands/ls.rs

47 lines
1.1 KiB
Rust
Raw Normal View History

2019-05-10 18:59:12 +02:00
use crate::errors::ShellError;
use crate::object::process::Process;
use crate::object::{DirEntry, ShellObject, Value};
use crate::prelude::*;
2019-05-12 00:59:57 +02:00
use crate::Args;
use crate::Command;
2019-05-10 18:59:12 +02:00
use derive_new::new;
2019-05-11 10:08:21 +02:00
use std::path::PathBuf;
2019-05-10 18:59:12 +02:00
use sysinfo::SystemExt;
#[derive(new)]
2019-05-11 10:08:21 +02:00
pub struct LsBlueprint;
2019-05-10 18:59:12 +02:00
2019-05-11 10:08:21 +02:00
impl crate::CommandBlueprint for LsBlueprint {
fn create(
&self,
args: Vec<Value>,
2019-05-11 10:08:21 +02:00
host: &dyn crate::Host,
2019-05-10 18:59:12 +02:00
env: &mut crate::Environment,
2019-05-12 00:59:57 +02:00
) -> Result<Box<dyn Command>, ShellError> {
Ok(Box::new(Ls {
2019-05-11 10:08:21 +02:00
cwd: env.cwd().to_path_buf(),
2019-05-12 00:59:57 +02:00
}))
2019-05-11 10:08:21 +02:00
}
}
#[derive(new)]
pub struct Ls {
cwd: PathBuf,
}
impl crate::Command for Ls {
fn run(&mut self, stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
2019-05-10 18:59:12 +02:00
let entries =
std::fs::read_dir(&self.cwd).map_err((|e| ShellError::string(format!("{:?}", e))))?;
2019-05-10 18:59:12 +02:00
let mut shell_entries = VecDeque::new();
2019-05-10 18:59:12 +02:00
for entry in entries {
let value = Value::object(DirEntry::new(entry?)?);
shell_entries.push_back(ReturnValue::Value(value))
2019-05-10 18:59:12 +02:00
}
Ok(shell_entries)
2019-05-10 18:59:12 +02:00
}
}