From 9741d4d0aa3711613cbdf772a40148c0d795e42a Mon Sep 17 00:00:00 2001 From: Rashil Gandhi Date: Fri, 26 Nov 2021 12:36:53 +0530 Subject: [PATCH] add keymap support --- docs/config/README.md | 2 +- src/init/starship.lua | 2 ++ src/main.rs | 5 ++--- src/modules/character.rs | 36 +++++++++++++++++++++++++++++++++++- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 159f69b3a..d21a5b2c2 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -447,7 +447,7 @@ look at [this example](#with-custom-error-shape). ::: warning -`vicmd_symbol` is only supported in fish and zsh. +`vicmd_symbol` is only supported in cmd, fish and zsh. ::: diff --git a/src/init/starship.lua b/src/init/starship.lua index 1bf2e621e..601c433b8 100644 --- a/src/init/starship.lua +++ b/src/init/starship.lua @@ -32,6 +32,7 @@ function custom_prompt:filter(prompt) .." --status="..os.geterrorlevel() .." --cmd-duration="..math.floor(curr_duration*1000) .." --terminal-width="..console.getwidth() + .." --keymap="..rl.getvariable('keymap') ):read("*a") end @@ -40,6 +41,7 @@ function custom_prompt:rightfilter(prompt) .." --status="..os.geterrorlevel() .." --cmd-duration="..math.floor(curr_duration*1000) .." --terminal-width="..console.getwidth() + .." --keymap="..rl.getvariable('keymap') ):read("*a") end diff --git a/src/main.rs b/src/main.rs index 5dfd624ec..48b52e04e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ fn main() { .long("pipestatus") .value_name("PIPESTATUS") .help("Status codes from a command pipeline") - .long_help("Bash and Zsh supports returning codes for each process in a pipeline.") + .long_help("Bash, Fish and Zsh support returning codes for each process in a pipeline.") .multiple(true); let terminal_width_arg = Arg::with_name("terminal_width") @@ -72,8 +72,7 @@ fn main() { .short("k") .long("keymap") .value_name("KEYMAP") - // fish/zsh only - .help("The keymap of fish/zsh") + .help("The keymap of cmd/fish/zsh") .takes_value(true); let jobs_arg = Arg::with_name("jobs") diff --git a/src/modules/character.rs b/src/modules/character.rs index bf60f7729..0823b8200 100644 --- a/src/modules/character.rs +++ b/src/modules/character.rs @@ -32,7 +32,9 @@ pub fn module<'a>(context: &'a Context) -> Option> { // 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 (&context.shell, keymap) { - (Shell::Fish, "default") | (Shell::Zsh, "vicmd") => ShellEditMode::Normal, + (Shell::Fish, "default") | (Shell::Zsh, "vicmd") | (Shell::Cmd, "vi") => { + ShellEditMode::Normal + } _ => ASSUMED_MODE, }; @@ -193,4 +195,36 @@ mod test { .collect(); assert_eq!(expected_other, actual); } + + #[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); + } }