about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs17
-rw-r--r--src/module.rs1
-rw-r--r--src/module/battery.rs46
-rw-r--r--src/module/time.rs4
4 files changed, 62 insertions, 6 deletions
diff --git a/src/main.rs b/src/main.rs
index 76e7fbb..d7710c7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -345,7 +345,22 @@ impl Dock {
       let mut facilities = self.facilities.lock()?;
 
       for (module, framing) in &self.modules {
-        let text = module.lock()?.format(facilities.borrow_mut())?;
+        let text = {
+          let mut module = module.lock()?;
+
+          let text = module.format(facilities.borrow_mut())?;
+
+          /*   We do this after calling format(), rather than before, to give
+           * the module a chance to decide whether it's shy. Thus, for example,
+           * hot-plugging the only battery will make the Battery module appear
+           * or disappear on the very next redraw, which will likely exceed the
+           * user's expectation of slickness.
+           */
+          if !module.is_visible() { continue; }
+
+          text
+        };
+
         self.draw_text(&mut open_parcel, &text, framing.alignment)?;
       }
     }
diff --git a/src/module.rs b/src/module.rs
index 6074457..6d32c40 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -30,5 +30,6 @@ pub trait CreatableModule: Module + Sized {
 pub trait Module {
   fn format(&mut self, facilities: &mut FacilityMap) -> Result<String>;
   fn sleep_duration(&self) -> Option<Duration>;
+  fn is_visible(&self) -> bool;
 }
 
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
+  }
 }