diff options
author | Zhenhui Xie <xiezh0831@yahoo.co.jp> | 2020-07-08 06:45:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-07 18:45:32 -0400 |
commit | ec76fafff08933f6f31fb99ea974bdb5ae97a0af (patch) | |
tree | bb2c822cdf291635f03d27677c419488ecf77f53 /src | |
parent | 0f52b7b12e8c1a2060aa873a68032937dfa2c044 (diff) |
feat: refactor modules to use format strings (#1374)
Diffstat (limited to 'src')
93 files changed, 2821 insertions, 1818 deletions
diff --git a/src/config.rs b/src/config.rs index fe60edad8..be55af0f8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -235,37 +235,84 @@ impl StarshipConfig { /// Get the subset of the table for a module by its name pub fn get_module_config(&self, module_name: &str) -> Option<&Value> { - let module_config = self.config.as_ref()?.as_table()?.get(module_name); + let module_config = self.get_config(&[module_name]); if module_config.is_some() { log::debug!( "Config found for \"{}\": \n{:?}", &module_name, &module_config ); - } else { - log::trace!("No config found for \"{}\"", &module_name); } module_config } + /// Get the value of the config in a specific path + pub fn get_config(&self, path: &[&str]) -> Option<&Value> { + let mut prev_table = self.config.as_ref()?.as_table()?; + + assert_ne!( + path.len(), + 0, + "Starship::get_config called with an empty path" + ); + + let (table_options, _) = path.split_at(path.len() - 1); + + // Assumes all keys except the last in path has a table + for option in table_options { + match prev_table.get(*option) { + Some(value) => match value.as_table() { + Some(value) => { + prev_table = value; + } + None => { + log::trace!( + "No config found for \"{}\": \"{}\" is not a table", + path.join("."), + &option + ); + return None; + } + }, + None => { + log::trace!( + "No config found for \"{}\": Option \"{}\" not found", + path.join("."), + &option + ); + return None; + } + } + } + + let last_option = path.last().unwrap(); + let value = prev_table.get(*last_option); + if value.is_none() { + log::trace!( + "No config found for \"{}\": Option \"{}\" not found", + path.join("."), + &last_option + ); + }; + value + } + /// Get the subset of the table for a custom module by its name pub fn get_custom_module_config(&self, module_name: &str) -> Option<&Value> { - let module_config = self.get_custom_modules()?.get(module_name); + let module_config = self.get_config(&["custom", module_name]); if module_config.is_some() { log::debug!( "Custom config found for \"{}\": \n{:?}", &module_name, &module_config ); - } else { - log::trace!("No custom config found for \"{}\"", &module_name); } module_config } /// Get the table of all the registered custom modules, if any pub fn get_custom_modules(&self) -> Option<&toml::value::Table> { - self.config.as_ref()?.as_table()?.get("custom")?.as_table() + self.get_config(&["custom"])?.as_table() } pub fn get_root_config(&self) -> StarshipRootConfig { @@ -277,75 +324,6 @@ impl StarshipConfig { } } -#[derive(Clone)] -pub struct SegmentConfig<'a> { - pub value: &'a str, - pub style: Option<Style>, -} - -impl<'a> ModuleConfig<'a> for SegmentConfig<'a> { - fn from_config(config: &'a Value) -> Option<Self> { - match config { - Value::String(ref config_str) => Some(Self { - value: config_str, - style: None, - }), - Value::Table(ref config_table) => Some(Self { - value: config_table.get("value")?.as_str()?, - style: config_table.get("style").and_then(<Style>::from_config), - }), - _ => None, - } - } - - fn load_config(&self, config: &'a Value) -> Self { - let mut new_config = self.clone(); - match config { - Value::String(ref config_str) => { - new_config.value = config_str; - } - Value::Table(ref config_table) => { - if let Some(Value::String(value)) = config_table.get("value") { - new_config.value = value; - }; - if let Some(style) = config_table.get("style") { - new_config.style = <Style>::from_config(style); - }; - } - _ => {} - }; - new_config - } -} - -impl<'a> SegmentConfig<'a> { - pub fn new(value: &'a str) -> Self { - Self { value, style: None } - } - - /// Immutably set value - pub fn with_value(&self, value: &'a str) -> Self { - Self { - value, - style: self.style, - } - } - - /// Immutably set style - pub fn with_style(&self, style: Option<Style>) -> Self { - Self { - value: self.value, - style, - } - } -} - -impl Default for SegmentConfig<'static> { - fn default() -> Self { - Self::new("") - } -} - /** Parse a style string which represents an ansi style. Valid tokens in the style string include the following: - 'fg:<color>' (specifies that the color read should be a foreground color) diff --git a/src/configs/aws.rs b/src/configs/aws.rs index 228558a24..89a9d871d 100644 --- a/src/configs/aws.rs +++ b/src/configs/aws.rs @@ -1,48 +1,24 @@ -use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; -use std::collections::HashMap; - -use ansi_term::{Color, Style}; +use crate::config::{ModuleConfig, RootModuleConfig}; use starship_module_config_derive::ModuleConfig; - -#[derive(Clone, PartialEq)] -pub enum AwsItems { - All, - Region, - Profile, -} +use std::collections::HashMap; #[derive(Clone, ModuleConfig)] pub struct AwsConfig<'a> { - pub symbol: SegmentConfig<'a>, - pub profile: SegmentConfig<'a>, - pub region: SegmentConfig<'a>, - pub style: Style, + pub format: &'a str, + pub symbol: &'a str, + pub style: &'a str, pub disabled: bool, - pub displayed_items: AwsItems, pub region_aliases: HashMap<String, &'a str>, } impl<'a> RootModuleConfig<'a> for AwsConfig<'a> { fn new() -> Self { AwsConfig { - symbol: SegmentConfig::new("☁️ "), - profile: SegmentConfig::default(), - region: SegmentConfig::default(), - style: Color::Yellow.bold(), + format: "on [$symbol$profile(\\($region\\))]($style) ", + symbol: "☁️ ", + style: "bold yellow", disabled: false, - displayed_items: AwsItems::All, region_aliases: HashMap::new(), } } } - -impl<'a> ModuleConfig<'a> for AwsItems { - fn from_config(config: &toml::Value) -> Option<Self> { - match config.as_str()? { - "all" => Some(AwsItems::All), - "region" => Some(AwsItems::Region), - "profile" => Some(AwsItems::Profile), - _ => None, - } - } -} diff --git a/src/configs/battery.rs b/src/configs/battery.rs index d86227d79..ec7287705 100644 --- a/src/configs/battery.rs +++ b/src/configs/battery.rs @@ -1,40 +1,39 @@ -use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; +use crate::config::{ModuleConfig, RootModuleConfig}; -use ansi_term::{Color, Style}; use starship_module_config_derive::ModuleConfig; #[derive(Clone, ModuleConfig)] pub struct BatteryConfig<'a> { - 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 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 display: Vec<BatteryDisplayConfig<'a>>, pub disabled: bool, - pub percentage: SegmentConfig<'a>, + pub format: &'a str, } impl<'a> RootModuleConfig<'a> for BatteryConfig<'a> { fn new() -> Self { BatteryConfig { - full_symbol: SegmentConfig::new("•"), - charging_symbol: SegmentConfig::new("↑"), - discharging_symbol: SegmentConfig::new("↓"), + full_symbol: "•", + charging_symbol: "↑", + discharging_symbol: "↓", unknown_symbol: None, empty_symbol: None, + format: "[$symbol$percentage]($style) ", display: vec![BatteryDisplayConfig { threshold: 10, - style: Color::Red.bold(), + style: "red bold", }], disabled: false, - percentage: SegmentConfig::default(), } } } #[derive(Clone, ModuleConfig)] -pub struct BatteryDisplayConfig { +pub struct BatteryDisplayConfig<'a> { pub threshold: i64, - pub style: Style, + pub style: &'a str, } diff --git a/src/configs/character.rs b/src/configs/character.rs index 92f977ebb..7486890e7 100644 --- a/src/configs/character.rs +++ b/src/configs/character.rs @@ -1,28 +1,23 @@ -use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; +use crate::config::{ModuleConfig, RootModuleConfig}; -use ansi_term::{Color, Style}; use starship_module_config_derive::ModuleConfig; #[derive(Clone, ModuleConfig)] pub struct CharacterConfig<'a> { - pub symbol: SegmentConfig<'a>, - pub error_symbol: SegmentConfig<'a>, - pub vicmd_symbol: SegmentConfig<'a>, - pub use_symbol_for_status: bool, - pub style_success: Style, - pub style_failure: Style, + pub format: &'a str, + pub success_symbol: &'a str, + pub error_symbol: &'a str, + pub vicmd_symbol: &'a str, pub disabled: bool, } impl<'a> RootModuleConfig<'a> for CharacterConfig<'a> { fn new() -> Self { CharacterConfig { - symbol: SegmentConfig::new("❯"), - error_symbol: SegmentConfig::new("✖ |