From 7125d483fa347f08759a650266548d229c7a9461 Mon Sep 17 00:00:00 2001 From: Lucien Cartier-Tilet Date: Mon, 10 Aug 2020 20:16:25 +0200 Subject: [PATCH] Make levels safer It is now impossible to have any level set to something below 5% of the battery and above 50%. If a lower level is set higher than its higher level, the program will attempt to lower the level by 1% without going under 5%. --- src/battery_state.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/battery_state.rs b/src/battery_state.rs index 3651d88..d100e35 100644 --- a/src/battery_state.rs +++ b/src/battery_state.rs @@ -29,8 +29,8 @@ pub const DEFAULT_REFRESH: u64 = 5; impl BatteryState { pub fn new( low_level: f32, - very_low_level: f32, - critical_level: f32, + mut very_low_level: f32, + mut critical_level: f32, refresh_rate: u64, verbosity: VerbosityLevel, ) -> battery::Result { @@ -48,6 +48,14 @@ impl BatteryState { } }; + let low_level = low_level.min(50_f32).max(5_f32); + if very_low_level > low_level { + very_low_level = f32::max(low_level - 1_f32, 5_f32) + }; + if critical_level > very_low_level { + critical_level = f32::max(very_low_level - 1_f32, 5_f32) + }; + Ok(Self { manager, battery,