diff options
author | Zhenhui Xie <xiezh0831@126.com> | 2019-10-02 13:55:17 +0800 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-10-02 14:55:16 +0900 |
commit | f14392b5eac10cc7aacd7beb81a982955c73c6fc (patch) | |
tree | 821e448a688b9f415e7f3819f495a27019c50e9e | |
parent | 9fc5a43355bb89e06db99322269b7cc09d7a36e9 (diff) |
refactor: Rewrite battery module to use module config (#454)
-rw-r--r-- | starship/src/configs/battery.rs | 32 | ||||
-rw-r--r-- | starship/src/modules/battery.rs | 19 |
2 files changed, 35 insertions, 16 deletions
diff --git a/starship/src/configs/battery.rs b/starship/src/configs/battery.rs index cb0e69c4d..e60b95e2f 100644 --- a/starship/src/configs/battery.rs +++ b/starship/src/configs/battery.rs @@ -1,25 +1,35 @@ -use crate::config::{ModuleConfig, RootModuleConfig}; +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; use ansi_term::{Color, Style}; use starship_module_config_derive::ModuleConfig; #[derive(Clone, ModuleConfig)] pub struct BatteryConfig<'a> { - pub full_symbol: &'a str, - pub charging_symbol: &'a str, - pub discharging_symbol: &'a str, - pub unknown_symbol: Option<&'a str>, - pub empty_symbol: Option<&'a str>, + pub full_symbol: SegmentConfig<'a>, + pub charging_symbol: SegmentConfig<'a>, + pub discharging_symbol: SegmentConfig<'a>, + pub unknown_symbol: Option<SegmentConfig<'a>>, + pub empty_symbol: Option<SegmentConfig<'a>>, pub display: Vec<BatteryDisplayConfig>, pub disabled: bool, + pub percentage: SegmentConfig<'a>, } impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> { fn new() -> Self { BatteryConfig { - full_symbol: "•", - charging_symbol: "↑", - discharging_symbol: "↓", + full_symbol: SegmentConfig { + value: "•", + style: None, + }, + charging_symbol: SegmentConfig { + value: "↑", + style: None, + }, + discharging_symbol: SegmentConfig { + value: "↓", + style: None, + }, unknown_symbol: None, empty_symbol: None, display: vec![BatteryDisplayConfig { @@ -27,6 +37,10 @@ impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> { style: Color::Red.bold(), }], disabled: false, + percentage: SegmentConfig { + value: "", + style: None, + }, } } } diff --git a/starship/src/modules/battery.rs b/starship/src/modules/battery.rs index 3bd19366b..e67280297 100644 --- a/starship/src/modules/battery.rs +++ b/starship/src/modules/battery.rs @@ -16,7 +16,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { let BatteryStatus { state, percentage } = battery_status; let mut module = context.new_module("battery"); - let battery_config = BatteryConfig::try_load(module.config); + let battery_config: BatteryConfig = BatteryConfig::try_load(module.config); // Parse config under `display` let display_styles = &battery_config.display; @@ -31,23 +31,23 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { match state { battery::State::Full => { - module.new_segment("full_symbol", battery_config.full_symbol); + module.create_segment("full_symbol", &battery_config.full_symbol); } battery::State::Charging => { - module.new_segment("charging_symbol", battery_config.charging_symbol); + module.create_segment("charging_symbol", &battery_config.charging_symbol); } battery::State::Discharging => { - module.new_segment("discharging_symbol", battery_config.discharging_symbol); + module.create_segment("discharging_symbol", &battery_config.discharging_symbol); } battery::State::Unknown => { log::debug!("Unknown detected"); if let Some(unknown_symbol) = battery_config.unknown_symbol { - module.new_segment("unknown_symbol", unknown_symbol); + module.create_segment("unknown_symbol", &unknown_symbol); } } battery::State::Empty => { if let Some(empty_symbol) = battery_config.empty_symbol { - module.new_segment("empty_symbol", empty_symbol); + module.create_segment("empty_symbol", &empty_symbol); } } _ => { @@ -60,7 +60,12 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { // Round the percentage to a whole number percent_string.push(percentage.round().to_string()); percent_string.push(percentage_char.to_string()); - module.new_segment("percentage", percent_string.join("").as_ref()); + module.create_segment( + "percentage", + &battery_config + .percentage + .with_value(percent_string.join("").as_ref()), + ); Some(module) } else { |