From 3dd035056dc2435e0b822c923f9f1b569bd748c2 Mon Sep 17 00:00:00 2001 From: Quinn Strahl Date: Sat, 31 Aug 2019 03:59:18 -0400 Subject: [PATCH] Support vicmd_symbol in fish-shell (#254) --- docs/config/README.md | 14 ++++----- src/init.rs | 9 +++++- src/main.rs | 4 +-- src/modules/character.rs | 24 +++++++++++++-- tests/testsuite/character.rs | 58 +++++++++++++++++++++++++++++++----- 5 files changed, 88 insertions(+), 21 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index e0f6a7a1..878b8039 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -122,13 +122,13 @@ can do this in two ways: by changing color (red/green) or by changing its shape ### Options -| Variable | Default | Description | -| ----------------------- | ------- | --------------------------------------------------------------------------------- | -| `symbol` | `"❯"` | The symbol used before the text input in the prompt. | -| `error_symbol` | `"✖"` | The symbol used before text input if the previous command failed. | -| `use_symbol_for_status` | `false` | Indicate error status by changing the symbol. | -| `vicmd_symbol` | `"❮"` | The symbol used before the text input in the prompt if zsh is in vim normal mode. | -| `disabled` | `false` | Disables the `character` module. | +| Variable | Default | Description | +| ----------------------- | ------- | ----------------------------------------------------------------------------------- | +| `symbol` | `"❯"` | The symbol used before the text input in the prompt. | +| `error_symbol` | `"✖"` | The symbol used before text input if the previous command failed. | +| `use_symbol_for_status` | `false` | Indicate error status by changing the symbol. | +| `vicmd_symbol` | `"❮"` | The symbol used before the text input in the prompt if shell is in vim normal mode. | +| `disabled` | `false` | Disables the `character` module. | ### Example diff --git a/src/init.rs b/src/init.rs index b7307dbe..9bb7ad9b 100644 --- a/src/init.rs +++ b/src/init.rs @@ -255,11 +255,18 @@ export STARSHIP_SHELL="zsh" const FISH_INIT: &str = r##" function fish_prompt + switch "$fish_key_bindings" + case fish_hybrid_key_bindings fish_vi_key_bindings + set keymap "$fish_bind_mode" + case '*' + set keymap insert + end set -l exit_code $status # Account for changes in variable name between v2.7 and v3.0 set -l CMD_DURATION "$CMD_DURATION$cmd_duration" set -l starship_duration (math --scale=0 "$CMD_DURATION / 1000") - ## STARSHIP ## prompt --status=$exit_code --cmd-duration=$starship_duration --jobs=(count (jobs -p)) + ## STARSHIP ## prompt --status=$exit_code --keymap=$keymap --cmd-duration=$starship_duration --jobs=(count (jobs -p)) end +function fish_mode_prompt; end export STARSHIP_SHELL="fish" "##; diff --git a/src/main.rs b/src/main.rs index 577d939f..a251c93e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,8 +48,8 @@ fn main() { .short("k") .long("keymap") .value_name("KEYMAP") - // zsh only - .help("The keymap of zsh") + // fish/zsh only + .help("The keymap of 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 5a8ba898..9c4d7e98 100644 --- a/src/modules/character.rs +++ b/src/modules/character.rs @@ -13,6 +13,12 @@ pub fn module<'a>(context: &'a Context) -> Option> { 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 color_success = Color::Green.bold(); let color_failure = Color::Red.bold(); @@ -25,16 +31,28 @@ pub fn module<'a>(context: &'a Context) -> Option> { .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 if keymap == "vicmd" { - module.new_segment("vicmd_symbol", VICMD_CHAR) } else { - module.new_segment("symbol", SUCCESS_CHAR) + match mode { + ShellEditMode::Normal => module.new_segment("vicmd_symbol", VICMD_CHAR), + ShellEditMode::Insert => module.new_segment("symbol", SUCCESS_CHAR), + } }; if exit_success { diff --git a/tests/testsuite/character.rs b/tests/testsuite/character.rs index 353db157..bdb2a09e 100644 --- a/tests/testsuite/character.rs +++ b/tests/testsuite/character.rs @@ -74,35 +74,77 @@ fn char_module_symbolyes_status() -> io::Result<()> { } #[test] -fn char_module_vicmd_keymap() -> io::Result<()> { - let expected_vicmd = format!("{} ", Color::Green.bold().paint("❮")); - let expected_specified = format!("{} ", Color::Green.bold().paint("N")); - let expected_other = format!("{} ", Color::Green.bold().paint("❯")); +fn char_module_zsh_keymap() -> io::Result<()> { + let expected_vicmd = "❮"; + // TODO make this less... well, stupid when ANSI escapes can be mocked out + let expected_specified = "I HIGHLY DOUBT THIS WILL SHOW UP IN OTHER OUTPUT"; + let expected_other = "❯"; // zle keymap is vicmd let output = common::render_module("character") + .env("STARSHIP_SHELL", "zsh") .arg("--keymap=vicmd") .output()?; let actual = String::from_utf8(output.stdout).unwrap(); - assert_eq!(expected_vicmd, actual); + assert!(actual.contains(&expected_vicmd)); // specified vicmd character let output = common::render_module("character") .use_config(toml::toml! { [character] - vicmd_symbol = "N" + vicmd_symbol = "I HIGHLY DOUBT THIS WILL SHOW UP IN OTHER OUTPUT" }) + .env("STARSHIP_SHELL", "zsh") .arg("--keymap=vicmd") .output()?; let actual = String::from_utf8(output.stdout).unwrap(); - assert_eq!(expected_specified, actual); + assert!(actual.contains(&expected_specified)); // zle keymap is other let output = common::render_module("character") + .env("STARSHIP_SHELL", "zsh") .arg("--keymap=visual") .output()?; let actual = String::from_utf8(output.stdout).unwrap(); - assert_eq!(expected_other, actual); + assert!(actual.contains(&expected_other)); + + Ok(()) +} + +#[test] +fn char_module_fish_keymap() -> io::Result<()> { + let expected_vicmd = "❮"; + // TODO make this less... well, stupid when ANSI escapes can be mocked out + let expected_specified = "I HIGHLY DOUBT THIS WILL SHOW UP IN OTHER OUTPUT"; + let expected_other = "❯"; + + // fish keymap is default + let output = common::render_module("character") + .env("STARSHIP_SHELL", "fish") + .arg("--keymap=default") + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + assert!(actual.contains(&expected_vicmd)); + + // specified vicmd character + let output = common::render_module("character") + .use_config(toml::toml! { + [character] + vicmd_symbol = "I HIGHLY DOUBT THIS WILL SHOW UP IN OTHER OUTPUT" + }) + .env("STARSHIP_SHELL", "fish") + .arg("--keymap=default") + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + assert!(actual.contains(&expected_specified)); + + // fish keymap is other + let output = common::render_module("character") + .env("STARSHIP_SHELL", "fish") + .arg("--keymap=visual") + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + assert!(actual.contains(&expected_other)); Ok(()) }