From 7913ae76f8e20a3b957c64a565d0d43fcd2d44e7 Mon Sep 17 00:00:00 2001 From: Jonathan Rothberg Date: Sat, 7 Sep 2019 15:31:16 -0700 Subject: [PATCH 1/2] Expand pwd command Expand functionality of the pwd command to better handle the different types of shells (e.g. FilesystemShell, ValueShell, etc.). --- src/commands/command.rs | 2 +- src/commands/pwd.rs | 16 ++++------------ src/shell/filesystem_shell.rs | 22 ++++++++++++++++++++++ src/shell/help_shell.rs | 4 ++++ src/shell/shell.rs | 1 + src/shell/shell_manager.rs | 6 ++++++ src/shell/value_shell.rs | 12 ++++++++++++ 7 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/commands/command.rs b/src/commands/command.rs index 922f647fc..6be179895 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -1,7 +1,7 @@ use crate::context::{SourceMap, SpanSource}; +use crate::data::Value; use crate::errors::ShellError; use crate::evaluate::Scope; -use crate::data::Value; use crate::parser::hir; use crate::parser::{registry, ConfigDeserializer}; use crate::prelude::*; diff --git a/src/commands/pwd.rs b/src/commands/pwd.rs index 2fd88ce8b..aa56c5165 100644 --- a/src/commands/pwd.rs +++ b/src/commands/pwd.rs @@ -1,9 +1,9 @@ use crate::commands::WholeStreamCommand; +// use crate::data::{Dictionary, Value}; use crate::errors::ShellError; -use crate::data::{Dictionary, Value}; use crate::parser::registry::Signature; use crate::prelude::*; -use indexmap::IndexMap; +// use indexmap::IndexMap; pub struct PWD; @@ -30,15 +30,7 @@ impl WholeStreamCommand for PWD { } pub fn pwd(args: CommandArgs, registry: &CommandRegistry) -> Result { + let shell_manager = args.shell_manager.clone(); let args = args.evaluate_once(registry)?; - let span = args.call_info.name_span; - - let mut indexmap = IndexMap::new(); - indexmap.insert( - "name".to_string(), - Tagged::from_simple_spanned_item(Value::string(std::env::current_dir()?.to_string_lossy()), span), - ); - - let value = Tagged::from_simple_spanned_item(Value::Row(Dictionary::from(indexmap)), span); - Ok(OutputStream::one(value)) + shell_manager.pwd(args) } diff --git a/src/shell/filesystem_shell.rs b/src/shell/filesystem_shell.rs index 10ca86488..cd9d24350 100644 --- a/src/shell/filesystem_shell.rs +++ b/src/shell/filesystem_shell.rs @@ -946,6 +946,28 @@ impl Shell for FilesystemShell { self.path.clone() } + fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result { + let path = PathBuf::from(self.path()); + let p = match dunce::canonicalize(path.as_path()) { + Ok(p) => p, + Err(_) => { + return Err(ShellError::labeled_error( + "unable to show current directory", + "pwd command failed", + args.call_info.name_span, + )); + } + }; + + let mut stream = VecDeque::new(); + stream.push_back(ReturnSuccess::value( + Value::Primitive(Primitive::String(p.to_string_lossy().to_string())) + .simple_spanned(args.call_info.name_span), + )); + + Ok(stream.into()) + } + fn set_path(&mut self, path: String) { let pathbuf = PathBuf::from(&path); let path = match dunce::canonicalize(pathbuf.as_path()) { diff --git a/src/shell/help_shell.rs b/src/shell/help_shell.rs index aebdeff39..35c939d7d 100644 --- a/src/shell/help_shell.rs +++ b/src/shell/help_shell.rs @@ -117,6 +117,10 @@ impl Shell for HelpShell { self.path.clone() } + fn pwd(&self, _: EvaluatedWholeStreamCommandArgs) -> Result { + Ok(OutputStream::empty()) + } + fn set_path(&mut self, path: String) { let _ = std::env::set_current_dir(&path); self.path = path.clone(); diff --git a/src/shell/shell.rs b/src/shell/shell.rs index dc1f104b6..549aa79d2 100644 --- a/src/shell/shell.rs +++ b/src/shell/shell.rs @@ -20,6 +20,7 @@ pub trait Shell: std::fmt::Debug { fn mv(&self, args: MoveArgs, name: Span, path: &str) -> Result; fn rm(&self, args: RemoveArgs, name: Span, path: &str) -> Result; fn path(&self) -> String; + fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result; fn set_path(&mut self, path: String); fn complete( diff --git a/src/shell/shell_manager.rs b/src/shell/shell_manager.rs index 136d9b017..53984c950 100644 --- a/src/shell/shell_manager.rs +++ b/src/shell/shell_manager.rs @@ -62,6 +62,12 @@ impl ShellManager { self.shells.lock().unwrap()[self.current_shell].path() } + pub fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result { + let env = self.shells.lock().unwrap(); + + env[self.current_shell].pwd(args) + } + pub fn set_path(&mut self, path: String) { self.shells.lock().unwrap()[self.current_shell].set_path(path) } diff --git a/src/shell/value_shell.rs b/src/shell/value_shell.rs index 84c829462..e14d5603a 100644 --- a/src/shell/value_shell.rs +++ b/src/shell/value_shell.rs @@ -199,6 +199,18 @@ impl Shell for ValueShell { self.path.clone() } + fn pwd(&self, args: EvaluatedWholeStreamCommandArgs) -> Result { + let path = PathBuf::from(&self.path()); + + let mut stream = VecDeque::new(); + stream.push_back(ReturnSuccess::value( + Value::Primitive(Primitive::String(path.to_string_lossy().to_string())) + .simple_spanned(args.call_info.name_span), + )); + + Ok(stream.into()) + } + fn set_path(&mut self, path: String) { let _ = std::env::set_current_dir(&path); self.path = path.clone(); From 7427ea51dff067cc14d1c312befb2f92a45115fc Mon Sep 17 00:00:00 2001 From: Jonathan Rothberg Date: Sat, 7 Sep 2019 15:43:30 -0700 Subject: [PATCH 2/2] Removed commented out code. --- src/commands/pwd.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/commands/pwd.rs b/src/commands/pwd.rs index aa56c5165..37e2668bd 100644 --- a/src/commands/pwd.rs +++ b/src/commands/pwd.rs @@ -1,9 +1,7 @@ use crate::commands::WholeStreamCommand; -// use crate::data::{Dictionary, Value}; use crate::errors::ShellError; use crate::parser::registry::Signature; use crate::prelude::*; -// use indexmap::IndexMap; pub struct PWD;