mirror of
https://github.com/sajenim/rush.git
synced 2025-06-28 01:44: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()
|
||||
.read_line(&mut input)
|
||||
.expect("failed to readline");
|
||||
// Remove newline character from the input.
|
||||
input.pop();
|
||||
return input;
|
||||
input
|
||||
}
|
||||
|
||||
fn parse_input(s: &str) -> Vec<&str> {
|
||||
return s.split_whitespace().collect();
|
||||
s.split_whitespace().collect()
|
||||
}
|
||||
|
||||
fn get_command(v: Vec<&str>) -> String {
|
||||
return v[0..1].concat();
|
||||
fn get_command(tokens: Vec<&str>) -> &str {
|
||||
tokens[0]
|
||||
}
|
||||
|
||||
fn get_args(v: Vec<&str>) -> Vec<&str> {
|
||||
return v[1..].to_vec();
|
||||
fn get_args(tokens: Vec<&str>) -> Vec<&str> {
|
||||
tokens[1..].to_vec()
|
||||
}
|
||||
|
||||
fn run_command(cmd: String, args: Vec<&str>) {
|
||||
let mut child = Command::new(cmd)
|
||||
fn run_command(cmd: &str, args: Vec<&str>) {
|
||||
let status = Command::new(cmd)
|
||||
.args(args)
|
||||
.spawn()
|
||||
.status()
|
||||
.expect("failed to execute process");
|
||||
|
||||
child.wait().expect("failed to wait on child");
|
||||
assert!(status.success());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
loop {
|
||||
// Display the prompt for the user and wait for input.
|
||||
// Display the prompt for the user.
|
||||
display_prompt();
|
||||
|
||||
// Get the users input and store it as &str
|
||||
// Get the users input.
|
||||
let input = get_input();
|
||||
let input = input.as_str();
|
||||
|
||||
// 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.
|
||||
let cmd = get_command(tokens.clone());
|
||||
let args = get_args(tokens.clone());
|
||||
let args = get_args(tokens);
|
||||
|
||||
// Run the command and arguments.
|
||||
run_command(cmd, args);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue