2022-03-26 09:42:19 +00:00
|
|
|
|
use super::{Context, Module, ModuleConfig, Shell};
|
2019-10-04 12:42:33 +00:00
|
|
|
|
use crate::configs::character::CharacterConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
|
use crate::formatter::StringFormatter;
|
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
|
|
|
|
///
|
2022-06-04 10:41:24 +00:00
|
|
|
|
/// The character segment prints an arrow character in a color dependent on the
|
2020-07-07 22:45:32 +00:00
|
|
|
|
/// 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 20:12:53 +00:00
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-08-31 07:59:18 +00:00
|
|
|
|
enum ShellEditMode {
|
|
|
|
|
Normal,
|
2022-06-04 10:41:24 +00:00
|
|
|
|
Visual,
|
|
|
|
|
Replace,
|
|
|
|
|
ReplaceOne,
|
2019-08-31 07:59:18 +00:00
|
|
|
|
Insert,
|
2021-01-16 12:26:52 +00:00
|
|
|
|
}
|
2019-08-31 07:59:18 +00:00
|
|
|
|
const ASSUMED_MODE: ShellEditMode = ShellEditMode::Insert;
|
|
|
|
|
// TODO: extend config to more modes
|
2019-08-17 19:33:19 +00:00
|
|
|
|
|
2019-09-09 23:14:38 +00:00
|
|
|
|
let mut module = context.new_module("character");
|
2019-10-04 12:42:33 +00:00
|
|
|
|
let config: CharacterConfig = CharacterConfig::try_load(module.config);
|
2019-05-01 20:34:24 +00:00
|
|
|
|
|
2019-10-20 08:26:27 +00:00
|
|
|
|
let props = &context.properties;
|
2022-01-15 09:21:38 +00:00
|
|
|
|
let exit_code = props.status_code.as_deref().unwrap_or("0");
|
2022-01-04 09:49:42 +00:00
|
|
|
|
let keymap = props.keymap.as_str();
|
2022-01-15 09:21:38 +00:00
|
|
|
|
let exit_success = exit_code == "0";
|
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"
|
2021-04-28 18:39:32 +00:00
|
|
|
|
let mode = match (&context.shell, keymap) {
|
2023-06-12 09:05:44 +00:00
|
|
|
|
(Shell::Fish, "default")
|
|
|
|
|
| (Shell::Zsh, "vicmd")
|
|
|
|
|
| (Shell::Cmd | Shell::PowerShell, "vi") => ShellEditMode::Normal,
|
2022-06-04 10:41:24 +00:00
|
|
|
|
(Shell::Fish, "visual") => ShellEditMode::Visual,
|
|
|
|
|
(Shell::Fish, "replace") => ShellEditMode::Replace,
|
|
|
|
|
(Shell::Fish, "replace_one") => ShellEditMode::ReplaceOne,
|
2019-08-31 07:59:18 +00:00
|
|
|
|
_ => ASSUMED_MODE,
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
|
let symbol = match mode {
|
2022-06-18 21:23:59 +00:00
|
|
|
|
ShellEditMode::Normal => config.vimcmd_symbol,
|
2022-06-04 10:41:24 +00:00
|
|
|
|
ShellEditMode::Visual => config.vimcmd_visual_symbol,
|
|
|
|
|
ShellEditMode::Replace => config.vimcmd_replace_symbol,
|
|
|
|
|
ShellEditMode::ReplaceOne => config.vimcmd_replace_one_symbol,
|
2020-07-07 22:45:32 +00:00
|
|
|
|
ShellEditMode::Insert => {
|
|
|
|
|
if exit_success {
|
|
|
|
|
config.success_symbol
|
|
|
|
|
} else {
|
|
|
|
|
config.error_symbol
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-04 12:42:33 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
|
formatter
|
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
|
"symbol" => Some(symbol),
|
|
|
|
|
_ => None,
|
|
|
|
|
})
|
2021-11-01 21:18:45 +00:00
|
|
|
|
.parse(None, Some(context))
|
2020-07-07 22:45:32 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
|
Ok(segments) => segments,
|
|
|
|
|
Err(error) => {
|
|
|
|
|
log::warn!("Error in module `character`:\n{}", error);
|
|
|
|
|
return None;
|
2019-08-31 07:59:18 +00:00
|
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
|
});
|
2019-08-10 21:30:30 +00:00
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
|
Some(module)
|
2019-04-04 00:14:26 +00:00
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use crate::context::Shell;
|
|
|
|
|
use crate::test::ModuleRenderer;
|
2022-09-04 16:44:54 +00:00
|
|
|
|
use nu_ansi_term::Color;
|
2020-08-07 19:13:12 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
2021-02-11 20:08:17 +00:00
|
|
|
|
fn success_status() {
|
2020-08-07 19:13:12 +00: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 20:08:17 +00:00
|
|
|
|
fn failure_status() {
|
2020-08-07 19:13:12 +00: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 20:08:17 +00:00
|
|
|
|
fn custom_symbol() {
|
2020-08-07 19:13:12 +00: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 20:08:17 +00:00
|
|
|
|
fn zsh_keymap() {
|
2020-08-07 19:13:12 +00: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 20:08:17 +00:00
|
|
|
|
fn fish_keymap() {
|
2020-08-07 19:13:12 +00:00
|
|
|
|
let expected_vicmd = Some(format!("{} ", Color::Green.bold().paint("❮")));
|
|
|
|
|
let expected_specified = Some(format!("{} ", Color::Green.bold().paint("V")));
|
2022-06-04 10:41:24 +00:00
|
|
|
|
let expected_visual = Some(format!("{} ", Color::Yellow.bold().paint("❮")));
|
|
|
|
|
let expected_replace = Some(format!("{} ", Color::Purple.bold().paint("❮")));
|
2023-06-13 22:06:04 +00:00
|
|
|
|
let expected_replace_one = expected_replace.as_deref();
|
2020-08-07 19:13:12 +00:00
|
|
|
|
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);
|
|
|
|
|
|
2022-06-04 10:41:24 +00:00
|
|
|
|
// fish keymap is visual
|
2020-08-07 19:13:12 +00:00
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
|
.keymap("visual")
|
|
|
|
|
.collect();
|
2022-06-04 10:41:24 +00:00
|
|
|
|
assert_eq!(expected_visual, actual);
|
|
|
|
|
|
|
|
|
|
// fish keymap is replace
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
|
.keymap("replace")
|
|
|
|
|
.collect();
|
|
|
|
|
assert_eq!(expected_replace, actual);
|
|
|
|
|
|
|
|
|
|
// fish keymap is replace_one
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
|
.keymap("replace_one")
|
|
|
|
|
.collect();
|
2023-06-13 22:06:04 +00:00
|
|
|
|
assert_eq!(expected_replace_one, actual.as_deref());
|
2022-06-04 10:41:24 +00:00
|
|
|
|
|
|
|
|
|
// fish keymap is other
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
.shell(Shell::Fish)
|
|
|
|
|
.keymap("other")
|
|
|
|
|
.collect();
|
2020-08-07 19:13:12 +00:00
|
|
|
|
assert_eq!(expected_other, actual);
|
|
|
|
|
}
|
2022-01-10 05:47:53 +00:00
|
|
|
|
|
|
|
|
|
#[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);
|
|
|
|
|
}
|
2023-06-12 09:05:44 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn powershell_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("❯")));
|
|
|
|
|
|
|
|
|
|
// powershell keymap is vi
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
.shell(Shell::PowerShell)
|
|
|
|
|
.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::PowerShell)
|
|
|
|
|
.keymap("vi")
|
|
|
|
|
.collect();
|
|
|
|
|
assert_eq!(expected_specified, actual);
|
|
|
|
|
|
|
|
|
|
// powershell keymap is other
|
|
|
|
|
let actual = ModuleRenderer::new("character")
|
|
|
|
|
.shell(Shell::PowerShell)
|
|
|
|
|
.keymap("visual")
|
|
|
|
|
.collect();
|
|
|
|
|
assert_eq!(expected_other, actual);
|
|
|
|
|
}
|
2020-08-07 19:13:12 +00:00
|
|
|
|
}
|