mirror of
https://github.com/sajenim/rush.git
synced 2025-06-28 09:54:21 +08:00
refactor
This commit is contained in:
parent
40d73ecac0
commit
cc104f3f42
3 changed files with 10 additions and 10 deletions
|
@ -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();
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -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),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue