feat: show live battery charge by default

This commit is contained in:
Valdemar Lorenzen
2026-10-12 12:02:10 +02:00
parent 0d3fbc01f8
commit 8c6a050171
11 changed files with 746 additions and 41 deletions
+69
View File
@@ -1354,6 +1354,8 @@ pub struct Editor {
top_bar: ChromeLine,
bottom_bar: ChromeLine,
labels: BTreeMap<String, String>,
battery_charge: Option<u8>,
battery_label: String,
quit: bool,
capabilities: Vec<String>,
features: HashMap<String, Feature>,
@@ -1442,6 +1444,8 @@ impl Editor {
top_bar: ChromeLine::top(),
bottom_bar: ChromeLine::bottom(),
labels: BTreeMap::new(),
battery_charge: None,
battery_label: String::new(),
quit: false,
capabilities,
features: HashMap::new(),
@@ -3840,6 +3844,9 @@ impl Editor {
pub fn label(&self, name: &str) -> &str {
self.labels.get(name).map_or("", String::as_str)
}
pub fn battery_label(&self) -> &str {
&self.battery_label
}
pub fn path(&self) -> Option<&Path> {
self.active.path.as_deref()
}
@@ -3921,6 +3928,20 @@ impl Editor {
Ok(true)
}
pub(crate) fn set_battery_charge(&mut self, charge: Option<u8>) -> bool {
let charge = charge.filter(|charge| *charge <= 100);
if self.battery_charge == charge {
return false;
}
self.battery_charge = charge;
self.battery_label.clear();
if let Some(charge) = charge {
use std::fmt::Write as _;
write!(self.battery_label, "bat {charge}%").unwrap();
}
true
}
pub fn register(&mut self, feature: Feature) -> Result<(), String> {
self.install(feature, false)
}
@@ -17816,6 +17837,54 @@ mod tests {
assert_eq!(editor.text_revision(), 0);
}
#[test]
fn native_battery_cache_validates_changes_without_editing_or_replacing_labels() {
let mut editor = editor();
editor.handle(Input::Char('i'));
editor.handle(Input::Paste("document".into()));
editor.handle(Input::Escape);
editor.handle(Input::Char('v'));
let text = editor.context().text;
let selection = editor.selections();
let revision = editor.text_revision();
let history = (editor.active.undo.len(), editor.active.redo.len());
assert_eq!(editor.battery_label(), "");
assert!(!editor.set_battery_charge(None));
assert!(!editor.set_battery_charge(Some(101)));
assert!(editor.set_battery_charge(Some(27)));
assert_eq!(editor.battery_label(), "bat 27%");
assert!(!editor.set_battery_charge(Some(27)));
assert_eq!(editor.label("battery"), "");
editor.set_label("battery".into(), "custom".into()).unwrap();
assert!(editor.set_battery_charge(Some(0)));
assert_eq!(editor.battery_label(), "bat 0%");
assert_eq!(editor.label("battery"), "custom");
assert!(editor.set_battery_charge(Some(100)));
assert_eq!(editor.battery_label(), "bat 100%");
assert!(editor.set_battery_charge(Some(255)));
assert_eq!(editor.battery_label(), "");
assert!(!editor.set_battery_charge(None));
assert_eq!(editor.label("battery"), "custom");
assert_eq!(editor.context().text, text);
assert_eq!(editor.selections(), selection);
assert_eq!(editor.mode(), Mode::Visual);
assert_eq!(editor.text_revision(), revision);
assert_eq!(
(editor.active.undo.len(), editor.active.redo.len()),
history
);
assert!(editor.set_battery_charge(Some(28)));
editor.execute("tab.new");
assert_eq!(editor.battery_label(), "bat 28%");
editor.set_label("battery".into(), String::new()).unwrap();
assert_eq!(editor.battery_label(), "bat 28%");
assert_eq!(editor.label("battery"), "");
assert!(editor.set_battery_charge(None));
assert_eq!(editor.battery_label(), "");
}
#[test]
fn named_labels_sanitize_and_survive_tabs_until_independently_cleared() {
let mut editor = editor();