Browse Source

feat: implement basic command parsing

main
fdai7375 2 years ago
parent
commit
c96f2ebc93
  1. 6
      src/main.rs
  2. 22
      src/parse.rs

6
src/main.rs

@ -1,5 +1,7 @@
mod parse;
mod prompt; mod prompt;
use parse::parse_line;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
use rustyline::{Editor, Result}; use rustyline::{Editor, Result};
@ -15,7 +17,9 @@ fn main() -> Result<()> {
match readline { match readline {
Ok(line) => { Ok(line) => {
println!("Line: {}", line);
if let Err(e) = parse_line(&line) {
println!("{}", e)
}
} }
Err(ReadlineError::Interrupted) => { Err(ReadlineError::Interrupted) => {
println!("CTRL-C"); println!("CTRL-C");

22
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::<Vec<&str>>();
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(())
}
Loading…
Cancel
Save