mirror of
https://github.com/sajenim/rush.git
synced 2025-06-28 01:44:21 +08:00
a very basic shell
This commit is contained in:
parent
566cc72407
commit
8e47b73567
1 changed files with 56 additions and 0 deletions
56
src/main.rs
Normal file
56
src/main.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
use std::io::{self, Write};
|
||||
use std::process::Command;
|
||||
|
||||
fn display_prompt() {
|
||||
print!("> ");
|
||||
io::stdout().flush().expect("unable to flush buffer");
|
||||
}
|
||||
|
||||
fn get_input() -> String {
|
||||
let mut input = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("failed to readline");
|
||||
input.pop();
|
||||
return input;
|
||||
}
|
||||
|
||||
fn parse_input(s: &str) -> Vec<&str> {
|
||||
return s.split_whitespace().collect();
|
||||
}
|
||||
|
||||
fn get_command(v: Vec<&str>) -> String {
|
||||
return v[0..1].concat();
|
||||
}
|
||||
|
||||
fn get_args(v: Vec<&str>) -> Vec<&str> {
|
||||
return v[1..].to_vec();
|
||||
}
|
||||
|
||||
fn run_command(cmd: String, args: Vec<&str>) {
|
||||
Command::new(cmd)
|
||||
.args(args)
|
||||
.spawn()
|
||||
.expect("failed to execute process");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
loop {
|
||||
// Display the prompt for the user and wait for input.
|
||||
display_prompt();
|
||||
|
||||
// Get the users input and store it as &str
|
||||
let input = get_input();
|
||||
let input = input.as_str();
|
||||
|
||||
// Parse the users input and create tokens.
|
||||
let tokens = parse_input(input);
|
||||
|
||||
// Convert our tokens to our command and arguments.
|
||||
let cmd = get_command(tokens.clone());
|
||||
let args = get_args(tokens.clone());
|
||||
|
||||
// Run the command and arguments.
|
||||
run_command(cmd, args);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue