Browse Source

feat: add command execution

main
fdai7375 2 years ago
parent
commit
e7cf8a331f
  1. 12
      src/execute.rs
  2. 7
      src/main.rs
  3. 21
      src/parse.rs

12
src/execute.rs

@ -0,0 +1,12 @@
use std::{
io,
process::{Command, Stdio},
};
pub fn execute(command: &str, args: &[&str]) -> io::Result<()> {
let mut command = Command::new(command);
command.stdout(Stdio::piped()).args(args);
let handle = command.spawn()?;
io::copy(&mut handle.stdout.unwrap(), &mut io::stdout())?;
Ok(())
}

7
src/main.rs

@ -1,6 +1,8 @@
mod execute;
mod parse;
mod prompt;
use execute::execute;
use parse::parse_line;
use rustyline::error::ReadlineError;
use rustyline::{Editor, Result};
@ -17,9 +19,8 @@ fn main() -> Result<()> {
match readline {
Ok(line) => {
if let Err(e) = parse_line(&line) {
println!("{}", e)
}
let (command, args) = parse_line(&line);
execute(command, &args)?;
}
Err(ReadlineError::Interrupted) => {
println!("CTRL-C");

21
src/parse.rs

@ -1,22 +1,9 @@
use std::{
io::{self, Write},
process::Command,
};
pub fn parse_line(line: &str) -> io::Result<()> {
pub fn parse_line(line: &str) -> (&str, Vec<&str>) {
let tokens = line.trim().split(' ').collect::<Vec<&str>>();
let mut args: &[&str] = &[];
let mut args: Vec<&str> = Vec::new();
let command = tokens[0];
if tokens.len() > 1 {
args = &tokens[1..];
args = tokens[1..].to_vec();
}
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(())
(command, args)
}
Loading…
Cancel
Save