1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-16 01:57:07 +00:00
starship/src/modules/character.rs

70 lines
2.5 KiB
Rust
Raw Normal View History

2019-05-01 20:34:24 +00:00
use super::{Context, Module};
2019-04-12 23:11:40 +00:00
use ansi_term::Color;
/// Creates a module for the prompt character
2019-04-05 01:35:24 +00:00
///
/// The character segment prints an arrow character in a color dependant on the exit-
2019-04-04 16:18:02 +00:00
/// code of the last executed command:
/// - If the exit-code was "0", the arrow will be formatted with `style_success`
2019-04-04 16:18:02 +00:00
/// (green by default)
/// - 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,
};
const ASSUMED_MODE: ShellEditMode = ShellEditMode::Insert;
// TODO: extend config to more modes
let mut module = context.new_module("character");
2019-05-01 20:34:24 +00:00
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());
2019-05-01 20:34:24 +00:00
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");
// Match shell "keymap" names to normalized vi modes
// NOTE: in vi mode, fish reports normal mode as "default".
// Unfortunately, this is also the name of the non-vi default mode.
// We do some environment detection in src/init.rs to translate.
// The result: in non-vi fish, keymap is always reported as "insert"
let mode = match (shell.as_str(), keymap) {
("fish", "default") | ("zsh", "vicmd") => ShellEditMode::Normal,
_ => 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)
} else {
match mode {
ShellEditMode::Normal => module.new_segment("vicmd_symbol", VICMD_CHAR),
ShellEditMode::Insert => module.new_segment("symbol", SUCCESS_CHAR),
}
};
if exit_success {
symbol.set_style(style_success);
2019-04-04 00:14:26 +00:00
} else {
symbol.set_style(style_failure);
2019-04-08 03:28:38 +00:00
};
2019-04-04 00:14:26 +00:00
2019-05-01 20:34:24 +00:00
Some(module)
2019-04-04 00:14:26 +00:00
}