Add notification timeout
This commit is contained in:
parent
24aa158ac2
commit
fadbd78055
2 changed files with 38 additions and 22 deletions
|
@ -18,7 +18,7 @@ Send [D-Bus](https://www.freedesktop.org/wiki/Software/dbus/) notifications when
|
||||||
|
|
||||||
## Usage
|
## 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
|
### 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% ... :
|
- 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:
|
- Use a custom icon:
|
||||||
```
|
```
|
||||||
|
|
52
src/main.rs
52
src/main.rs
|
@ -18,11 +18,11 @@ fn main() {
|
||||||
.help("Read the battery capacity from this uevent file."),
|
.help("Read the battery capacity from this uevent file."),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("threshold")
|
Arg::with_name("alert")
|
||||||
.short("t")
|
.short("a")
|
||||||
.long("threshold")
|
.long("alert")
|
||||||
.default_value("15")
|
.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(
|
||||||
Arg::with_name("notification-step")
|
Arg::with_name("notification-step")
|
||||||
|
@ -31,6 +31,13 @@ fn main() {
|
||||||
.default_value("3")
|
.default_value("3")
|
||||||
.help("Repeat notifications for every n percent the battery discharges"),
|
.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(
|
||||||
Arg::with_name("icon")
|
Arg::with_name("icon")
|
||||||
.short("i")
|
.short("i")
|
||||||
|
@ -41,24 +48,35 @@ fn main() {
|
||||||
.get_matches();
|
.get_matches();
|
||||||
let uevt_file = matches.value_of("uevent").unwrap();
|
let uevt_file = matches.value_of("uevent").unwrap();
|
||||||
let icon = matches.value_of("icon").unwrap();
|
let icon = matches.value_of("icon").unwrap();
|
||||||
let threshold = matches
|
let threshold = matches.value_of("alert").unwrap().parse::<i8>().unwrap();
|
||||||
.value_of("threshold")
|
|
||||||
.unwrap()
|
|
||||||
.parse::<i8>()
|
|
||||||
.unwrap();
|
|
||||||
let step = matches
|
let step = matches
|
||||||
.value_of("notification-step")
|
.value_of("notification-step")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.parse::<i8>()
|
.parse::<i8>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
let timeout = matches.value_of("timeout").unwrap().parse::<i32>().unwrap();
|
||||||
|
|
||||||
// periodic task to compare battery level with configuration
|
// 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
|
// Puts a notification to the D-Bus
|
||||||
// discharges and is below the configured threshold
|
fn send_notification(cap: i8, icon: &str, timeout: i32) {
|
||||||
fn watch(uevt_file: &str, threshold: i8, step: i8, icon: &str) {
|
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 ticker = tick(Duration::from_millis(5000));
|
||||||
let mut alert_threshold = threshold;
|
let mut alert_threshold = threshold;
|
||||||
loop {
|
loop {
|
||||||
|
@ -66,13 +84,7 @@ fn watch(uevt_file: &str, threshold: i8, step: i8, icon: &str) {
|
||||||
recv(ticker) -> _ => {
|
recv(ticker) -> _ => {
|
||||||
let (cap, status) = extract_status(&uevt_file);
|
let (cap, status) = extract_status(&uevt_file);
|
||||||
if status.to_lowercase() == "discharging" && cap <= alert_threshold {
|
if status.to_lowercase() == "discharging" && cap <= alert_threshold {
|
||||||
Notification::new()
|
notify(cap);
|
||||||
.summary(&format!("Battery {}%", cap))
|
|
||||||
.body("Charge your battery soon to avoid shutdown")
|
|
||||||
.icon(icon)
|
|
||||||
.urgency(Urgency::Critical)
|
|
||||||
.show()
|
|
||||||
.unwrap();
|
|
||||||
alert_threshold = cap - step;
|
alert_threshold = cap - step;
|
||||||
}
|
}
|
||||||
else if status.to_lowercase() == "charging" {
|
else if status.to_lowercase() == "charging" {
|
||||||
|
|
Reference in a new issue