Merge pull request #335 from androbtech/wraps

rmwrapped cd - ls.
This commit is contained in:
Jonathan Turner 2019-08-23 20:11:17 +12:00 committed by GitHub
commit 35346d7710
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -73,20 +73,30 @@ impl Shell for FilesystemShell {
} }
fn ls(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> { fn ls(&self, args: EvaluatedWholeStreamCommandArgs) -> Result<OutputStream, ShellError> {
let cwd = self.path.clone(); let cwd = self.path();
let mut full_path = PathBuf::from(&self.path); let mut full_path = PathBuf::from(self.path());
match &args.nth(0) { match &args.nth(0) {
Some(Tagged { item: value, .. }) => full_path.push(Path::new(&value.as_string()?)), Some(Tagged { item: value, .. }) => full_path.push(Path::new(&value.as_string()?)),
_ => {} _ => {}
} }
let entries = glob::glob(&full_path.to_string_lossy());
if entries.is_err() { let entries: Vec<_> = match glob::glob(&full_path.to_string_lossy()) {
return Err(ShellError::string("Invalid pattern.")); Ok(files) => files.collect(),
} Err(_) => {
if let Some(source) = args.nth(0) {
return Err(ShellError::labeled_error(
"Invalid pattern",
"Invalid pattern",
source.span(),
));
} else {
return Err(ShellError::string("Invalid pattern."));
}
}
};
let mut shell_entries = VecDeque::new(); let mut shell_entries = VecDeque::new();
let entries: Vec<_> = entries.unwrap().collect();
// If this is a single entry, try to display the contents of the entry if it's a directory // If this is a single entry, try to display the contents of the entry if it's a directory
if entries.len() == 1 { if entries.len() == 1 {
@ -185,11 +195,11 @@ impl Shell for FilesystemShell {
match std::env::set_current_dir(&path) { match std::env::set_current_dir(&path) {
Ok(_) => {} Ok(_) => {}
Err(_) => { Err(_) => {
if args.len() > 0 { if let Some(directory) = args.nth(0) {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Can not change to directory", "Can not change to directory",
"directory not found", "directory not found",
args.nth(0).unwrap().span().clone(), directory.span(),
)); ));
} else { } else {
return Err(ShellError::string("Can not change to directory")); return Err(ShellError::string("Can not change to directory"));