nushell/src/commands/ls.rs

114 lines
4.3 KiB
Rust
Raw Normal View History

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-06-08 00:35:07 +02:00
use crate::parser::lexer::Spanned;
use crate::prelude::*;
2019-06-13 23:47:25 +02:00
use std::ffi::OsStr;
2019-06-03 09:41:28 +02:00
use std::path::{Path, PathBuf};
2019-05-10 18:59:12 +02:00
pub fn ls(args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-06-13 23:47:25 +02:00
let env = args.env.lock().unwrap();
let path = env.last().unwrap().path.to_path_buf();
let obj = &env.last().unwrap().obj;
let mut full_path = PathBuf::from(path);
2019-06-03 09:41:28 +02:00
match &args.positional.get(0) {
2019-06-08 00:35:07 +02:00
Some(Spanned {
item: Value::Primitive(Primitive::String(s)),
..
}) => full_path.push(Path::new(s)),
2019-06-03 09:41:28 +02:00
_ => {}
}
2019-05-11 10:08:21 +02:00
2019-06-13 23:47:25 +02:00
match obj {
Value::Filesystem => {
let entries = std::fs::read_dir(&full_path);
2019-06-08 00:35:07 +02:00
2019-06-13 23:47:25 +02:00
let entries = match entries {
Err(e) => {
if let Some(s) = args.positional.get(0) {
return Err(ShellError::labeled_error(
e.to_string(),
e.to_string(),
s.span,
));
} else {
return Err(ShellError::string(e.to_string()));
}
}
Ok(o) => o,
};
2019-05-16 02:21:46 +02:00
2019-06-13 23:47:25 +02:00
let mut shell_entries = VecDeque::new();
2019-05-10 18:59:12 +02:00
2019-06-13 23:47:25 +02:00
for entry in entries {
let value = Value::Object(dir_entry_dict(&entry?)?);
shell_entries.push_back(ReturnValue::Value(value))
}
Ok(shell_entries.boxed())
}
_ => {
let mut entries = VecDeque::new();
let mut viewed = obj;
let sep_string = std::path::MAIN_SEPARATOR.to_string();
let sep = OsStr::new(&sep_string);
for p in full_path.iter() {
2019-06-14 03:59:13 +02:00
//let p = p.to_string_lossy();
2019-06-13 23:47:25 +02:00
match p {
x if x == sep => {}
2019-06-14 03:59:13 +02:00
step => {
let tmp = step.to_string_lossy();
let split_tmp = tmp.split('[');
let mut first = true;
for s in split_tmp {
if !first {
match s.find(']') {
Some(finish) => {
let idx = s[0..finish].parse::<usize>();
match idx {
Ok(idx) => match viewed.get_data_by_index(idx) {
Some(v) => {
viewed = v;
}
_ => println!("Idx not found"),
},
_ => println!("idx not a number"),
}
}
_ => println!("obj not some"),
/*
_ => match viewed.get_data_by_key(s) {
Some(v) => {
viewed = v;
}
_ => println!("Obj not Some"),
},
*/
}
} else {
match viewed.get_data_by_key(s) {
Some(v) => {
viewed = v;
}
_ => println!("Obj not Some"),
}
first = false;
}
2019-06-13 23:47:25 +02:00
}
2019-06-14 03:59:13 +02:00
}
2019-06-13 23:47:25 +02:00
}
}
match viewed {
Value::List(l) => {
for item in l {
entries.push_back(ReturnValue::Value(item.copy()));
}
}
x => {
entries.push_back(ReturnValue::Value(x.clone()));
}
}
Ok(entries.boxed())
}
2019-05-10 18:59:12 +02:00
}
}