Support vicmd_symbol in fish-shell (#254)

This commit is contained in:
Quinn Strahl
2019-08-31 03:59:18 -04:00
committed by Kevin Song
parent 4eb7beca48
commit 3dd035056d
5 changed files with 88 additions and 21 deletions
+8 -1
View File
@@ -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"
"##;
+2 -2
View File
@@ -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")
+21 -3
View File
@@ -13,6 +13,12 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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<Module<'a>> {
.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 {