1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-04 20:37:56 +00:00

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 character;
pub mod dotnet;
pub mod rust;

View File

@ -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<Module<'a>> {
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<Module<'a>> {
}
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)
}

View File

@ -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<Module<'a>> {
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<Module<'a>> {
// 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<Module<'a>> {
_ => 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)