Add notification timeout

This commit is contained in:
Felix Ortmann 2020-12-27 12:45:09 +01:00
parent 24aa158ac2
commit fadbd78055
2 changed files with 38 additions and 22 deletions

View file

@ -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:
```

View file

@ -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::<i8>()
.unwrap();
let threshold = matches.value_of("alert").unwrap().parse::<i8>().unwrap();
let step = matches
.value_of("notification-step")
.unwrap()
.parse::<i8>()
.unwrap();
let timeout = matches.value_of("timeout").unwrap().parse::<i32>().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<F: Fn(i8)>(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" {