about summary refs log tree commit diff
path: root/src/module
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-21 18:26:52 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-21 18:32:36 -0700
commit199e896d9625c8f5da46c0ff3e45dce04ea23f05 (patch)
tree5be8404893b52eee44d7b96f2219b8b218c9a56e /src/module
parent3f9e377d790da460919bd9f6ab82402424744c96 (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')
-rw-r--r--src/module/battery.rs46
-rw-r--r--src/module/time.rs4
2 files changed, 45 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")
+  }
 }
 
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
+  }
 }