From 20c093e5a4f31b914754022b37620ed345f4ba99 Mon Sep 17 00:00:00 2001 From: teesh3rt Date: Thu, 20 Mar 2025 19:39:45 +0200 Subject: [PATCH] feat: add echo, fix annoying windows bug --- coreutils/src/commands/echo.rs | 12 ++++++++++++ coreutils/src/commands/mod.rs | 3 +++ src/main.rs | 12 +----------- src/registry.rs | 5 +++-- utils/src/commands.rs | 20 +++++++++++++------- 5 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 coreutils/src/commands/echo.rs diff --git a/coreutils/src/commands/echo.rs b/coreutils/src/commands/echo.rs new file mode 100644 index 0000000..aa96efc --- /dev/null +++ b/coreutils/src/commands/echo.rs @@ -0,0 +1,12 @@ +use boxutils::commands::Command; + +pub struct Echo; + +impl Command for Echo { + fn execute(&self) { + let args: Vec = std::env::args().collect::>(); + let arguments: Vec = boxutils::commands::get_args(String::from("echo"), args); + let message = &arguments.join(" "); + println!("{}", message); + } +} diff --git a/coreutils/src/commands/mod.rs b/coreutils/src/commands/mod.rs index 57881a0..369a80c 100644 --- a/coreutils/src/commands/mod.rs +++ b/coreutils/src/commands/mod.rs @@ -3,3 +3,6 @@ pub use hello::Hello; mod cat; pub use cat::Cat; + +mod echo; +pub use echo::Echo; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7adb407..343fc74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,7 @@ mod boxcmd; mod registry; -use std::env; -use std::path::Path; - -fn being_called_as() -> String { - let args = env::args().collect::>(); - let exe_path = args[0].clone(); - let path = Path::new(&exe_path); - let being_called = path.file_name().unwrap().to_str().unwrap().to_string(); - let formatted = being_called.replace(".exe", ""); - formatted -} +use boxutils::commands::being_called_as; fn main() { let utility_name = being_called_as(); diff --git a/src/registry.rs b/src/registry.rs index 07429b7..a986e98 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -1,11 +1,12 @@ -use boxutils::registry::CommandRegistry; use super::boxcmd::Boxcmd; +use boxutils::registry::CommandRegistry; pub fn get_registry() -> CommandRegistry { -let mut registry = CommandRegistry::new(); + let mut registry = CommandRegistry::new(); registry.register("hello", Box::new(coreutils::commands::Hello)); registry.register("cat", Box::new(coreutils::commands::Cat)); + registry.register("echo", Box::new(coreutils::commands::Echo)); registry.register("box", Box::new(Boxcmd)); registry diff --git a/utils/src/commands.rs b/utils/src/commands.rs index 99d8040..0ebed45 100644 --- a/utils/src/commands.rs +++ b/utils/src/commands.rs @@ -1,15 +1,21 @@ +use std::env; use std::mem; +use std::path::Path; + +pub fn being_called_as() -> String { + let args = env::args().collect::>(); + let exe_path = args[0].clone(); + let path = Path::new(&exe_path); + let being_called = path.file_name().unwrap().to_str().unwrap().to_string(); + let formatted = being_called.replace(".exe", ""); + formatted +} pub fn get_args(commandname: String, args: Vec) -> Vec { let mut arguments: Vec = args.clone(); - let cmd = arguments[0].clone(); + let exe_name = being_called_as(); - let _ = mem::replace( - &mut arguments[0], - cmd - .replace(".exe", "") - .replace("./", "") - ); + let _ = mem::replace(&mut arguments[0], exe_name); if let Some(num) = arguments.iter().position(|x| *x == commandname) { arguments.drain(..=num);