mirror of
https://github.com/sajenim/rush.git
synced 2025-06-28 09:54:21 +08:00
refactor
This commit is contained in:
parent
28afb92461
commit
4eb0c39a66
2 changed files with 47 additions and 45 deletions
38
src/core.rs
Normal file
38
src/core.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
use std::io::{self, Write};
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
pub fn display_prompt() {
|
||||||
|
print!("> ");
|
||||||
|
io::stdout().flush().expect("unable to flush buffer");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_input() -> String {
|
||||||
|
let mut input = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut input)
|
||||||
|
.expect("failed to readline");
|
||||||
|
// Remove newline character from the input.
|
||||||
|
input.pop();
|
||||||
|
input
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_input(s: &str) -> Vec<&str> {
|
||||||
|
s.split_whitespace().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_command(tokens: Vec<&str>) -> &str {
|
||||||
|
tokens[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_args(tokens: Vec<&str>) -> Vec<&str> {
|
||||||
|
tokens[1..].to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn execute(cmd: &str, args: Vec<&str>) {
|
||||||
|
let status = Command::new(cmd)
|
||||||
|
.args(args)
|
||||||
|
.status()
|
||||||
|
.expect("failed to execute process");
|
||||||
|
|
||||||
|
assert!(status.success());
|
||||||
|
}
|
54
src/main.rs
54
src/main.rs
|
@ -1,52 +1,16 @@
|
||||||
use std::io::{self, Write};
|
mod core;
|
||||||
use std::process::Command;
|
mod inbuilt;
|
||||||
|
|
||||||
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");
|
|
||||||
// Remove newline character from the input.
|
|
||||||
input.pop();
|
|
||||||
input
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_input(s: &str) -> Vec<&str> {
|
|
||||||
s.split_whitespace().collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_command(tokens: Vec<&str>) -> &str {
|
|
||||||
tokens[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_args(tokens: Vec<&str>) -> Vec<&str> {
|
|
||||||
tokens[1..].to_vec()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run_command(cmd: &str, args: Vec<&str>) {
|
|
||||||
let status = Command::new(cmd)
|
|
||||||
.args(args)
|
|
||||||
.status()
|
|
||||||
.expect("failed to execute process");
|
|
||||||
|
|
||||||
assert!(status.success());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
loop {
|
loop {
|
||||||
// Display the prompt for the user.
|
// Display the prompt for the user.
|
||||||
display_prompt();
|
core::display_prompt();
|
||||||
|
|
||||||
// Get the users input.
|
// Get the users input.
|
||||||
let input = get_input();
|
let input = core::get_input();
|
||||||
|
|
||||||
// Parse the users input and create tokens.
|
// Parse the users input and create tokens.
|
||||||
let tokens = parse_input(&input);
|
let tokens = core::parse_input(&input);
|
||||||
|
|
||||||
// If the user enters empty input don't execute the command.
|
// If the user enters empty input don't execute the command.
|
||||||
if tokens.is_empty() {
|
if tokens.is_empty() {
|
||||||
|
@ -54,10 +18,10 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert our tokens to our command and arguments.
|
// Convert our tokens to our command and arguments.
|
||||||
let cmd = get_command(tokens.clone());
|
let cmd = core::get_command(tokens.clone());
|
||||||
let args = get_args(tokens);
|
let args = core::get_args(tokens);
|
||||||
|
|
||||||
// Run the command and arguments.
|
// Execute command and argument.
|
||||||
run_command(cmd, args);
|
core::execute(cmd, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue