mirror of
https://github.com/sajenim/rush.git
synced 2025-06-28 09:54:21 +08:00
refactor
This commit is contained in:
parent
03b3b3088a
commit
28afb92461
1 changed files with 20 additions and 15 deletions
35
src/main.rs
35
src/main.rs
|
@ -11,46 +11,51 @@ fn get_input() -> String {
|
||||||
io::stdin()
|
io::stdin()
|
||||||
.read_line(&mut input)
|
.read_line(&mut input)
|
||||||
.expect("failed to readline");
|
.expect("failed to readline");
|
||||||
|
// Remove newline character from the input.
|
||||||
input.pop();
|
input.pop();
|
||||||
return input;
|
input
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_input(s: &str) -> Vec<&str> {
|
fn parse_input(s: &str) -> Vec<&str> {
|
||||||
return s.split_whitespace().collect();
|
s.split_whitespace().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_command(v: Vec<&str>) -> String {
|
fn get_command(tokens: Vec<&str>) -> &str {
|
||||||
return v[0..1].concat();
|
tokens[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_args(v: Vec<&str>) -> Vec<&str> {
|
fn get_args(tokens: Vec<&str>) -> Vec<&str> {
|
||||||
return v[1..].to_vec();
|
tokens[1..].to_vec()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_command(cmd: String, args: Vec<&str>) {
|
fn run_command(cmd: &str, args: Vec<&str>) {
|
||||||
let mut child = Command::new(cmd)
|
let status = Command::new(cmd)
|
||||||
.args(args)
|
.args(args)
|
||||||
.spawn()
|
.status()
|
||||||
.expect("failed to execute process");
|
.expect("failed to execute process");
|
||||||
|
|
||||||
child.wait().expect("failed to wait on child");
|
assert!(status.success());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
loop {
|
loop {
|
||||||
// Display the prompt for the user and wait for input.
|
// Display the prompt for the user.
|
||||||
display_prompt();
|
display_prompt();
|
||||||
|
|
||||||
// Get the users input and store it as &str
|
// Get the users input.
|
||||||
let input = get_input();
|
let input = get_input();
|
||||||
let input = input.as_str();
|
|
||||||
|
|
||||||
// Parse the users input and create tokens.
|
// Parse the users input and create tokens.
|
||||||
let tokens = parse_input(input);
|
let tokens = parse_input(&input);
|
||||||
|
|
||||||
|
// If the user enters empty input don't execute the command.
|
||||||
|
if tokens.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Convert our tokens to our command and arguments.
|
// Convert our tokens to our command and arguments.
|
||||||
let cmd = get_command(tokens.clone());
|
let cmd = get_command(tokens.clone());
|
||||||
let args = get_args(tokens.clone());
|
let args = get_args(tokens);
|
||||||
|
|
||||||
// Run the command and arguments.
|
// Run the command and arguments.
|
||||||
run_command(cmd, args);
|
run_command(cmd, args);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue