From 65f97a673b184b7881c5ae123dcbb952185f2a0c Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 28 Aug 2024 07:59:11 +0200 Subject: [PATCH] refactor(main): improve command input handling loop --- src/main.rs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index d4ce347..d43b637 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,26 @@ -use std::{io::stdin, process::Command}; +use std::{io::{stdin, stdout, Write}, process::Command}; fn main(){ - let mut input = String::new(); - stdin().read_line(&mut input).unwrap(); + loop { + // use the `>` character as the prompt + // need to explicitly flush this to ensure it prints before read_line + print!("> "); + stdout().flush(); - // read_line leaves a trailing newline, which trim removes - let command = input.trim(); + let mut input = String::new(); + stdin().read_line(&mut input).unwrap(); - Command::new(command) - .spawn() - .unwrap(); + let mut parts = input.trim().split_whitespace(); + let command = parts.next().unwrap(); + let args = parts; + + let mut child = Command::new(command) + .args(args) + .spawn() + .unwrap(); + + // don't accept another command until this one completes + child.wait(); + } } +