2019-05-01 20:34:24 +00:00
|
|
|
|
use super::{Context, Module};
|
2019-04-12 23:11:40 +00:00
|
|
|
|
use ansi_term::Color;
|
2019-04-19 20:57:14 +00:00
|
|
|
|
|
2019-07-19 20:18:52 +00:00
|
|
|
|
/// Creates a module for the prompt character
|
2019-04-05 01:35:24 +00:00
|
|
|
|
///
|
2019-07-19 20:18:52 +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 `COLOR_SUCCESS`
|
|
|
|
|
/// (green by default)
|
|
|
|
|
/// - If the exit-code was anything else, the arrow will be formatted with
|
|
|
|
|
/// `COLOR_FAILURE` (red by default)
|
2019-07-02 20:12:53 +00:00
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-08-18 15:34:45 +00:00
|
|
|
|
const SUCCESS_CHAR: &str = "❯";
|
2019-08-10 21:30:30 +00:00
|
|
|
|
const FAILURE_CHAR: &str = "✖";
|
2019-08-17 19:33:19 +00:00
|
|
|
|
const VICMD_CHAR: &str = "❮";
|
2019-08-31 07:59:18 +00:00
|
|
|
|
enum ShellEditMode {
|
|
|
|
|
Normal,
|
|
|
|
|
Insert,
|
|
|
|
|
};
|
|
|
|
|
const ASSUMED_MODE: ShellEditMode = ShellEditMode::Insert;
|
|
|
|
|
// TODO: extend config to more modes
|
2019-08-17 19:33:19 +00:00
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
|
let color_success = Color::Green.bold();
|
|
|
|
|
let color_failure = Color::Red.bold();
|
2019-04-04 00:14:26 +00:00
|
|
|
|
|
2019-07-19 20:18:52 +00:00
|
|
|
|
let mut module = context.new_module("character")?;
|
2019-05-01 20:34:24 +00:00
|
|
|
|
module.get_prefix().set_value("");
|
|
|
|
|
|
|
|
|
|
let arguments = &context.arguments;
|
2019-08-10 21:30:30 +00:00
|
|
|
|
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";
|
2019-08-31 07:59:18 +00:00
|
|
|
|
let shell = std::env::var("STARSHIP_SHELL").unwrap_or_default();
|
2019-08-17 19:33:19 +00:00
|
|
|
|
let keymap = arguments.value_of("keymap").unwrap_or("viins");
|
2019-08-10 21:30:30 +00:00
|
|
|
|
|
2019-08-31 07:59:18 +00:00
|
|
|
|
// 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,
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-10 21:30:30 +00:00
|
|
|
|
/* 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 {
|
2019-08-31 07:59:18 +00:00
|
|
|
|
match mode {
|
|
|
|
|
ShellEditMode::Normal => module.new_segment("vicmd_symbol", VICMD_CHAR),
|
|
|
|
|
ShellEditMode::Insert => module.new_segment("symbol", SUCCESS_CHAR),
|
|
|
|
|
}
|
2019-08-10 21:30:30 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if exit_success {
|
2019-05-01 20:34:24 +00:00
|
|
|
|
symbol.set_style(color_success.bold());
|
2019-04-04 00:14:26 +00:00
|
|
|
|
} else {
|
2019-05-01 20:34:24 +00:00
|
|
|
|
symbol.set_style(color_failure.bold());
|
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
|
|
|
|
}
|