diff --git a/src/cli.rs b/src/cli.rs index 66572d3d8..706b76702 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -164,6 +164,7 @@ pub async fn cli() -> Result<(), Box> { context.add_commands(vec![ whole_stream_command(PS), + whole_stream_command(PWD), whole_stream_command(LS), whole_stream_command(CD), whole_stream_command(Size), diff --git a/src/commands.rs b/src/commands.rs index cd44fbb09..e0e1c80e5 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -39,6 +39,7 @@ pub(crate) mod plugin; pub(crate) mod post; pub(crate) mod prev; pub(crate) mod ps; +pub(crate) mod pwd; pub(crate) mod reject; pub(crate) mod reverse; pub(crate) mod rm; @@ -104,6 +105,7 @@ pub(crate) use pick::Pick; pub(crate) use post::Post; pub(crate) use prev::Previous; pub(crate) use ps::PS; +pub(crate) use pwd::PWD; pub(crate) use reject::Reject; pub(crate) use reverse::Reverse; pub(crate) use rm::Remove; diff --git a/src/commands/pwd.rs b/src/commands/pwd.rs new file mode 100644 index 000000000..2fd88ce8b --- /dev/null +++ b/src/commands/pwd.rs @@ -0,0 +1,44 @@ +use crate::commands::WholeStreamCommand; +use crate::errors::ShellError; +use crate::data::{Dictionary, Value}; +use crate::parser::registry::Signature; +use crate::prelude::*; +use indexmap::IndexMap; + +pub struct PWD; + +impl WholeStreamCommand for PWD { + fn name(&self) -> &str { + "pwd" + } + + fn signature(&self) -> Signature { + Signature::build("pwd") + } + + fn usage(&self) -> &str { + "Output the current working directory." + } + + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + pwd(args, registry) + } +} + +pub fn pwd(args: CommandArgs, registry: &CommandRegistry) -> Result { + 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)) +}