2019-05-10 18:59:12 +02:00
|
|
|
use crate::errors::ShellError;
|
2019-06-03 09:41:28 +02:00
|
|
|
use crate::object::{dir_entry_dict, Primitive, Value};
|
2019-05-13 19:30:51 +02:00
|
|
|
use crate::prelude::*;
|
2019-06-03 09:41:28 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
pub fn ls(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let cwd = args.env.lock().unwrap().cwd().to_path_buf();
|
2019-06-03 09:41:28 +02:00
|
|
|
let mut full_path = PathBuf::from(cwd);
|
|
|
|
match &args.positional.get(0) {
|
|
|
|
Some(Value::Primitive(Primitive::String(s))) => full_path.push(Path::new(s)),
|
|
|
|
_ => {}
|
|
|
|
}
|
2019-05-11 10:08:21 +02:00
|
|
|
|
2019-06-03 09:41:28 +02:00
|
|
|
let entries =
|
|
|
|
std::fs::read_dir(&full_path).map_err(|e| ShellError::string(format!("{:?}", e)))?;
|
2019-05-16 02:21:46 +02:00
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
let mut shell_entries = VecDeque::new();
|
2019-05-10 18:59:12 +02:00
|
|
|
|
2019-05-22 09:12:03 +02:00
|
|
|
for entry in entries {
|
|
|
|
let value = Value::Object(dir_entry_dict(&entry?)?);
|
|
|
|
shell_entries.push_back(ReturnValue::Value(value))
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|
2019-05-22 09:12:03 +02:00
|
|
|
|
2019-05-23 09:23:06 +02:00
|
|
|
Ok(shell_entries.boxed())
|
2019-05-10 18:59:12 +02:00
|
|
|
}
|