This commit is contained in:
♥ Minnie ♥ 2025-06-08 09:06:21 +08:00
parent 40d73ecac0
commit cc104f3f42
Signed by: jasmine
GPG key ID: 8563E358D4E8040E
3 changed files with 10 additions and 10 deletions

View file

@ -20,15 +20,15 @@ pub fn parse_input(s: &str) -> Vec<String> {
shlex::split(s).unwrap() shlex::split(s).unwrap()
} }
pub fn get_command(tokens: Vec<&str>) -> &str { pub fn get_command(tokens: &[String]) -> String {
tokens[0] tokens[0].clone()
} }
pub fn get_args(tokens: Vec<&str>) -> Vec<&str> { pub fn get_args(tokens: &[String]) -> Vec<String> {
tokens[1..].to_vec() tokens[1..].to_vec()
} }
pub fn execute(cmd: &str, args: Vec<&str>) { pub fn execute(cmd: &str, args: &[String]) {
let status = Command::new(cmd) let status = Command::new(cmd)
.args(args) .args(args)
.status(); .status();

View file

@ -1,6 +1,6 @@
use std::env; use std::env;
pub fn cd(args: Vec<&str>) { pub fn cd(args: &[String]) {
let path = args[0].to_string(); let path = args[0].to_string();
assert!(env::set_current_dir(&path).is_ok()); assert!(env::set_current_dir(&path).is_ok());
} }

View file

@ -18,15 +18,15 @@ fn main() {
} }
// Convert our tokens to our command and arguments. // Convert our tokens to our command and arguments.
let cmd = core::get_command(tokens.clone()); let cmd = core::get_command(&tokens);
let args = core::get_args(tokens); let args = core::get_args(&tokens);
// Execute inbuilt commands if supplied, otherwise execute command and argument. // Execute inbuilt commands if supplied, otherwise execute command and argument.
match cmd { match cmd.as_str() {
"cd" => inbuilt::cd(args), "cd" => inbuilt::cd(&args),
"help" => inbuilt::help(), "help" => inbuilt::help(),
"exit" => inbuilt::exit(), "exit" => inbuilt::exit(),
_ => core::execute(cmd, args), _ => core::execute(&cmd, &args),
} }
} }
} }