Add experimental --notify option

See #139
This commit is contained in:
Félix Saparelli 2021-07-31 05:11:42 +12:00
parent a3173194a1
commit d54d74854e
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
4 changed files with 736 additions and 32 deletions

740
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,7 @@ path = "src/main.rs"
log = "0.4.14"
watchexec = { path = "../lib", version = "1.17.1" }
color-eyre = "0.5.11"
notify-rust = "4.5.2"
[dependencies.clap]
version = "2.33.3"

View File

@ -131,7 +131,11 @@ pub fn get_args() -> Result<CliHandler> {
.arg(Arg::with_name("watch-when-idle")
.help("Deprecated alias for --on-busy-update=do-nothing, which will become the default in 2.0.")
.short("W")
.long("watch-when-idle"));
.long("watch-when-idle"))
.arg(Arg::with_name("notif")
.help("Send a desktop notification when watchexec notices a change (experimental, behaviour may change)")
.short("N")
.long("notify"));
let mut raw_args: Vec<OsString> = env::args_os().collect();
@ -279,7 +283,7 @@ pub fn get_args() -> Result<CliHandler> {
LevelFilter::Warn
};
CliHandler::new(config, loglevel, false)
CliHandler::new(config, loglevel, args.is_present("notif"))
}
// until 2.0

View File

@ -1,5 +1,6 @@
use color_eyre::eyre;
use log::LevelFilter;
use log::{warn, LevelFilter};
use notify_rust::Notification;
use watchexec::{
config::Config,
error::Result,
@ -31,7 +32,21 @@ impl Handler for CliHandler {
fn on_manual(&self) -> Result<bool> {
self.inner.on_manual()
}
fn on_update(&self, ops: &[PathOp]) -> Result<bool> {
self.inner.on_update(ops)
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
})
}
}