refactor: Rewrite aws and character module to use module config (#459)

This commit is contained in:
Zhenhui Xie 2019-10-04 20:42:33 +08:00 committed by Matan Kushner
parent 1d701729cf
commit cda01deffa
5 changed files with 91 additions and 34 deletions

View File

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

View File

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

View File

@ -1,4 +1,6 @@
pub mod aws;
pub mod battery; pub mod battery;
pub mod character;
pub mod dotnet; pub mod dotnet;
pub mod rust; pub mod rust;

View File

@ -1,11 +1,11 @@
use std::env; use std::env;
use ansi_term::Color;
use super::{Context, Module}; use super::{Context, Module};
use crate::config::RootModuleConfig;
use crate::configs::aws::AwsConfig;
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
const AWS_CHAR: &str = "☁️ ";
const AWS_PREFIX: &str = "on "; const AWS_PREFIX: &str = "on ";
let aws_profile = env::var("AWS_PROFILE").ok()?; let aws_profile = env::var("AWS_PROFILE").ok()?;
@ -14,16 +14,14 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
} }
let mut module = context.new_module("aws"); let mut module = context.new_module("aws");
let config: AwsConfig = AwsConfig::try_load(module.config);
let module_style = module module.set_style(config.style);
.config_value_style("style")
.unwrap_or_else(|| Color::Yellow.bold());
module.set_style(module_style);
module.get_prefix().set_value(AWS_PREFIX); module.get_prefix().set_value(AWS_PREFIX);
module.new_segment("symbol", AWS_CHAR); module.create_segment("symbol", &config.symbol);
module.new_segment("profile", &aws_profile); module.create_segment("profile", &config.profile.with_value(&aws_profile));
Some(module) Some(module)
} }

View File

@ -1,5 +1,7 @@
use super::{Context, Module}; use super::{Context, Module};
use ansi_term::Color;
use crate::config::RootModuleConfig;
use crate::configs::character::CharacterConfig;
/// Creates a module for the prompt character /// 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 /// - If the exit-code was anything else, the arrow will be formatted with
/// `style_failure` (red by default) /// `style_failure` (red by default)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
const SUCCESS_CHAR: &str = "";
const FAILURE_CHAR: &str = "";
const VICMD_CHAR: &str = "";
enum ShellEditMode { enum ShellEditMode {
Normal, Normal,
Insert, Insert,
@ -21,19 +20,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
// TODO: extend config to more modes // TODO: extend config to more modes
let mut module = context.new_module("character"); let mut module = context.new_module("character");
let config: CharacterConfig = CharacterConfig::try_load(module.config);
module.get_prefix().set_value(""); 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 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 exit_success = arguments.value_of("status_code").unwrap_or("0") == "0";
let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default(); let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default();
let keymap = arguments.value_of("keymap").unwrap_or("viins"); let keymap = arguments.value_of("keymap").unwrap_or("viins");
@ -48,21 +38,21 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => ASSUMED_MODE, _ => ASSUMED_MODE,
}; };
/* If an error symbol is set in the config, use symbols to indicate if exit_success {
success/failure, in addition to color */ module.set_style(config.style_success);
let symbol = if use_symbol && !exit_success {
module.new_segment("error_symbol", FAILURE_CHAR)
} else { } else {
match mode { module.set_style(config.style_failure);
ShellEditMode::Normal => module.new_segment("vicmd_symbol", VICMD_CHAR),
ShellEditMode::Insert => module.new_segment("symbol", SUCCESS_CHAR),
}
}; };
if exit_success { /* If an error symbol is set in the config, use symbols to indicate
symbol.set_style(style_success); success/failure, in addition to color */
if config.use_symbol_for_status && !exit_success {
module.create_segment("error_symbol", &config.error_symbol)
} else { } 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) Some(module)