From 9c24b7ff261b494adada63b9a0442679c82e3b9b Mon Sep 17 00:00:00 2001 From: fdai7375 Date: Mon, 23 Jan 2023 11:41:41 +0100 Subject: [PATCH] feat: add pwd builtin command --- src/builtins/mod.rs | 2 ++ src/builtins/pwd.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/builtins/pwd.rs diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index 218f26e..addf4f6 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -5,6 +5,7 @@ use once_cell::unsync::Lazy; mod cd; mod change_prompt; mod exit; +mod pwd; pub struct BuiltinConfig { pub prompt_style: PromptStyle, @@ -27,6 +28,7 @@ const BUILTINS: Lazy)>> = Lazy::new(|| { ("cd", Box::new(cd::Cd)), ("change-prompt", Box::new(change_prompt::ChangePrompt)), ("exit", Box::new(exit::Exit)), + ("pwd", Box::new(pwd::Pwd)), ] }); diff --git a/src/builtins/pwd.rs b/src/builtins/pwd.rs new file mode 100644 index 0000000..e9f3cdb --- /dev/null +++ b/src/builtins/pwd.rs @@ -0,0 +1,15 @@ +use crate::error::ShellError; + +use super::{Builtin, BuiltinConfig}; + +pub struct Pwd; + +impl Builtin for Pwd { + fn execute(&mut self, _: &mut BuiltinConfig, _: Vec) -> Result<(), ShellError> { + match std::env::current_dir() { + Ok(p) => println!("{}", p.display()), + Err(e) => return Err(ShellError::ExecuteFailure(e.to_string())), + } + Ok(()) + } +}