Avoid spamming notifications, fix multi build

This commit is contained in:
Thomas Forgione 2019-04-03 14:35:11 +02:00
parent 52ef41b9aa
commit 1ce0327bdb
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
3 changed files with 41 additions and 12 deletions

View File

@ -47,7 +47,7 @@ fn fetch_url(uri: hyper::Uri) -> impl Future<Item=(), Error=()> {
// 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);

View File

@ -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: AsRef<Path>>(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: AsRef<Path>>(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: AsRef<Path>>(p: P) -> Result<(), Error> {
println!();
},
Err(e) => error!("watch error: {:?}", e),
_ => (),
}
}
});
@ -158,6 +163,9 @@ pub struct GeneralBuilder {
/// The builders contained.
builders: Vec<Box<Builder>>,
/// The id of the notification to update a notification if possible.
notification_handle: Option<NotificationHandle>,
}
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<String>) -> Result<(), Error> {
pub fn build(&mut self, path: &PathBuf, args: &Vec<String>) -> Result<(), Error> {
let result = build(path, args, &self.builders);
match result {
@ -219,15 +228,34 @@ impl GeneralBuilder {
}
/// Sends a notification of a successful build.
pub fn notify_success(&self) {
pub fn notify_success(&mut self) {
let _ = Notification::new()
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()
.show();
}
/// Sends a notification of an error.

View File

@ -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);