From b341da5771c0bb607caf917b1293153b38a5bf9c Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 6 Jul 2019 13:13:01 -0400 Subject: have eval streams also emit start events --- src/builtins.rs | 40 +++++++++++++++++++++++++--------------- src/eval.rs | 1 + src/main.rs | 1 + src/process.rs | 16 ++++++++++++++++ src/repl.rs | 5 ++++- 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/src/builtins.rs b/src/builtins.rs index d130907..2393e89 100644 --- a/src/builtins.rs +++ b/src/builtins.rs @@ -41,6 +41,7 @@ pub fn exec(cmd: &str, args: &[String]) -> Result { pub struct Builtin { cmd: String, args: Vec, + started: bool, done: bool, } @@ -50,6 +51,7 @@ impl Builtin { "cd" => Ok(Self { cmd: cmd.to_string(), args: args.to_vec(), + started: false, done: false, }), _ => Err(Error::UnknownBuiltin { @@ -65,22 +67,30 @@ impl futures::stream::Stream for Builtin { type Error = Error; fn poll(&mut self) -> futures::Poll, Self::Error> { - if self.done { - return Ok(futures::Async::Ready(None)); + if !self.started { + self.started = true; + Ok(futures::Async::Ready(Some( + crate::eval::CommandEvent::CommandStart( + self.cmd.clone(), + self.args.clone(), + ), + ))) + } else if !self.done { + self.done = true; + let res = match self.cmd.as_ref() { + "cd" => cd(&self.args), + _ => Err(Error::UnknownBuiltin { + cmd: self.cmd.clone(), + }), + }; + res.map(|_| { + futures::Async::Ready(Some( + crate::eval::CommandEvent::BuiltinExit, + )) + }) + } else { + Ok(futures::Async::Ready(None)) } - - self.done = true; - let res = match self.cmd.as_ref() { - "cd" => cd(&self.args), - _ => Err(Error::UnknownBuiltin { - cmd: self.cmd.clone(), - }), - }; - res.map(|_| { - futures::Async::Ready(Some( - crate::eval::CommandEvent::BuiltinExit, - )) - }) } } diff --git a/src/eval.rs b/src/eval.rs index dd68412..5f789b3 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -35,6 +35,7 @@ pub fn eval(line: &str) -> Result { } pub enum CommandEvent { + CommandStart(String, Vec), Output(Vec), ProcessExit(std::process::ExitStatus), BuiltinExit, diff --git a/src/main.rs b/src/main.rs index 3155564..30b4f14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ // clippy::cargo seems to be broken with rls currently // #![warn(clippy::cargo)] #![allow(clippy::collapsible_if)] +#![allow(clippy::if_not_else)] #![allow(clippy::module_name_repetitions)] #![allow(clippy::multiple_crate_versions)] #![allow(clippy::single_match)] diff --git a/src/process.rs b/src/process.rs index 090824a..81899d7 100644 --- a/src/process.rs +++ b/src/process.rs @@ -46,7 +46,10 @@ pub struct RunningProcess { // TODO: tokio::io::Stdin is broken // input: tokio::io::Stdin, input: tokio::reactor::PollEvented2, + cmd: String, + args: Vec, buf: Vec, + started: bool, output_done: bool, exit_done: bool, _screen: crossterm::RawScreen, @@ -70,7 +73,10 @@ impl RunningProcess { pty, process, input, + cmd: cmd.to_string(), + args: args.to_vec(), buf: Vec::with_capacity(4096), + started: false, output_done: false, exit_done: false, _screen: crossterm::RawScreen::into_raw_mode() @@ -85,6 +91,16 @@ impl futures::stream::Stream for RunningProcess { type Error = Error; fn poll(&mut self) -> futures::Poll, Self::Error> { + if !self.started { + self.started = true; + return Ok(futures::Async::Ready(Some( + crate::eval::CommandEvent::CommandStart( + self.cmd.clone(), + self.args.clone(), + ), + ))); + } + let ready = mio::Ready::readable(); let input_poll = self.input.poll_read_ready(ready); match input_poll { diff --git a/src/repl.rs b/src/repl.rs index 30d852e..2df42a8 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -24,8 +24,11 @@ pub fn repl() { } let repl = read().and_then(|line| { - eprint!("running '{}'\r\n", line); eval(&line).fold(None, |acc, event| match event { + crate::eval::CommandEvent::CommandStart(cmd, args) => { + eprint!("running '{} {:?}'\r\n", cmd, args); + futures::future::ok(acc) + } crate::eval::CommandEvent::Output(out) => match print(&out) { Ok(()) => futures::future::ok(acc), Err(e) => futures::future::err(e), -- cgit v1.2.3-54-g00ecf