mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
feat: Add configuration to change the character for non-zero sta… (#133)
Prompt can now switch characters in addition to switching character color. Add configuration options in so that users can do either, both, or neither.
This commit is contained in:
committed by
Matan Kushner
parent
994a865d4d
commit
39598ec691
+2
-2
@@ -44,7 +44,7 @@ impl<'a> Module<'a> {
|
||||
let mut segment = Segment::new(name);
|
||||
segment.set_style(self.style);
|
||||
// Use the provided value unless overwritten by config
|
||||
segment.set_value(self.config_value(name).unwrap_or(value));
|
||||
segment.set_value(self.config_value_str(name).unwrap_or(value));
|
||||
self.segments.push(segment);
|
||||
|
||||
self.segments.last_mut().unwrap()
|
||||
@@ -96,7 +96,7 @@ impl<'a> Module<'a> {
|
||||
}
|
||||
|
||||
/// Get a module's config value as a string
|
||||
fn config_value(&self, key: &str) -> Option<&str> {
|
||||
pub fn config_value_str(&self, key: &str) -> Option<&str> {
|
||||
self.config.and_then(|config| config.get_as_str(key))
|
||||
}
|
||||
|
||||
|
||||
@@ -10,17 +10,29 @@ use ansi_term::Color;
|
||||
/// - If the exit-code was anything else, the arrow will be formatted with
|
||||
/// `COLOR_FAILURE` (red by default)
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
const PROMPT_CHAR: &str = "➜";
|
||||
const SUCCESS_CHAR: &str = "➜";
|
||||
const FAILURE_CHAR: &str = "✖";
|
||||
let color_success = Color::Green.bold();
|
||||
let color_failure = Color::Red.bold();
|
||||
|
||||
let mut module = context.new_module("character")?;
|
||||
module.get_prefix().set_value("");
|
||||
|
||||
let symbol = module.new_segment("symbol", PROMPT_CHAR);
|
||||
|
||||
let arguments = &context.arguments;
|
||||
if arguments.value_of("status_code").unwrap_or("0") == "0" {
|
||||
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";
|
||||
|
||||
/* 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)
|
||||
} else {
|
||||
module.new_segment("symbol", SUCCESS_CHAR)
|
||||
};
|
||||
|
||||
if exit_success {
|
||||
symbol.set_style(color_success.bold());
|
||||
} else {
|
||||
symbol.set_style(color_failure.bold());
|
||||
|
||||
Reference in New Issue
Block a user