From 1ce0327bdb42691503c0b37c6b7de5f95809c8f1 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 3 Apr 2019 14:35:11 +0200 Subject: [PATCH] Avoid spamming notifications, fix multi build --- src/client.rs | 2 +- src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++---------- src/server.rs | 3 ++- 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/client.rs b/src/client.rs index 1ef9146..87448dc 100644 --- a/src/client.rs +++ b/src/client.rs @@ -47,7 +47,7 @@ fn fetch_url(uri: hyper::Uri) -> impl Future { // If there was an error, build in client .map_err(move |_| { warn!("{}", "server not listening, building in client..."); - let builder = GeneralBuilder::new(); + let mut builder = GeneralBuilder::new(); let uri = percent_decode(uri.path().as_bytes()).decode_utf8().unwrap(); let (path, args) = builder_arguments_from_string(&*uri); let _ = builder.build(&path, &args); diff --git a/src/lib.rs b/src/lib.rs index a3864c0..6ea542a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,9 +11,9 @@ use std::time::Duration; use colored::*; -use notify::{Watcher, RecursiveMode, watcher}; +use notify::{Watcher, RecursiveMode, watcher, DebouncedEvent}; -use notify_rust::Notification; +use notify_rust::{Notification, NotificationHandle}; /// The different types of error that can occur. #[derive(Debug)] @@ -116,6 +116,7 @@ pub fn watch>(p: P) -> Result<(), Error> { path.push(p.as_ref()); thread::spawn(move || { + let mut builder = GeneralBuilder::new(); let (tx, rx) = channel(); let mut watcher = watcher(tx, Duration::from_millis(200)).unwrap(); watcher.watch(&path, RecursiveMode::Recursive).unwrap(); @@ -124,8 +125,11 @@ pub fn watch>(p: P) -> Result<(), Error> { loop { match rx.recv() { - Ok(_) => { - let builder = GeneralBuilder::new(); + Ok(DebouncedEvent::NoticeWrite(_)) | + Ok(DebouncedEvent::Write(_)) | + Ok(DebouncedEvent::Create(_)) | + Ok(DebouncedEvent::Rename(_, _)) | + Ok(DebouncedEvent::Chmod(_)) => { let start_string = format!("---- STARTING BUILD ---- from {}", path.display()); println!("{}", start_string.bold().green()); @@ -141,6 +145,7 @@ pub fn watch>(p: P) -> Result<(), Error> { println!(); }, Err(e) => error!("watch error: {:?}", e), + _ => (), } } }); @@ -158,6 +163,9 @@ pub struct GeneralBuilder { /// The builders contained. builders: Vec>, + + /// The id of the notification to update a notification if possible. + notification_handle: Option, } impl GeneralBuilder { @@ -203,11 +211,12 @@ impl GeneralBuilder { Box::new(CMakeBuilder::new()), Box::new(CargoBuilder::new()), ], + notification_handle: None, } } /// Triggers a build. - pub fn build(&self, path: &PathBuf, args: &Vec) -> Result<(), Error> { + pub fn build(&mut self, path: &PathBuf, args: &Vec) -> Result<(), Error> { let result = build(path, args, &self.builders); match result { @@ -219,13 +228,32 @@ impl GeneralBuilder { } /// Sends a notification of a successful build. - pub fn notify_success(&self) { + pub fn notify_success(&mut self) { + + match self.notification_handle.as_mut() { + Some(handle) => { + handle + .appname("Mars") + .summary("Success") + .body("Mars finished successfully") + .icon(self.success.to_str().unwrap()); + + handle.update(); + }, + + None => { + let handle = Notification::new() + .appname("Mars") + .summary("Success") + .body("Mars finished successfully") + .icon(self.success.to_str().unwrap()) + .show(); + + self.notification_handle = handle.ok(); + }, + } let _ = Notification::new() - .appname("Mars") - .summary("Success") - .body("Mars finished successfully") - .icon(self.success.to_str().unwrap()) .show(); } diff --git a/src/server.rs b/src/server.rs index 19a8640..ea27336 100644 --- a/src/server.rs +++ b/src/server.rs @@ -53,6 +53,8 @@ fn main() { }); + let mut builder = GeneralBuilder::new(); + for _ in rx { let uri = { @@ -62,7 +64,6 @@ fn main() { if let Some(uri) = uri { - let builder = GeneralBuilder::new(); let uri = percent_decode(uri.path().as_bytes()).decode_utf8().unwrap(); let (path, args) = builder_arguments_from_string(&*uri);