about summary refs log tree commit diff
path: root/src/module/battery.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/module/battery.rs')
-rw-r--r--src/module/battery.rs46
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")
+  }
 }