From 2392124f71066cfcb5c5070fd14c693278784c30 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Thu, 27 Feb 2020 13:30:55 +0200 Subject: [PATCH] =?UTF-8?q?Add=20a=20configuration=20variable=20to=20displ?= =?UTF-8?q?ay=20a=20notification=20at=20the=20be=E2=80=A6=20(#345)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.rs | 7 +++++++ src/main.rs | 1 + src/terminal.rs | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/src/config.rs b/src/config.rs index 677e4e05..3c6f1912 100644 --- a/src/config.rs +++ b/src/config.rs @@ -70,6 +70,7 @@ pub struct ConfigFile { no_retry: Option, run_in_tmux: Option, cleanup: Option, + notify_each_step: Option, accept_all_windows_updates: Option, only: Option>, } @@ -355,6 +356,12 @@ impl Config { self.config_file.accept_all_windows_updates.unwrap_or(true) } + /// Whether to send a desktop notification at the beginning of every step + #[allow(dead_code)] + pub fn notify_each_step(&self) -> bool { + self.config_file.notify_each_step.unwrap_or(false) + } + /// Extra yay arguments #[cfg(target_os = "linux")] pub fn yay_arguments(&self) -> &str { diff --git a/src/main.rs b/src/main.rs index 0c8537a3..44568b43 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ fn run() -> Result<()> { let config = Config::load(&base_dirs, opt)?; terminal::set_title(config.set_title()); + terminal::set_desktop_notifications(config.notify_each_step()); debug!("Version: {}", crate_version!()); debug!("OS: {}", env!("TARGET")); diff --git a/src/terminal.rs b/src/terminal.rs index 8f28a5de..62a59a9c 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -34,6 +34,7 @@ struct Terminal { prefix: String, term: Term, set_title: bool, + desktop_notification: bool, } impl Terminal { @@ -46,9 +47,14 @@ impl Terminal { .map(|prefix| format!("({}) ", prefix)) .unwrap_or_else(|_| String::new()), set_title: true, + desktop_notification: false, } } + fn set_desktop_notifications(&mut self, desktop_notifications: bool) { + self.desktop_notification = desktop_notifications + } + fn set_title(&mut self, set_title: bool) { self.set_title = set_title } @@ -58,6 +64,20 @@ impl Terminal { self.term .set_title(format!("{}Topgrade - {}", self.prefix, message.as_ref())); } + + #[cfg(target_os = "macos")] + { + if self.desktop_notification { + Notification::new() + .summary("Topgrade") + .body(message.as_ref()) + .appname("topgrade") + .timeout(5) + .show() + .ok(); + } + } + let now = Local::now(); let message = format!( "{}{:02}:{:02}:{:02} - {}", @@ -225,3 +245,10 @@ pub fn get_char() -> char { pub fn set_title(set_title: bool) { TERMINAL.lock().unwrap().set_title(set_title); } + +pub fn set_desktop_notifications(desktop_notifications: bool) { + TERMINAL + .lock() + .unwrap() + .set_desktop_notifications(desktop_notifications); +}