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()
|
||||
}
|
||||
|
||||
pub fn get_command(tokens: Vec<&str>) -> &str {
|
||||
tokens[0]
|
||||
pub fn get_command(tokens: &[String]) -> String {
|
||||
tokens[0].clone()
|
||||
}
|
||||
|
||||
pub fn get_args(tokens: Vec<&str>) -> Vec<&str> {
|
||||
pub fn get_args(tokens: &[String]) -> Vec<String> {
|
||||
tokens[1..].to_vec()
|
||||
}
|
||||
|
||||
pub fn execute(cmd: &str, args: Vec<&str>) {
|
||||
pub fn execute(cmd: &str, args: &[String]) {
|
||||
let status = Command::new(cmd)
|
||||
.args(args)
|
||||
.status();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::env;
|
||||
|
||||
pub fn cd(args: Vec<&str>) {
|
||||
pub fn cd(args: &[String]) {
|
||||
let path = args[0].to_string();
|
||||
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.
|
||||
let cmd = core::get_command(tokens.clone());
|
||||
let args = core::get_args(tokens);
|
||||
let cmd = core::get_command(&tokens);
|
||||
let args = core::get_args(&tokens);
|
||||
|
||||
// Execute inbuilt commands if supplied, otherwise execute command and argument.
|
||||
match cmd {
|
||||
"cd" => inbuilt::cd(args),
|
||||
match cmd.as_str() {
|
||||
"cd" => inbuilt::cd(&args),
|
||||
"help" => inbuilt::help(),
|
||||
"exit" => inbuilt::exit(),
|
||||
_ => core::execute(cmd, args),
|
||||
_ => core::execute(&cmd, &args),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue