2019-05-10 18:59:12 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-05-16 00:23:36 +02:00
|
|
|
use crate::object::{dir_entry_dict, Value};
|
2019-05-13 19:30:51 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
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
|
|
|
|
|
|
|
#[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,
|
2019-05-16 00:58:44 +02:00
|
|
|
_args: Vec<Value>,
|
|
|
|
_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 {
|
2019-05-16 00:58:44 +02:00
|
|
|
fn run(&mut self, _stream: VecDeque<Value>) -> Result<VecDeque<ReturnValue>, ShellError> {
|
2019-05-10 18:59:12 +02:00
|
|
|
let entries =
|
2019-05-16 00:58:44 +02:00
|
|
|
std::fs::read_dir(&self.cwd).map_err(|e| ShellError::string(format!("{:?}", e)))?;
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-05-13 19:30:51 +02:00
|
|
|
let mut shell_entries = VecDeque::new();
|
2019-05-10 18:59:12 +02:00
|
|
|
|
|
|
|
for entry in entries {
|
2019-05-15 23:44:06 +02:00
|
|
|
let value = Value::Object(dir_entry_dict(&entry?)?);
|
2019-05-13 19:30:51 +02:00
|
|
|
shell_entries.push_back(ReturnValue::Value(value))
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 19:30:51 +02:00
|
|
|
Ok(shell_entries)
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
|
|
|
}
|