From cda01deffa68ca08cbf16d0c5dfb7a792e8375ce Mon Sep 17 00:00:00 2001 From: Zhenhui Xie Date: Fri, 4 Oct 2019 20:42:33 +0800 Subject: [PATCH] refactor: Rewrite aws and character module to use module config (#459) --- starship/src/configs/aws.rs | 29 ++++++++++++++++++++++ starship/src/configs/character.rs | 38 +++++++++++++++++++++++++++++ starship/src/configs/mod.rs | 2 ++ starship/src/modules/aws.rs | 16 ++++++------- starship/src/modules/character.rs | 40 ++++++++++++------------------- 5 files changed, 91 insertions(+), 34 deletions(-) create mode 100644 starship/src/configs/aws.rs create mode 100644 starship/src/configs/character.rs diff --git a/starship/src/configs/aws.rs b/starship/src/configs/aws.rs new file mode 100644 index 000000000..96ed3cb5b --- /dev/null +++ b/starship/src/configs/aws.rs @@ -0,0 +1,29 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct AwsConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub profile: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for AwsConfig<'a> { + fn new() -> Self { + AwsConfig { + symbol: SegmentConfig { + value: "☁️ ", + style: None, + }, + profile: SegmentConfig { + value: "", + style: None, + }, + style: Color::Yellow.bold(), + disabled: false, + } + } +} diff --git a/starship/src/configs/character.rs b/starship/src/configs/character.rs new file mode 100644 index 000000000..77a509cff --- /dev/null +++ b/starship/src/configs/character.rs @@ -0,0 +1,38 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +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 disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for CharacterConfig<'a> { + fn new() -> Self { + CharacterConfig { + symbol: SegmentConfig { + value: "❯", + style: None, + }, + error_symbol: SegmentConfig { + value: "✖", + style: None, + }, + vicmd_symbol: SegmentConfig { + value: "❮", + style: None, + }, + use_symbol_for_status: false, + style_success: Color::Green.bold(), + style_failure: Color::Red.bold(), + disabled: false, + } + } +} diff --git a/starship/src/configs/mod.rs b/starship/src/configs/mod.rs index b20fd7c98..f557345e4 100644 --- a/starship/src/configs/mod.rs +++ b/starship/src/configs/mod.rs @@ -1,4 +1,6 @@ +pub mod aws; pub mod battery; +pub mod character; pub mod dotnet; pub mod rust; diff --git a/starship/src/modules/aws.rs b/starship/src/modules/aws.rs index 65ca05142..fb8fabfa7 100644 --- a/starship/src/modules/aws.rs +++ b/starship/src/modules/aws.rs @@ -1,11 +1,11 @@ use std::env; -use ansi_term::Color; - use super::{Context, Module}; +use crate::config::RootModuleConfig; +use crate::configs::aws::AwsConfig; + pub fn module<'a>(context: &'a Context) -> Option> { - const AWS_CHAR: &str = "☁️ "; const AWS_PREFIX: &str = "on "; let aws_profile = env::var("AWS_PROFILE").ok()?; @@ -14,16 +14,14 @@ pub fn module<'a>(context: &'a Context) -> Option> { } let mut module = context.new_module("aws"); + let config: AwsConfig = AwsConfig::try_load(module.config); - let module_style = module - .config_value_style("style") - .unwrap_or_else(|| Color::Yellow.bold()); - module.set_style(module_style); + module.set_style(config.style); module.get_prefix().set_value(AWS_PREFIX); - module.new_segment("symbol", AWS_CHAR); - module.new_segment("profile", &aws_profile); + module.create_segment("symbol", &config.symbol); + module.create_segment("profile", &config.profile.with_value(&aws_profile)); Some(module) } diff --git a/starship/src/modules/character.rs b/starship/src/modules/character.rs index 9385e53c9..4925147cb 100644 --- a/starship/src/modules/character.rs +++ b/starship/src/modules/character.rs @@ -1,5 +1,7 @@ use super::{Context, Module}; -use ansi_term::Color; + +use crate::config::RootModuleConfig; +use crate::configs::character::CharacterConfig; /// Creates a module for the prompt character /// @@ -10,9 +12,6 @@ use ansi_term::Color; /// - If the exit-code was anything else, the arrow will be formatted with /// `style_failure` (red by default) pub fn module<'a>(context: &'a Context) -> Option> { - const SUCCESS_CHAR: &str = "❯"; - const FAILURE_CHAR: &str = "✖"; - const VICMD_CHAR: &str = "❮"; enum ShellEditMode { Normal, Insert, @@ -21,19 +20,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { // TODO: extend config to more modes let mut module = context.new_module("character"); + let config: CharacterConfig = CharacterConfig::try_load(module.config); module.get_prefix().set_value(""); - let style_success = module - .config_value_style("style_success") - .unwrap_or_else(|| Color::Green.bold()); - let style_failure = module - .config_value_style("style_failure") - .unwrap_or_else(|| Color::Red.bold()); - let arguments = &context.arguments; - let use_symbol = module - .config_value_bool("use_symbol_for_status") - .unwrap_or(false); let exit_success = arguments.value_of("status_code").unwrap_or("0") == "0"; let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default(); let keymap = arguments.value_of("keymap").unwrap_or("viins"); @@ -48,21 +38,21 @@ pub fn module<'a>(context: &'a Context) -> Option> { _ => ASSUMED_MODE, }; - /* If an error symbol is set in the config, use symbols to indicate - success/failure, in addition to color */ - let symbol = if use_symbol && !exit_success { - module.new_segment("error_symbol", FAILURE_CHAR) + if exit_success { + module.set_style(config.style_success); } else { - match mode { - ShellEditMode::Normal => module.new_segment("vicmd_symbol", VICMD_CHAR), - ShellEditMode::Insert => module.new_segment("symbol", SUCCESS_CHAR), - } + module.set_style(config.style_failure); }; - if exit_success { - symbol.set_style(style_success); + /* If an error symbol is set in the config, use symbols to indicate + success/failure, in addition to color */ + if config.use_symbol_for_status && !exit_success { + module.create_segment("error_symbol", &config.error_symbol) } else { - symbol.set_style(style_failure); + match mode { + ShellEditMode::Normal => module.create_segment("vicmd_symbol", &config.vicmd_symbol), + ShellEditMode::Insert => module.create_segment("symbol", &config.symbol), + } }; Some(module)