From 2091fde2c0ce7a3c87b9186447937dc678572c80 Mon Sep 17 00:00:00 2001 From: fdai7375 Date: Tue, 17 Jan 2023 00:08:42 +0100 Subject: [PATCH] feat: implement basic command parsing --- src/main.rs | 6 +++++- src/parse.rs | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/parse.rs diff --git a/src/main.rs b/src/main.rs index 935329e..8fb89de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ +mod parse; mod prompt; +use parse::parse_line; use rustyline::error::ReadlineError; use rustyline::{Editor, Result}; @@ -15,7 +17,9 @@ fn main() -> Result<()> { match readline { Ok(line) => { - println!("Line: {}", line); + if let Err(e) = parse_line(&line) { + println!("{}", e) + } } Err(ReadlineError::Interrupted) => { println!("CTRL-C"); diff --git a/src/parse.rs b/src/parse.rs new file mode 100644 index 0000000..938369f --- /dev/null +++ b/src/parse.rs @@ -0,0 +1,22 @@ +use std::{ + io::{self, Write}, + process::Command, +}; + +pub fn parse_line(line: &str) -> io::Result<()> { + let tokens = line.trim().split(' ').collect::>(); + let mut args: &[&str] = &[]; + let command = tokens[0]; + if !args.is_empty() { + args = &tokens[1..]; + } + let mut command = Command::new(command); + + command.args(args); + + let output = command.output()?; + + io::stderr().write_all(&output.stderr)?; + io::stdout().write_all(&output.stdout)?; + Ok(()) +}