summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
author谢祯晖 <xiezh0831@126.com>2019-09-21 00:52:54 +0800
committerMatan Kushner <hello@matchai.me>2019-09-20 12:52:54 -0400
commit14fe2461380c0d8d04c26f87684952a1d67c4323 (patch)
tree90fb6cf3d26e42afebbfa2ba667c995876e1273f
parentc2875d99b678c547c15681e85a4a96d9d264989a (diff)
fix: Add display for unknown battery state (#316)
-rw-r--r--docs/config/README.md12
-rw-r--r--src/module.rs14
-rw-r--r--src/modules/battery.rs11
3 files changed, 37 insertions, 0 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index fb293b425..0ba0b48b0 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -119,6 +119,18 @@ The module is only visible when the device's battery is below 10%.
| `display` | [link](#battery-display) | Display threshold and style for the module. |
| `disabled` | `false` | Disables the `battery` module. |
+<details>
+<summary>There are also options for some uncommon battery states.</summary>
+
+| Variable | Description |
+|------------------|-----------------------------------------------------|
+| `unknown_symbol` | The symbol shown when the battery state is unknown. |
+| `empty_symbol` | The symbol shown when the battery state is empty. |
+
+Note: Battery indicator will be hidden if the status is `unknown` or `empty` unless you specify the option in the config.
+
+</details>
+
### Example
```toml
diff --git a/src/module.rs b/src/module.rs
index 624834861..2cda82a08 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -75,6 +75,20 @@ impl<'a> Module<'a> {
self.segments.last_mut().unwrap()
}
+ /// Should config exists, get a reference to a newly created segment in the module
+ pub fn new_segment_if_config_exists(&mut self, name: &str) -> Option<&mut Segment> {
+ // Use the provided value unless overwritten by config
+ if let Some(value) = self.config_value_str(name) {
+ let mut segment = Segment::new(name);
+ segment.set_style(self.style);
+ segment.set_value(value);
+ self.segments.push(segment);
+ Some(self.segments.last_mut().unwrap())
+ } else {
+ None
+ }
+ }
+
/// Whether a module has non-empty segments
pub fn is_empty(&self) -> bool {
self.segments.iter().all(|segment| segment.is_empty())
diff --git a/src/modules/battery.rs b/src/modules/battery.rs
index 881893940..3ecc651f3 100644
--- a/src/modules/battery.rs
+++ b/src/modules/battery.rs
@@ -45,6 +45,17 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
battery::State::Discharging => {
module.new_segment("discharging_symbol", BATTERY_DISCHARGING);
}
+ battery::State::Unknown => {
+ log::debug!("Unknown detected");
+ module.new_segment_if_config_exists("unknown_symbol")?;
+ }
+ battery::State::Empty => {
+ module.new_segment_if_config_exists("empty_symbol")?;
+ }
+ _ => {
+ log::debug!("Unhandled battery state `{}`", state);
+ return None;
+ }
_ => return None,
}