Add a configuration variable to display a notification at the be… (#345)

This commit is contained in:
Roey Darwish Dror
2020-02-27 13:30:55 +02:00
committed by GitHub
parent 738d70c91d
commit 2392124f71
3 changed files with 35 additions and 0 deletions

View File

@@ -70,6 +70,7 @@ pub struct ConfigFile {
no_retry: Option<bool>, no_retry: Option<bool>,
run_in_tmux: Option<bool>, run_in_tmux: Option<bool>,
cleanup: Option<bool>, cleanup: Option<bool>,
notify_each_step: Option<bool>,
accept_all_windows_updates: Option<bool>, accept_all_windows_updates: Option<bool>,
only: Option<Vec<Step>>, only: Option<Vec<Step>>,
} }
@@ -355,6 +356,12 @@ impl Config {
self.config_file.accept_all_windows_updates.unwrap_or(true) 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 /// Extra yay arguments
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn yay_arguments(&self) -> &str { pub fn yay_arguments(&self) -> &str {

View File

@@ -42,6 +42,7 @@ fn run() -> Result<()> {
let config = Config::load(&base_dirs, opt)?; let config = Config::load(&base_dirs, opt)?;
terminal::set_title(config.set_title()); terminal::set_title(config.set_title());
terminal::set_desktop_notifications(config.notify_each_step());
debug!("Version: {}", crate_version!()); debug!("Version: {}", crate_version!());
debug!("OS: {}", env!("TARGET")); debug!("OS: {}", env!("TARGET"));

View File

@@ -34,6 +34,7 @@ struct Terminal {
prefix: String, prefix: String,
term: Term, term: Term,
set_title: bool, set_title: bool,
desktop_notification: bool,
} }
impl Terminal { impl Terminal {
@@ -46,9 +47,14 @@ impl Terminal {
.map(|prefix| format!("({}) ", prefix)) .map(|prefix| format!("({}) ", prefix))
.unwrap_or_else(|_| String::new()), .unwrap_or_else(|_| String::new()),
set_title: true, 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) { fn set_title(&mut self, set_title: bool) {
self.set_title = set_title self.set_title = set_title
} }
@@ -58,6 +64,20 @@ impl Terminal {
self.term self.term
.set_title(format!("{}Topgrade - {}", self.prefix, message.as_ref())); .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 now = Local::now();
let message = format!( let message = format!(
"{}{:02}:{:02}:{:02} - {}", "{}{:02}:{:02}:{:02} - {}",
@@ -225,3 +245,10 @@ pub fn get_char() -> char {
pub fn set_title(set_title: bool) { pub fn set_title(set_title: bool) {
TERMINAL.lock().unwrap().set_title(set_title); TERMINAL.lock().unwrap().set_title(set_title);
} }
pub fn set_desktop_notifications(desktop_notifications: bool) {
TERMINAL
.lock()
.unwrap()
.set_desktop_notifications(desktop_notifications);
}