diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-21 18:26:52 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-21 18:32:36 -0700 |
| commit | 199e896d9625c8f5da46c0ff3e45dce04ea23f05 (patch) | |
| tree | 5be8404893b52eee44d7b96f2219b8b218c9a56e /src/module/battery.rs | |
| parent | 3f9e377d790da460919bd9f6ab82402424744c96 (diff) | |
there's a concept of module visibility
so that, for example, the battery module can self-hide and self-show when batteries are added or removed. also, said module now displays a list of battery names Force-Push: yes Change-Id: I8696f1b7e914aad3b1a663af12d6a00d9861b28c
Diffstat (limited to 'src/module/battery.rs')
| -rw-r--r-- | src/module/battery.rs | 46 |
1 files changed, 41 insertions, 5 deletions
diff --git a/src/module/battery.rs b/src/module/battery.rs index 8df4d31..cac5eda 100644 --- a/src/module/battery.rs +++ b/src/module/battery.rs @@ -7,6 +7,7 @@ use std::time::Duration; pub struct BatteryModule { + is_visible: bool, } @@ -17,14 +18,14 @@ impl CreatableModule for BatteryModule { let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom( "Battery module can't access envsys facility."))?; + let mut is_visible = false; for name in envsys.device_iter() { - if name.starts_with("acpibat") { - println!("battery {}", name); + if Self::is_battery_device(name) { + is_visible = true; } } - Ok(BatteryModule { - }) + Ok(BatteryModule { is_visible }) } } @@ -33,11 +34,46 @@ impl Module for BatteryModule { let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom( "Battery module can't access envsys facility."))?; - Ok("battery".to_string()) + /* When there's more than one battery, we want to present them in an + * order which is both stable and predictable. Their device names are also + * user-visible via envstat(8), so lexicographic ordering will be fine for + * both needs. Anyway, to make that happen, we collect and sort the ones + * we're interested in in advance. + */ + let mut devices = Vec::new(); + for name in envsys.device_iter() { + if Self::is_battery_device(name) { + devices.push(name); + } + } + devices.sort(); + + self.is_visible = !devices.is_empty(); + + let mut text = String::new(); + for device in &devices { + if !text.is_empty() { + text.push_str(" "); + } + + text.push_str(device); + } + + Ok(text) } fn sleep_duration(&self) -> Option<Duration> { None } + + fn is_visible(&self) -> bool { + self.is_visible + } +} + +impl BatteryModule { + fn is_battery_device(name: &str) -> bool { + name.starts_with("acpibat") + } } |