From 3dd035056dc2435e0b822c923f9f1b569bd748c2 Mon Sep 17 00:00:00 2001 From: Quinn Strahl Date: Sat, 31 Aug 2019 03:59:18 -0400 Subject: Support vicmd_symbol in fish-shell (#254) --- src/init.rs | 9 ++++++++- src/main.rs | 4 ++-- src/modules/character.rs | 24 +++++++++++++++++++++--- 3 files changed, 31 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/init.rs b/src/init.rs index b7307dbef..9bb7ad9b1 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 577d939f7..a251c93e8 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 5a8ba898a..9c4d7e982 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 { -- cgit v1.2.3