From 55bc6a27c65ec29ceec21c647c5be8d81433236c Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 5 Sep 2020 09:16:49 +0200 Subject: [PATCH] Make prompts forward compatible with fish 3.1.2 by passing locally exported variable Commit 5d135d555 (prompts: fix pipestatus for jobs prefixed with "not") introduced a backwards compatibility hack about adding an optional argument to __fish_print_pipestatus. This hack would break downgrading to fish 3.1.2 if the user copied the new prompt to their config - they would get a backtrace on every prompt which is arguably worse than the patch's minor improvement. This does away with the error trace - old fish just won't show the fancy new pipestatus on `not true`. Implemented by passing the last $status as the poor man's kwarg, which works since 3.1.0 (9b86d5dd1 Export all local exported variables in a new scope). The prompts don't work with fish 3.0.0 or older; downgrading does not seem too important in general but I think this patch is an okay simplification. --- share/functions/__fish_print_pipestatus.fish | 5 ++--- share/functions/fish_prompt.fish | 4 ++-- share/tools/web_config/sample_prompts/classic_status.fish | 4 ++-- share/tools/web_config/sample_prompts/classic_vcs.fish | 4 ++-- share/tools/web_config/sample_prompts/informative.fish | 4 ++-- share/tools/web_config/sample_prompts/informative_vcs.fish | 4 ++-- 6 files changed, 12 insertions(+), 13 deletions(-) diff --git a/share/functions/__fish_print_pipestatus.fish b/share/functions/__fish_print_pipestatus.fish index bc8092076..89d1fc8e5 100644 --- a/share/functions/__fish_print_pipestatus.fish +++ b/share/functions/__fish_print_pipestatus.fish @@ -1,8 +1,7 @@ function __fish_print_pipestatus --description "Print pipestatus for prompt" - # take $status as optional argument to maintain compatibility set -l last_status - if set last_status (string match -r -- '^\d+$' $argv[1]) - set -e argv[1] + if set -q __fish_last_status + set last_status $__fish_last_status else set last_status $argv[-1] # default to $pipestatus[-1] end diff --git a/share/functions/fish_prompt.fish b/share/functions/fish_prompt.fish index c172084b2..553367c34 100644 --- a/share/functions/fish_prompt.fish +++ b/share/functions/fish_prompt.fish @@ -3,7 +3,7 @@ function fish_prompt --description 'Write out the prompt' set -l last_pipestatus $pipestatus - set -l last_status $status + set -lx __fish_last_status $status # Export for __fish_print_pipestatus. set -l normal (set_color normal) # Color the prompt differently when we're root @@ -30,7 +30,7 @@ function fish_prompt --description 'Write out the prompt' set bold_flag end set __fish_prompt_status_generation $status_generation - set -l prompt_status (__fish_print_pipestatus $last_status "[" "]" "|" (set_color $fish_color_status) (set_color $bold_flag $fish_color_status) $last_pipestatus) + set -l prompt_status (__fish_print_pipestatus "[" "]" "|" (set_color $fish_color_status) (set_color $bold_flag $fish_color_status) $last_pipestatus) echo -n -s (set_color $fish_color_user) "$USER" $normal @ (set_color $color_host) (prompt_hostname) $normal ' ' (set_color $color_cwd) (prompt_pwd) $normal (fish_vcs_prompt) $normal " "$prompt_status $suffix " " end diff --git a/share/tools/web_config/sample_prompts/classic_status.fish b/share/tools/web_config/sample_prompts/classic_status.fish index a0c699fa8..f916fb5dd 100644 --- a/share/tools/web_config/sample_prompts/classic_status.fish +++ b/share/tools/web_config/sample_prompts/classic_status.fish @@ -4,7 +4,7 @@ function fish_prompt --description "Write out the prompt" # Save our status set -l last_pipestatus $pipestatus - set -l last_status $status + set -lx __fish_last_status $status # Export for __fish_print_pipestatus. set -l color_cwd set -l suffix @@ -23,6 +23,6 @@ function fish_prompt --description "Write out the prompt" end echo -n -s "$USER" @ (prompt_hostname) ' ' (set_color $color_cwd) (prompt_pwd) \ - " "(__fish_print_pipestatus $last_status "[" "]" "|" (set_color $fish_color_status) (set_color --bold $fish_color_status) $last_pipestatus) \ + " "(__fish_print_pipestatus "[" "]" "|" (set_color $fish_color_status) (set_color --bold $fish_color_status) $last_pipestatus) \ (set_color normal) "$suffix " end diff --git a/share/tools/web_config/sample_prompts/classic_vcs.fish b/share/tools/web_config/sample_prompts/classic_vcs.fish index c172084b2..553367c34 100644 --- a/share/tools/web_config/sample_prompts/classic_vcs.fish +++ b/share/tools/web_config/sample_prompts/classic_vcs.fish @@ -3,7 +3,7 @@ function fish_prompt --description 'Write out the prompt' set -l last_pipestatus $pipestatus - set -l last_status $status + set -lx __fish_last_status $status # Export for __fish_print_pipestatus. set -l normal (set_color normal) # Color the prompt differently when we're root @@ -30,7 +30,7 @@ function fish_prompt --description 'Write out the prompt' set bold_flag end set __fish_prompt_status_generation $status_generation - set -l prompt_status (__fish_print_pipestatus $last_status "[" "]" "|" (set_color $fish_color_status) (set_color $bold_flag $fish_color_status) $last_pipestatus) + set -l prompt_status (__fish_print_pipestatus "[" "]" "|" (set_color $fish_color_status) (set_color $bold_flag $fish_color_status) $last_pipestatus) echo -n -s (set_color $fish_color_user) "$USER" $normal @ (set_color $color_host) (prompt_hostname) $normal ' ' (set_color $color_cwd) (prompt_pwd) $normal (fish_vcs_prompt) $normal " "$prompt_status $suffix " " end diff --git a/share/tools/web_config/sample_prompts/informative.fish b/share/tools/web_config/sample_prompts/informative.fish index cec7c3c99..8d608b013 100644 --- a/share/tools/web_config/sample_prompts/informative.fish +++ b/share/tools/web_config/sample_prompts/informative.fish @@ -4,7 +4,7 @@ function fish_prompt --description 'Informative prompt' #Save the return status of the previous command set -l last_pipestatus $pipestatus - set -l last_status $status + set -lx __fish_last_status $status # Export for __fish_print_pipestatus. if functions -q fish_is_root_user; and fish_is_root_user printf '%s@%s %s%s%s# ' $USER (prompt_hostname) (set -q fish_color_cwd_root @@ -12,7 +12,7 @@ function fish_prompt --description 'Informative prompt' or set_color $fish_color_cwd) \ (prompt_pwd) (set_color normal) else - set -l pipestatus_string (__fish_print_pipestatus $last_status "[" "] " "|" (set_color $fish_color_status) \ + set -l pipestatus_string (__fish_print_pipestatus "[" "] " "|" (set_color $fish_color_status) \ (set_color --bold $fish_color_status) $last_pipestatus) printf '[%s] %s%s@%s %s%s %s%s%s \f\r> ' (date "+%H:%M:%S") (set_color brblue) \ diff --git a/share/tools/web_config/sample_prompts/informative_vcs.fish b/share/tools/web_config/sample_prompts/informative_vcs.fish index 1b66c14b8..0df4008d2 100644 --- a/share/tools/web_config/sample_prompts/informative_vcs.fish +++ b/share/tools/web_config/sample_prompts/informative_vcs.fish @@ -3,7 +3,7 @@ function fish_prompt --description 'Write out the prompt' set -l last_pipestatus $pipestatus - set -l last_status $status + set -lx __fish_last_status $status # Export for __fish_print_pipestatus. if not set -q __fish_git_prompt_show_informative_status set -g __fish_git_prompt_show_informative_status 1 @@ -78,7 +78,7 @@ function fish_prompt --description 'Write out the prompt' printf '%s ' (fish_vcs_prompt) - set -l pipestatus_string (__fish_print_pipestatus $last_status "[" "] " "|" (set_color $fish_color_status) (set_color --bold $fish_color_status) $last_pipestatus) + set -l pipestatus_string (__fish_print_pipestatus "[" "] " "|" (set_color $fish_color_status) (set_color --bold $fish_color_status) $last_pipestatus) echo -n $pipestatus_string set_color normal