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();