2020-01-26 17:37:18 -05:00
|
|
|
|
use super::{Context, Module, RootModuleConfig, Shell};
|
2019-10-04 20:42:33 +08:00
|
|
|
|
use crate::configs::character::CharacterConfig;
|
2020-07-08 06:45:32 +08:00
|
|
|
|
use crate::formatter::StringFormatter;
|
2019-04-19 16:57:14 -04:00
|
|
|
|
|
2019-07-19 16:18:52 -04:00
|
|
|
|
/// Creates a module for the prompt character
|
2019-04-04 21:35:24 -04:00
|
|
|
|
///
|
2020-07-08 06:45:32 +08:00
|
|
|
|
/// The character segment prints an arrow character in a color dependant on the
|
|
|
|
|
|
/// exit-code of the last executed command:
|
|
|
|
|
|
/// - If the exit-code was "0", it will be formatted with `success_symbol`
|
|
|
|
|
|
/// (green arrow by default)
|
|
|
|
|
|
/// - If the exit-code was anything else, it will be formatted with
|
|
|
|
|
|
/// `error_symbol` (red arrow by default)
|
2019-07-02 16:12:53 -04:00
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-08-31 03:59:18 -04:00
|
|
|
|
enum ShellEditMode {
|
|
|
|
|
|
Normal,
|
|
|
|
|
|
Insert,
|
2021-01-16 20:26:52 +08:00
|
|
|
|
}
|
2019-08-31 03:59:18 -04:00
|
|
|
|
const ASSUMED_MODE: ShellEditMode = ShellEditMode::Insert;
|
|
|
|
|
|
// TODO: extend config to more modes
|
2019-08-18 04:33:19 +09:00
|
|
|
|
|
2019-09-09 18:14:38 -05:00
|
|
|
|
let mut module = context.new_module("character");
|
2019-10-04 20:42:33 +08:00
|
|
|
|
let config: CharacterConfig = CharacterConfig::try_load(module.config);
|
2019-05-01 16:34:24 -04:00
|
|
|
|
|
2019-10-20 09:26:27 +01:00
|
|
|
|
let props = &context.properties;
|
2022-01-04 10:49:42 +01:00
|
|
|
|
let exit_code = props.status_code;
|
|
|
|
|
|
let keymap = props.keymap.as_str();
|
|
|
|
|
|
let exit_success = exit_code.unwrap_or_default() == 0;
|
2019-08-10 14:30:30 -07:00
|
|
|
|
|
2019-08-31 03:59:18 -04: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"
|
2021-04-28 20:39:32 +02:00
|
|
|
|
let mode = match (&context.shell, keymap) {
|
2022-01-10 11:17:53 +05:30
|
|
|
|
(Shell::Fish, "default") | (Shell::Zsh, "vicmd") | (Shell::Cmd, "vi") => {
|
|
|
|
|
|
ShellEditMode::Normal
|
|
|
|
|
|
}
|
2019-08-31 03:59:18 -04:00
|
|
|
|
_ => ASSUMED_MODE,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2020-07-08 06:45:32 +08:00
|
|
|
|
let symbol = match mode {
|
|
|
|
|
|
ShellEditMode::Normal => config.vicmd_symbol,
|
|
|
|
|
|
ShellEditMode::Insert => {
|
|
|
|
|
|
if exit_success {
|
|
|
|
|
|
config.success_symbol
|
|
|
|
|
|
} else {
|
|
|
|
|
|
config.error_symbol
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-10-04 20:42:33 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2020-07-08 06:45:32 +08:00
|
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
|
|
formatter
|
|
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
|
|
"symbol" => Some(symbol),
|
|
|
|
|
|
_ => None,
|
|
|
|
|
|
})
|
2021-11-01 14:18:45 -07:00
|
|
|
|
.parse(None, Some(context))
|
2020-07-08 06:45:32 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
|
|
Ok(segments) => segments,
|
|
|
|
|
|
Err(error) => {
|
|
|
|
|
|
log::warn!("Error in module `character`:\n{}", error);
|
|
|
|
|
|
return None;
|
2019-08-31 03:59:18 -04:00
|
|
|
|
}
|
2020-07-08 06:45:32 +08:00
|
|
|
|
});
|
2019-08-10 14:30:30 -07:00
|
|
|
|
|
2019-05-01 16:34:24 -04:00
|
|
|
|
Some(module)
|
2019-04-03 20:14:26 -04:00
|
|
|
|
}
|
2020-08-07 21:13:12 +02:00
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod test {
|
|
|
|
|
|
use crate::context::Shell;
|
|
|
|
|
|
use crate::test::ModuleRenderer;
|
|
|
|
|
|
use ansi_term::Color;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 21:08:17 +01:00
|
|
|
|
fn success_status() {
|
2020-08-07 21:13:12 +02:00
|
|
|
|
let expected = Some(format!("{} ", Color::Green.bold().paint("❯")));
|
|
|
|
|
|
|
|
|
|
|
|
// Status code 0
|
|
|
|
|
|
let actual = ModuleRenderer::new("character").status(0).collect();
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
|
|
|
|
|
|
// No status code
|
|
|
|
|
|
let actual = ModuleRenderer::new("character").collect();
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 21:08:17 +01:00
|
|
|
|
fn failure_status() {
|
2020-08-07 21:13:12 +02:00
|
|
|
|
let expected = Some(format!("{} ", Color::Red.bold().paint("❯")));
|
|
|
|
|
|
|
|
|
|
|
|
let exit_values = [1, 54321, -5000];
|
|
|
|
|
|
|
|
|
|
|
|
for status in &exit_values {
|
|
|
|
|
|
let actual = ModuleRenderer::new("character").status(*status).collect();
|
|
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 21:08:17 +01:00
|
|
|
|
fn custom_symbol() {
|
2020-08-07 21:13:12 +02:00
|
|
|
|
let expected_fail = Some(format!("{} ", Color::Red.bold().paint("✖")));
|
|
|
|
|
|
let expected_success = Some(format!("{} ", Color::Green.bold().paint("➜")));
|
|
|
|
|
|
|
|
|
|
|
|
let exit_values = [1, 54321, -5000];
|
|
|
|
|
|
|
|
|
|
|
|
// Test failure values
|
|
|
|
|
|
for status in &exit_values {
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.config(toml::toml! {
|
|
|
|
|
|
[character]
|
|
|
|
|
|
success_symbol = "[➜](bold green)"
|
|
|
|
|
|
error_symbol = "[✖](bold red)"
|
|
|
|
|
|
})
|
|
|
|
|
|
.status(*status)
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_fail, actual);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Test success
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.config(toml::toml! {
|
|
|
|
|
|
[character]
|
|
|
|
|
|
success_symbol = "[➜](bold green)"
|
|
|
|
|
|
error_symbol = "[✖](bold red)"
|
|
|
|
|
|
})
|
|
|
|
|
|
.status(0)
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_success, actual);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 21:08:17 +01:00
|
|
|
|
fn zsh_keymap() {
|
2020-08-07 21:13:12 +02:00
|
|
|
|
let expected_vicmd = Some(format!("{} ", Color::Green.bold().paint("❮")));
|
|
|
|
|
|
let expected_specified = Some(format!("{} ", Color::Green.bold().paint("V")));
|
|
|
|
|
|
let expected_other = Some(format!("{} ", Color::Green.bold().paint("❯")));
|
|
|
|
|
|
|
|
|
|
|
|
// zle keymap is vicmd
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.shell(Shell::Zsh)
|
|
|
|
|
|
.keymap("vicmd")
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_vicmd, actual);
|
|
|
|
|
|
|
|
|
|
|
|
// specified vicmd character
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.config(toml::toml! {
|
|
|
|
|
|
[character]
|
|
|
|
|
|
vicmd_symbol = "[V](bold green)"
|
|
|
|
|
|
})
|
|
|
|
|
|
.shell(Shell::Zsh)
|
|
|
|
|
|
.keymap("vicmd")
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_specified, actual);
|
|
|
|
|
|
|
|
|
|
|
|
// zle keymap is other
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.shell(Shell::Zsh)
|
|
|
|
|
|
.keymap("visual")
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_other, actual);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 21:08:17 +01:00
|
|
|
|
fn fish_keymap() {
|
2020-08-07 21:13:12 +02:00
|
|
|
|
let expected_vicmd = Some(format!("{} ", Color::Green.bold().paint("❮")));
|
|
|
|
|
|
let expected_specified = Some(format!("{} ", Color::Green.bold().paint("V")));
|
|
|
|
|
|
let expected_other = Some(format!("{} ", Color::Green.bold().paint("❯")));
|
|
|
|
|
|
|
|
|
|
|
|
// fish keymap is default
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
|
|
.keymap("default")
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_vicmd, actual);
|
|
|
|
|
|
|
|
|
|
|
|
// specified vicmd character
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.config(toml::toml! {
|
|
|
|
|
|
[character]
|
|
|
|
|
|
vicmd_symbol = "[V](bold green)"
|
|
|
|
|
|
})
|
|
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
|
|
.keymap("default")
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_specified, actual);
|
|
|
|
|
|
|
|
|
|
|
|
// fish keymap is other
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
|
|
.keymap("visual")
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_other, actual);
|
|
|
|
|
|
}
|
2022-01-10 11:17:53 +05:30
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn cmd_keymap() {
|
|
|
|
|
|
let expected_vicmd = Some(format!("{} ", Color::Green.bold().paint("❮")));
|
|
|
|
|
|
let expected_specified = Some(format!("{} ", Color::Green.bold().paint("V")));
|
|
|
|
|
|
let expected_other = Some(format!("{} ", Color::Green.bold().paint("❯")));
|
|
|
|
|
|
|
|
|
|
|
|
// cmd keymap is vi
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.shell(Shell::Cmd)
|
|
|
|
|
|
.keymap("vi")
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_vicmd, actual);
|
|
|
|
|
|
|
|
|
|
|
|
// specified vicmd character
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.config(toml::toml! {
|
|
|
|
|
|
[character]
|
|
|
|
|
|
vicmd_symbol = "[V](bold green)"
|
|
|
|
|
|
})
|
|
|
|
|
|
.shell(Shell::Cmd)
|
|
|
|
|
|
.keymap("vi")
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_specified, actual);
|
|
|
|
|
|
|
|
|
|
|
|
// cmd keymap is other
|
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
|
.shell(Shell::Cmd)
|
|
|
|
|
|
.keymap("visual")
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
assert_eq!(expected_other, actual);
|
|
|
|
|
|
}
|
2020-08-07 21:13:12 +02:00
|
|
|
|
}
|