nushell/src/commands/ls.rs

48 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};
2019-05-11 10:08:21 +02:00
use crate::{Command, CommandSuccess};
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<String>,
host: &dyn crate::Host,
2019-05-10 18:59:12 +02:00
env: &mut crate::Environment,
2019-05-11 10:08:21 +02:00
) -> Box<dyn Command> {
Box::new(Ls {
cwd: env.cwd().to_path_buf(),
})
}
}
#[derive(new)]
pub struct Ls {
cwd: PathBuf,
}
impl crate::Command for Ls {
fn run(&mut self) -> Result<CommandSuccess, ShellError> {
2019-05-10 18:59:12 +02:00
let entries =
2019-05-11 10:08:21 +02:00
std::fs::read_dir(&self.cwd).map_err((|e| ShellError::new(format!("{:?}", e))))?;
2019-05-10 18:59:12 +02:00
let mut shell_entries = vec![];
for entry in entries {
let value = Value::object(DirEntry::new(entry?)?);
shell_entries.push(value)
}
2019-05-11 10:08:21 +02:00
Ok(CommandSuccess {
value: Value::list(shell_entries),
action: vec![],
})
2019-05-10 18:59:12 +02:00
}
}