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 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,
},
}
}
}

View File

@ -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 {