refactor: Rewrite battery module to use module config (#454)

This commit is contained in:
Zhenhui Xie 2019-10-02 13:55:17 +08:00 committed by Matan Kushner
parent 9fc5a43355
commit f14392b5ea
2 changed files with 35 additions and 16 deletions

View File

@ -1,25 +1,35 @@
use crate::config::{ModuleConfig, RootModuleConfig}; use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
use ansi_term::{Color, Style}; use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig; use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)] #[derive(Clone, ModuleConfig)]
pub struct BatteryConfig<'a> { pub struct BatteryConfig<'a> {
pub full_symbol: &'a str, pub full_symbol: SegmentConfig<'a>,
pub charging_symbol: &'a str, pub charging_symbol: SegmentConfig<'a>,
pub discharging_symbol: &'a str, pub discharging_symbol: SegmentConfig<'a>,
pub unknown_symbol: Option<&'a str>, pub unknown_symbol: Option<SegmentConfig<'a>>,
pub empty_symbol: Option<&'a str>, pub empty_symbol: Option<SegmentConfig<'a>>,
pub display: Vec<BatteryDisplayConfig>, pub display: Vec<BatteryDisplayConfig>,
pub disabled: bool, pub disabled: bool,
pub percentage: SegmentConfig<'a>,
} }
impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> { impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> {
fn new() -> Self { fn new() -> Self {
BatteryConfig { BatteryConfig {
full_symbol: "", full_symbol: SegmentConfig {
charging_symbol: "", value: "",
discharging_symbol: "", style: None,
},
charging_symbol: SegmentConfig {
value: "",
style: None,
},
discharging_symbol: SegmentConfig {
value: "",
style: None,
},
unknown_symbol: None, unknown_symbol: None,
empty_symbol: None, empty_symbol: None,
display: vec![BatteryDisplayConfig { display: vec![BatteryDisplayConfig {
@ -27,6 +37,10 @@ impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> {
style: Color::Red.bold(), style: Color::Red.bold(),
}], }],
disabled: false, disabled: false,
percentage: SegmentConfig {
value: "",
style: None,
},
} }
} }
} }

View File

@ -16,7 +16,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let BatteryStatus { state, percentage } = battery_status; let BatteryStatus { state, percentage } = battery_status;
let mut module = context.new_module("battery"); 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` // Parse config under `display`
let display_styles = &battery_config.display; let display_styles = &battery_config.display;
@ -31,23 +31,23 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
match state { match state {
battery::State::Full => { battery::State::Full => {
module.new_segment("full_symbol", battery_config.full_symbol); module.create_segment("full_symbol", &battery_config.full_symbol);
} }
battery::State::Charging => { battery::State::Charging => {
module.new_segment("charging_symbol", battery_config.charging_symbol); module.create_segment("charging_symbol", &battery_config.charging_symbol);
} }
battery::State::Discharging => { battery::State::Discharging => {
module.new_segment("discharging_symbol", battery_config.discharging_symbol); module.create_segment("discharging_symbol", &battery_config.discharging_symbol);
} }
battery::State::Unknown => { battery::State::Unknown => {
log::debug!("Unknown detected"); log::debug!("Unknown detected");
if let Some(unknown_symbol) = battery_config.unknown_symbol { 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 => { battery::State::Empty => {
if let Some(empty_symbol) = battery_config.empty_symbol { 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 // Round the percentage to a whole number
percent_string.push(percentage.round().to_string()); percent_string.push(percentage.round().to_string());
percent_string.push(percentage_char.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) Some(module)
} else { } else {