From 645ab74c625f88910aa4adb57d6b283d53233285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Fri, 3 Sep 2021 09:25:23 +1200 Subject: [PATCH] Implement Outcome::Wait, and CLI on-busy=queue --- cli/src/config.rs | 39 +++++++++++++++++++-------------------- lib/src/action.rs | 14 +++++++++++--- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/cli/src/config.rs b/cli/src/config.rs index 17c6e52..c036fbf 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -100,8 +100,7 @@ fn runtime(args: &ArgMatches<'static>) -> Result { } if once { - // TODO - // action.outcome(Outcome::both(Outcome::Start, Outcome::both(Outcome::Wait, Outcome::Exit)); + action.outcome(Outcome::both(Outcome::Start, Outcome::wait(Outcome::Exit))); return fut; } @@ -125,23 +124,23 @@ fn runtime(args: &ArgMatches<'static>) -> Result { if !has_paths { if !signals.is_empty() { - let mut out = Outcome::DoNothing; - for sig in signals { - out = Outcome::both( - out, - Outcome::Signal(match sig { - InputSignal::Hangup => Signal::SIGHUP, - InputSignal::Interrupt => Signal::SIGINT, - InputSignal::Quit => Signal::SIGQUIT, - InputSignal::Terminate => Signal::SIGTERM, - InputSignal::User1 => Signal::SIGUSR1, - InputSignal::User2 => Signal::SIGUSR2, - }), - ); - } + let mut out = Outcome::DoNothing; + for sig in signals { + out = Outcome::both( + out, + Outcome::Signal(match sig { + InputSignal::Hangup => Signal::SIGHUP, + InputSignal::Interrupt => Signal::SIGINT, + InputSignal::Quit => Signal::SIGQUIT, + InputSignal::Terminate => Signal::SIGTERM, + InputSignal::User1 => Signal::SIGUSR1, + InputSignal::User2 => Signal::SIGUSR2, + }), + ); + } - action.outcome(out); - return fut; + action.outcome(out); + return fut; } let completion = action.events.iter().flat_map(|e| e.completions()).next(); @@ -165,8 +164,8 @@ fn runtime(args: &ArgMatches<'static>) -> Result { } (false, "restart") => Outcome::both(Outcome::Stop, Outcome::Start), (_, "signal") => Outcome::Signal(signal), - // (true, "queue") => Outcome::wait(Outcome::both(Outcome::Clear, Outcome::Start)), - // (false, "queue") => Outcome::wait(Outcome::Start), + (true, "queue") => Outcome::wait(Outcome::both(Outcome::Clear, Outcome::Start)), + (false, "queue") => Outcome::wait(Outcome::Start), _ => Outcome::DoNothing, }; diff --git a/lib/src/action.rs b/lib/src/action.rs index 6090ba4..c87e545 100644 --- a/lib/src/action.rs +++ b/lib/src/action.rs @@ -148,9 +148,9 @@ pub enum Outcome { /// If the command isn't running, start it. Start, - // TODO - // /// Wait for command completion, then start a new one. - // Queue, + /// Wait for command completion. + Wait, + /// Send this signal to the command. Signal(Signal), @@ -182,6 +182,10 @@ impl Outcome { Self::Both(Box::new(one), Box::new(two)) } + pub fn wait(and_then: Outcome) -> Self { + Self::Both(Box::new(Outcome::Wait), Box::new(and_then)) + } + fn resolve(self, is_running: bool) -> Self { match (is_running, self) { (true, Self::IfRunning(then, _)) => then.resolve(true), @@ -379,6 +383,10 @@ async fn apply_outcome( p.signal(sig).await; } + (Some(p), Outcome::Wait) => { + p.wait().await?; + } + (_, Outcome::Clear) => { clearscreen::clear()?; }