refactor(main): improve command input handling loop
All checks were successful
Release / Release (push) Successful in 39s
All checks were successful
Release / Release (push) Successful in 39s
This commit is contained in:
parent
dab3905078
commit
65f97a673b
21
src/main.rs
21
src/main.rs
@ -1,13 +1,26 @@
|
|||||||
use std::{io::stdin, process::Command};
|
use std::{io::{stdin, stdout, Write}, process::Command};
|
||||||
|
|
||||||
fn main(){
|
fn main(){
|
||||||
|
loop {
|
||||||
|
// use the `>` character as the prompt
|
||||||
|
// need to explicitly flush this to ensure it prints before read_line
|
||||||
|
print!("> ");
|
||||||
|
stdout().flush();
|
||||||
|
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
stdin().read_line(&mut input).unwrap();
|
stdin().read_line(&mut input).unwrap();
|
||||||
|
|
||||||
// read_line leaves a trailing newline, which trim removes
|
let mut parts = input.trim().split_whitespace();
|
||||||
let command = input.trim();
|
let command = parts.next().unwrap();
|
||||||
|
let args = parts;
|
||||||
|
|
||||||
Command::new(command)
|
let mut child = Command::new(command)
|
||||||
|
.args(args)
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// don't accept another command until this one completes
|
||||||
|
child.wait();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user