Hook up --notify

This commit is contained in:
Félix Saparelli 2021-12-24 19:06:01 +13:00
parent 6f8049dd93
commit 08a1e7cc67
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
3 changed files with 27 additions and 63 deletions

View File

@ -146,9 +146,9 @@ pub fn get_args(tagged_filterer: bool) -> Result<ArgMatches<'static>> {
.short("W")
.hidden(true)
.long("watch-when-idle"))
.arg(Arg::with_name("notif") // TODO
.arg(Arg::with_name("notif")
.help_heading(Some(OPTSET_OUTPUT))
.help("Send a desktop notification when watchexec notices a change (experimental, behaviour may change)")
.help("Send a desktop notification when the command ends")
.short("N")
.long("notify"));

View File

@ -2,6 +2,7 @@ use std::{convert::Infallible, env::current_dir, path::Path, str::FromStr, time:
use clap::ArgMatches;
use miette::{IntoDiagnostic, Result};
use notify_rust::Notification;
use watchexec::{
action::{Action, Outcome},
command::Shell,
@ -59,6 +60,7 @@ pub fn runtime(args: &ArgMatches<'static>) -> Result<RuntimeConfig> {
});
let clear = args.is_present("clear");
let notif = args.is_present("notif");
let mut on_busy = args
.value_of("on-busy-update")
.unwrap_or("queue")
@ -131,23 +133,37 @@ pub fn runtime(args: &ArgMatches<'static>) -> Result<RuntimeConfig> {
let completion = action.events.iter().flat_map(|e| e.completions()).next();
if let Some(status) = completion {
match status {
let (msg, printit) = match status {
Some(ProcessEnd::ExitError(code)) => {
eprintln!("[Command exited with {}]", code);
(format!("Command exited with {}", code), true)
}
Some(ProcessEnd::ExitSignal(sig)) => {
eprintln!("[Command killed by {:?}]", sig);
(format!("Command killed by {:?}", sig), true)
}
Some(ProcessEnd::ExitStop(sig)) => {
eprintln!("[Command stopped by {:?}]", sig);
}
Some(ProcessEnd::Continued) => {
eprintln!("[Command continued]");
(format!("Command stopped by {:?}", sig), true)
}
Some(ProcessEnd::Continued) => ("Command continued".to_string(), true),
Some(ProcessEnd::Exception(ex)) => {
eprintln!("[Command ended by exception {:#x}]", ex);
(format!("Command ended by exception {:#x}", ex), true)
}
_ => {}
Some(ProcessEnd::Success) => ("Command was successful".to_string(), false),
None => ("Command completed".to_string(), false),
};
if printit {
eprintln!("{}", msg);
}
if notif {
Notification::new()
.summary("Watchexec: command ended")
.body(&msg)
.show()
.map(drop)
.unwrap_or_else(|err| {
eprintln!("Failed to send desktop notification: {}", err);
});
}
action.outcome(Outcome::DoNothing);

View File

@ -1,52 +0,0 @@
use color_eyre::eyre;
use log::{warn, LevelFilter};
use notify_rust::Notification;
use watchexec::{
config::Config,
error::Result,
pathop::PathOp,
run::{ExecHandler, Handler},
};
pub struct CliHandler {
pub inner: ExecHandler,
pub log_level: LevelFilter,
pub notify: bool,
}
impl CliHandler {
pub fn new(config: Config, log_level: LevelFilter, notify: bool) -> eyre::Result<Self> {
Ok(Self {
inner: ExecHandler::new(config)?,
log_level,
notify,
})
}
}
impl Handler for CliHandler {
fn args(&self) -> Config {
self.inner.args()
}
fn on_manual(&self) -> Result<bool> {
self.inner.on_manual()
}
fn on_update(&self, ops: &[PathOp]) -> Result<bool> {
self.inner.on_update(ops).map(|o| {
if self.notify {
Notification::new()
.summary("Watchexec observed a change")
.body("Watchexec has seen a change, the command may have restarted.")
.show()
.map(drop)
.unwrap_or_else(|err| {
warn!("Failed to send desktop notification: {}", err);
});
}
o
})
}
}