refactor(main): improve command input handling loop
All checks were successful
Release / Release (push) Successful in 39s

This commit is contained in:
Jan Gleytenhoover 2024-08-28 07:59:11 +02:00
parent dab3905078
commit 65f97a673b
Signed by: jank
GPG Key ID: B267751B8AE29EFE

@ -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();
} }
}