From 199e896d9625c8f5da46c0ff3e45dce04ea23f05 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sat, 21 Nov 2026 18:26:52 -0700 Subject: 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 --- src/module/battery.rs | 46 +++++++++++++++++++++++++++++++++++++++++----- src/module/time.rs | 4 ++++ 2 files changed, 45 insertions(+), 5 deletions(-) (limited to 'src/module') 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 { None } + + fn is_visible(&self) -> bool { + self.is_visible + } +} + +impl BatteryModule { + fn is_battery_device(name: &str) -> bool { + name.starts_with("acpibat") + } } diff --git a/src/module/time.rs b/src/module/time.rs index 777afdd..3e144cf 100644 --- a/src/module/time.rs +++ b/src/module/time.rs @@ -53,5 +53,9 @@ impl Module for TimeModule { Some(Duration::from_millis(1000)) } } + + fn is_visible(&self) -> bool { + true + } } -- cgit 1.4.1