diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | main | bin | 3887512 -> 0 bytes | |||
-rw-r--r-- | main.rs | 30 |
3 files changed, 19 insertions, 12 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/.gitignore | |||
@@ -0,0 +1 @@ | |||
main | |||
Binary files differ | |||
@@ -2,6 +2,8 @@ use std::{ | |||
2 | collections::HashMap, env, fs::File, io::Read, process::Command, thread::sleep, time::Duration, | 2 | collections::HashMap, env, fs::File, io::Read, process::Command, thread::sleep, time::Duration, |
3 | }; | 3 | }; |
4 | 4 | ||
5 | static SLEEP: u64 = 1; | ||
6 | |||
5 | fn main() { | 7 | fn main() { |
6 | let mut args = env::args(); | 8 | let mut args = env::args(); |
7 | 9 | ||
@@ -18,13 +20,6 @@ fn main() { | |||
18 | let cap_path = format!("{bat_path}/capacity"); | 20 | let cap_path = format!("{bat_path}/capacity"); |
19 | let status_path = format!("{bat_path}/status"); | 21 | let status_path = format!("{bat_path}/status"); |
20 | 22 | ||
21 | // 2: Sleep | ||
22 | let sleep_secs = args | ||
23 | .next() | ||
24 | .expect("Invalid sleep duration") | ||
25 | .parse::<u64>() | ||
26 | .expect("Invalid sleep value"); | ||
27 | |||
28 | let mut warnings: HashMap<u8, String> = HashMap::new(); | 23 | let mut warnings: HashMap<u8, String> = HashMap::new(); |
29 | for arg in args { | 24 | for arg in args { |
30 | let (lvl, value) = arg.split_at(1); | 25 | let (lvl, value) = arg.split_at(1); |
@@ -40,18 +35,25 @@ fn main() { | |||
40 | 35 | ||
41 | let mut cap_cache = String::new(); | 36 | let mut cap_cache = String::new(); |
42 | loop { | 37 | loop { |
38 | sleep(Duration::from_secs(SLEEP)); | ||
43 | let cur_cap = read_file(&cap_path); | 39 | let cur_cap = read_file(&cap_path); |
44 | if cur_cap != cap_cache { | 40 | if cur_cap != cap_cache { |
45 | cap_cache.clone_from(&cur_cap); | 41 | cap_cache.clone_from(&cur_cap); |
46 | if &read_file(&status_path) == "Charging" { continue; }; | 42 | let bat_status = &read_file(&status_path); |
43 | if bat_status == "Charging" { | ||
44 | continue; | ||
45 | }; | ||
46 | if cur_cap == "100" && (bat_status == "Discharging" || bat_status == "Not charging") { | ||
47 | notify("n", None); | ||
48 | continue; | ||
49 | } | ||
47 | let val = cur_cap | 50 | let val = cur_cap |
48 | .parse::<u8>() | 51 | .parse::<u8>() |
49 | .expect("Couldn't parse capacity value"); | 52 | .expect("Couldn't parse capacity value"); |
50 | if let Some(lvl) = warnings.get(&val) { | 53 | if let Some(lvl) = warnings.get(&val) { |
51 | notify(lvl, val); | 54 | notify(lvl, Some(val)); |
52 | }; | 55 | }; |
53 | } | 56 | } |
54 | sleep(Duration::from_secs(sleep_secs)); | ||
55 | } | 57 | } |
56 | } | 58 | } |
57 | 59 | ||
@@ -62,14 +64,18 @@ fn read_file(path: &str) -> String { | |||
62 | buf.trim().to_string() | 64 | buf.trim().to_string() |
63 | } | 65 | } |
64 | 66 | ||
65 | fn notify(lvl: &str, remaining: u8) { | 67 | fn notify(lvl: &str, remaining: Option<u8>) { |
66 | let urgency = match lvl { | 68 | let urgency = match lvl { |
67 | "l" => "low", | 69 | "l" => "low", |
68 | "n" => "normal", | 70 | "n" => "normal", |
69 | "c" => "critical", | 71 | "c" => "critical", |
70 | _ => unreachable!(), | 72 | _ => unreachable!(), |
71 | }; | 73 | }; |
72 | let notif = format!("Remaining battery capacity: {remaining}"); | 74 | let notif = if let Some(remaining) = remaining { |
75 | &format!("Remaining battery capacity: {remaining}") | ||
76 | } else { | ||
77 | "Battery is full" | ||
78 | }; | ||
73 | Command::new("notify-send") | 79 | Command::new("notify-send") |
74 | .arg("--app-name=esbnd") | 80 | .arg("--app-name=esbnd") |
75 | .arg("--category=device") | 81 | .arg("--category=device") |