From 1a06e7c260efea4b45060f664e4280e5d1d69580 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 16 Oct 2024 17:10:59 +0200 Subject: add battery full notification --- .gitignore | 1 + main | Bin 3887512 -> 0 bytes main.rs | 30 ++++++++++++++++++------------ 3 files changed, 19 insertions(+), 12 deletions(-) create mode 100644 .gitignore delete mode 100755 main diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +main diff --git a/main b/main deleted file mode 100755 index 540a3ed..0000000 Binary files a/main and /dev/null differ diff --git a/main.rs b/main.rs index d6bf3d9..b2f73ad 100644 --- a/main.rs +++ b/main.rs @@ -2,6 +2,8 @@ use std::{ collections::HashMap, env, fs::File, io::Read, process::Command, thread::sleep, time::Duration, }; +static SLEEP: u64 = 1; + fn main() { let mut args = env::args(); @@ -18,13 +20,6 @@ fn main() { let cap_path = format!("{bat_path}/capacity"); let status_path = format!("{bat_path}/status"); - // 2: Sleep - let sleep_secs = args - .next() - .expect("Invalid sleep duration") - .parse::() - .expect("Invalid sleep value"); - let mut warnings: HashMap = HashMap::new(); for arg in args { let (lvl, value) = arg.split_at(1); @@ -40,18 +35,25 @@ fn main() { let mut cap_cache = String::new(); loop { + sleep(Duration::from_secs(SLEEP)); let cur_cap = read_file(&cap_path); if cur_cap != cap_cache { cap_cache.clone_from(&cur_cap); - if &read_file(&status_path) == "Charging" { continue; }; + let bat_status = &read_file(&status_path); + if bat_status == "Charging" { + continue; + }; + if cur_cap == "100" && (bat_status == "Discharging" || bat_status == "Not charging") { + notify("n", None); + continue; + } let val = cur_cap .parse::() .expect("Couldn't parse capacity value"); if let Some(lvl) = warnings.get(&val) { - notify(lvl, val); + notify(lvl, Some(val)); }; } - sleep(Duration::from_secs(sleep_secs)); } } @@ -62,14 +64,18 @@ fn read_file(path: &str) -> String { buf.trim().to_string() } -fn notify(lvl: &str, remaining: u8) { +fn notify(lvl: &str, remaining: Option) { let urgency = match lvl { "l" => "low", "n" => "normal", "c" => "critical", _ => unreachable!(), }; - let notif = format!("Remaining battery capacity: {remaining}"); + let notif = if let Some(remaining) = remaining { + &format!("Remaining battery capacity: {remaining}") + } else { + "Battery is full" + }; Command::new("notify-send") .arg("--app-name=esbnd") .arg("--category=device") -- cgit v1.2.3