From f14392b5eac10cc7aacd7beb81a982955c73c6fc Mon Sep 17 00:00:00 2001 From: Zhenhui Xie Date: Wed, 2 Oct 2019 13:55:17 +0800 Subject: [PATCH] refactor: Rewrite battery module to use module config (#454) --- starship/src/configs/battery.rs | 32 +++++++++++++++++++++++--------- 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 cb0e69c4..e60b95e2 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>, + pub empty_symbol: Option>, pub display: Vec, 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 3bd19366..e6728029 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> { 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> { 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> { // 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 {