mirror of
https://github.com/Llewellynvdm/starship.git
synced 2025-01-28 17:48:25 +00:00
feat(battery): Use best match instead of first match for battery.display
threshold (#6442)
Previously the selection code simply filtered for the first style found within the config with a threshold larger than the current battery percentage. If the config defined multiple display thresholds that were above the battery percentage it would only select the first one that was encountered rather than the one that was closest to the current battery percentage. This commit also adds a test to ensure this property is held true. The test accomplishes this by comparing the parsing of a config with two `battery.display` entries, one with a threshold at 100% & the other at 60%, with a mock battery set at 50% charge. This config is parsed with the 100% threshold defined above the 60% threshold, and then again with the 60% threshold defined above the 100% threshold. To ensure the entries are parsed parsed correctly each entry is given a different style, so the expected value that is compared against has the same style as the 60% threshold entry for both test cases. * Update src/modules/battery.rs Use more concise chain of functions from Iterator Co-authored-by: David Knaack <davidkna@users.noreply.github.com> --------- Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
This commit is contained in:
parent
e21775c694
commit
2690a329cf
@ -15,12 +15,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let config: BatteryConfig = BatteryConfig::try_load(module.config);
|
||||
|
||||
// Parse config under `display`.
|
||||
// Select the first style that match the threshold,
|
||||
// if all thresholds are lower do not display battery module.
|
||||
// Select the style that is most minimally greater than the current battery percentage.
|
||||
// If no such style exists do not display battery module.
|
||||
let display_style = config
|
||||
.display
|
||||
.iter()
|
||||
.find(|display_style| percentage <= display_style.threshold as f32)?;
|
||||
.filter(|display_style| percentage <= display_style.threshold as f32)
|
||||
.min_by_key(|display_style| display_style.threshold)?;
|
||||
|
||||
// Parse the format string and build the module
|
||||
match StringFormatter::new(config.format) {
|
||||
@ -413,4 +414,47 @@ mod tests {
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn battery_expected_style() {
|
||||
let mut mock = MockBatteryInfoProvider::new();
|
||||
|
||||
mock.expect_get_battery_info().times(2).returning(|| {
|
||||
Some(BatteryInfo {
|
||||
energy: 50.0,
|
||||
energy_full: 100.0,
|
||||
state: battery::State::Discharging,
|
||||
})
|
||||
});
|
||||
|
||||
// Larger threshold first
|
||||
let actual = ModuleRenderer::new("battery")
|
||||
.config(toml::toml! {
|
||||
[[battery.display]]
|
||||
threshold = 100
|
||||
style = "red"
|
||||
[[battery.display]]
|
||||
threshold = 60
|
||||
style = "green bold"
|
||||
})
|
||||
.battery_info_provider(&mock)
|
||||
.collect();
|
||||
let expected = Some(format!("{} ", Color::Green.bold().paint(" 50%")));
|
||||
assert_eq!(expected, actual);
|
||||
|
||||
// Smaller threshold first
|
||||
let actual = ModuleRenderer::new("battery")
|
||||
.config(toml::toml! {
|
||||
[[battery.display]]
|
||||
threshold = 60
|
||||
style = "green bold"
|
||||
[[battery.display]]
|
||||
threshold = 100
|
||||
style = "red"
|
||||
})
|
||||
.battery_info_provider(&mock)
|
||||
.collect();
|
||||
let expected = Some(format!("{} ", Color::Green.bold().paint(" 50%")));
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user