From fadbd780554f8d999ee4c5b7010b18088b1d589d Mon Sep 17 00:00:00 2001 From: Felix Ortmann Date: Sun, 27 Dec 2020 12:45:09 +0100 Subject: [PATCH] Add notification timeout --- README.md | 8 ++++++-- src/main.rs | 52 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index e2ecc0e..fa25fd1 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Send [D-Bus](https://www.freedesktop.org/wiki/Software/dbus/) notifications when ## Usage -You can run `batalert` with default setting to send the first notification when the battery falls below 15% and repeat the notification every 3%. `batalert` resets when you plug-in your charger. +You can run `batalert` with default setting to send the first notification when the battery falls below 15% and repeat the notification every 3%. Notifications timeout after 15 seconds. `batalert` resets when you plug-in your charger. ### Customization @@ -28,7 +28,11 @@ You can run `batalert` with default setting to send the first notification when ``` - Show a notification when the battery falls below 20%, repeat notification every 4% on 16%, 12%, 8% ... : ``` - batalert --threshold 20 --notification-step 4 + batalert --alert 20 --notification-step 4 + ``` +- Timeout notifications after a minute: + ``` + batalert --timeout 60 ``` - Use a custom icon: ``` diff --git a/src/main.rs b/src/main.rs index dcdd813..cc55389 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,11 +18,11 @@ fn main() { .help("Read the battery capacity from this uevent file."), ) .arg( - Arg::with_name("threshold") - .short("t") - .long("threshold") + Arg::with_name("alert") + .short("a") + .long("alert") .default_value("15") - .help("Send the first notification when battery falls below this"), + .help("Send the first notification when battery falls below this threshold"), ) .arg( Arg::with_name("notification-step") @@ -31,6 +31,13 @@ fn main() { .default_value("3") .help("Repeat notifications for every n percent the battery discharges"), ) + .arg( + Arg::with_name("timeout") + .short("t") + .long("timeout") + .default_value("15") + .help("Hide the notification after t seconds"), + ) .arg( Arg::with_name("icon") .short("i") @@ -41,24 +48,35 @@ fn main() { .get_matches(); let uevt_file = matches.value_of("uevent").unwrap(); let icon = matches.value_of("icon").unwrap(); - let threshold = matches - .value_of("threshold") - .unwrap() - .parse::() - .unwrap(); + let threshold = matches.value_of("alert").unwrap().parse::().unwrap(); let step = matches .value_of("notification-step") .unwrap() .parse::() .unwrap(); + let timeout = matches.value_of("timeout").unwrap().parse::().unwrap(); // periodic task to compare battery level with configuration - watch(&uevt_file, threshold, step, icon); + let notify = |cap| send_notification(cap, icon, timeout * 1000); + watch(&uevt_file, threshold, step, notify); } -// Checks the current battery percentage, alerts the user if the battery -// discharges and is below the configured threshold -fn watch(uevt_file: &str, threshold: i8, step: i8, icon: &str) { +// Puts a notification to the D-Bus +fn send_notification(cap: i8, icon: &str, timeout: i32) { + Notification::new() + .summary(&format!("Battery {}%", cap)) + .body("Charge your battery soon to avoid shutdown") + .icon(icon) + .urgency(Urgency::Critical) + .timeout(timeout) + .show() + .unwrap(); +} + +// Checks the current battery percentage. Calls the notification function +// periodically in the configured `step` interval if the battery discharges and +// is below the configured threshold +fn watch(uevt_file: &str, threshold: i8, step: i8, notify: F) { let ticker = tick(Duration::from_millis(5000)); let mut alert_threshold = threshold; loop { @@ -66,13 +84,7 @@ fn watch(uevt_file: &str, threshold: i8, step: i8, icon: &str) { recv(ticker) -> _ => { let (cap, status) = extract_status(&uevt_file); if status.to_lowercase() == "discharging" && cap <= alert_threshold { - Notification::new() - .summary(&format!("Battery {}%", cap)) - .body("Charge your battery soon to avoid shutdown") - .icon(icon) - .urgency(Urgency::Critical) - .show() - .unwrap(); + notify(cap); alert_threshold = cap - step; } else if status.to_lowercase() == "charging" {