summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZhenhui Xie <xiezh0831@126.com>2019-10-04 20:42:33 +0800
committerMatan Kushner <hello@matchai.me>2019-10-04 21:42:33 +0900
commitcda01deffa68ca08cbf16d0c5dfb7a792e8375ce (patch)
treec14741bf847d510ee659bf69c56bbc2eeaaca92d
parent1d701729cf3ff9a133e9266b95b3158086ec01da (diff)
refactor: Rewrite aws and character module to use module config (#459)
-rw-r--r--starship/src/configs/aws.rs29
-rw-r--r--starship/src/configs/character.rs38
-rw-r--r--starship/src/configs/mod.rs2
-rw-r--r--starship/src/modules/aws.rs16
-rw-r--r--starship/src/modules/character.rs38
5 files changed, 90 insertions, 33 deletions
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<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)
}
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<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,22 +38,22 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => ASSUMED_MODE,
};
+ if exit_success {
+ module.set_style(config.style_success);
+ } else {
+ module.set_style(config.style_failure);
+ };
+
/* 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 config.use_symbol_for_status && !exit_success {
+ module.create_segment("error_symbol", &config.error_symbol)
} else {
match mode {
- ShellEditMode::Normal => module.new_segment("vicmd_symbol", VICMD_CHAR),
- ShellEditMode::Insert => module.new_segment("symbol", SUCCESS_CHAR),
+ ShellEditMode::Normal => module.create_segment("vicmd_symbol", &config.vicmd_symbol),
+ ShellEditMode::Insert => module.create_segment("symbol", &config.symbol),
}
};
- if exit_success {
- symbol.set_style(style_success);
- } else {
- symbol.set_style(style_failure);
- };
-
Some(module)
}