feat: add echo, fix annoying windows bug
This commit is contained in:
parent
036b1147f3
commit
20c093e5a4
5 changed files with 32 additions and 20 deletions
12
coreutils/src/commands/echo.rs
Normal file
12
coreutils/src/commands/echo.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use boxutils::commands::Command;
|
||||
|
||||
pub struct Echo;
|
||||
|
||||
impl Command for Echo {
|
||||
fn execute(&self) {
|
||||
let args: Vec<String> = std::env::args().collect::<Vec<_>>();
|
||||
let arguments: Vec<String> = boxutils::commands::get_args(String::from("echo"), args);
|
||||
let message = &arguments.join(" ");
|
||||
println!("{}", message);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,3 +3,6 @@ pub use hello::Hello;
|
|||
|
||||
mod cat;
|
||||
pub use cat::Cat;
|
||||
|
||||
mod echo;
|
||||
pub use echo::Echo;
|
||||
12
src/main.rs
12
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::<Vec<String>>();
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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::<Vec<String>>();
|
||||
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<String>) -> Vec<String> {
|
||||
let mut arguments: Vec<String> = 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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue