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-13 19:30:51 +02:00
|
|
|
use crate::prelude::*;
|
2019-05-12 00:59:57 +02:00
|
|
|
use crate::Args;
|
2019-05-13 19:30:51 +02:00
|
|
|
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,
|
2019-05-13 19:30:51 +02:00
|
|
|
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 {
|
2019-05-13 19:30:51 +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-13 19:30:51 +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 {
|
|
|
|
let value = Value::object(DirEntry::new(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
|
|
|
}
|
|
|
|
}
|