From 6902459566b491e2debb136f43f815965910eeee Mon Sep 17 00:00:00 2001
From: Johannes Altmanninger <aclopte@gmail.com>
Date: Tue, 10 Dec 2019 22:32:35 +0100
Subject: [PATCH] prompt: don't print status of last process in pipe twice

If a command fails, print the pipestatus in red instead of yellow and
don't print the status of the last process again. See #6375.

Also use $fish_color_status for coloring status consistently.

Also use __fish_pipestatus_with_signal to print SIGPIPE instead
of a numeric code on e.g.: yes | less +q

[ci skip]
---
 share/functions/__fish_print_pipestatus.fish  |  6 ++----
 share/functions/fish_prompt.fish              |  6 +-----
 .../sample_prompts/classic_status.fish        |  8 +++-----
 .../sample_prompts/classic_vcs.fish           |  5 +----
 .../sample_prompts/informative.fish           | 19 +++++++------------
 .../sample_prompts/informative_vcs.fish       | 15 ++++++---------
 6 files changed, 20 insertions(+), 39 deletions(-)

diff --git a/share/functions/__fish_print_pipestatus.fish b/share/functions/__fish_print_pipestatus.fish
index 690588fde..b9f90e4f7 100644
--- a/share/functions/__fish_print_pipestatus.fish
+++ b/share/functions/__fish_print_pipestatus.fish
@@ -6,10 +6,8 @@ function __fish_print_pipestatus --description "Print pipestatus for prompt"
     set -l status_color $argv[5]
     set -e argv[1 2 3 4 5]
 
-    # only output $pipestatus if there was a pipe
-    # and any part of it but the last one had non-zero exit status
-    # if only the last process failed, prompts already print $status which is $pipestatus[-1]
-    if set -q argv[2] && string match -qvr '^0$' $argv[1..-2]
+    # only output status codes if some process in the pipe failed
+    if string match -qvr '^0$' $argv
         set -l sep (set_color normal){$brace_sep_color}{$separator}(set_color normal){$status_color}
         set -l last_pipestatus_string (string join "$sep" (__fish_pipestatus_with_signal $argv))
         printf "%s%s%s%s%s%s%s%s%s%s" (set_color normal )$brace_sep_color $left_brace \
diff --git a/share/functions/fish_prompt.fish b/share/functions/fish_prompt.fish
index 352119b1d..1b1fcc19b 100644
--- a/share/functions/fish_prompt.fish
+++ b/share/functions/fish_prompt.fish
@@ -3,7 +3,6 @@
 
 function fish_prompt --description 'Write out the prompt'
     set -l last_pipestatus $pipestatus
-    set -l last_status $status
     set -l normal (set_color normal)
 
     # Initialize our variables.
@@ -26,10 +25,7 @@ function fish_prompt --description 'Write out the prompt'
     end
 
     # Write pipestatus
-    set -l prompt_status (__fish_print_pipestatus "[" "] " "|" (set_color yellow) (set_color --bold yellow) $last_pipestatus)
-    if test $last_status -ne 0
-        set prompt_status " $prompt_status" (set_color $fish_color_status) "[$last_status]" "$normal"
-    end
+    set -l prompt_status (__fish_print_pipestatus " [" "]" "|" (set_color $fish_color_status) (set_color --bold $fish_color_status) $last_pipestatus)
 
     echo -n -s (set_color $fish_color_user) "$USER" $normal @ (set_color $fish_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 b84af5ebf..ff0e52669 100644
--- a/share/tools/web_config/sample_prompts/classic_status.fish
+++ b/share/tools/web_config/sample_prompts/classic_status.fish
@@ -4,13 +4,11 @@
 function fish_prompt --description "Write out the prompt"
     # Save our status
     set -l last_pipestatus $pipestatus
-    set -l last_status $status
 
-    __fish_print_pipestatus "[" "] " "|" (set_color yellow) (set_color --bold yellow) $last_pipestatus
+    set -q fish_color_status
+    or set -U fish_color_status red
 
-    if test $last_status -ne 0
-        printf "%s(%s)%s " (set_color red --bold) (__fish_status_to_signal $last_status) (set_color normal)
-    end
+    __fish_print_pipestatus "[" "] " "|" (set_color $fish_color_status) (set_color --bold $fish_color_status) $last_pipestatus
 
     set -l color_cwd
     set -l suffix
diff --git a/share/tools/web_config/sample_prompts/classic_vcs.fish b/share/tools/web_config/sample_prompts/classic_vcs.fish
index 352119b1d..694a524c6 100644
--- a/share/tools/web_config/sample_prompts/classic_vcs.fish
+++ b/share/tools/web_config/sample_prompts/classic_vcs.fish
@@ -26,10 +26,7 @@ function fish_prompt --description 'Write out the prompt'
     end
 
     # Write pipestatus
-    set -l prompt_status (__fish_print_pipestatus "[" "] " "|" (set_color yellow) (set_color --bold yellow) $last_pipestatus)
-    if test $last_status -ne 0
-        set prompt_status " $prompt_status" (set_color $fish_color_status) "[$last_status]" "$normal"
-    end
+    set -l prompt_status (__fish_print_pipestatus " [" "]" "|" (set_color $fish_color_status) (set_color --bold $fish_color_status) $last_pipestatus)
 
     echo -n -s (set_color $fish_color_user) "$USER" $normal @ (set_color $fish_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 fde4c3dfd..bced9ce89 100644
--- a/share/tools/web_config/sample_prompts/informative.fish
+++ b/share/tools/web_config/sample_prompts/informative.fish
@@ -4,14 +4,9 @@
 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 the color for the status depending on the value
-    if test $stat -gt 0
-        set -l status_color (set_color brred)
-    else
-        set -l status_color (set_color brgreen)
-    end
+    set -q fish_color_status
+    or set -U fish_color_status red
 
     switch "$USER"
         case root toor
@@ -20,11 +15,11 @@ function fish_prompt --description 'Informative prompt'
                                                              or set_color $fish_color_cwd) \
                 (prompt_pwd) (set_color normal)
         case '*'
-            set -l pipestatus_string (__fish_print_pipestatus "[" "] " "|" (set_color yellow) \
-                                      (set_color bryellow) $last_pipestatus)
+            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)%s \f\r> ' (date "+%H:%M:%S") (set_color brblue) \
-                $USER (prompt_hostname) (set_color $fish_color_cwd) $PWD "$pipestatus_string" \
-                $status_color $last_status (set_color normal)
+            printf '[%s] %s%s@%s %s%s %s%s%s \f\r> ' (date "+%H:%M:%S") (set_color brblue) \
+                $USER (prompt_hostname) (set_color $fish_color_cwd) $PWD $pipestatus_string \
+                (set_color normal)
     end
 end
diff --git a/share/tools/web_config/sample_prompts/informative_vcs.fish b/share/tools/web_config/sample_prompts/informative_vcs.fish
index cf16a3ca8..4da6f5172 100644
--- a/share/tools/web_config/sample_prompts/informative_vcs.fish
+++ b/share/tools/web_config/sample_prompts/informative_vcs.fish
@@ -3,7 +3,6 @@
 
 function fish_prompt --description 'Write out the prompt'
     set -l last_pipestatus $pipestatus
-    set -l last_status $status
 
     if not set -q __fish_git_prompt_show_informative_status
         set -g __fish_git_prompt_show_informative_status 1
@@ -57,6 +56,9 @@ function fish_prompt --description 'Write out the prompt'
         set -g __fish_git_prompt_color_cleanstate green --bold
     end
 
+    set -q fish_color_status
+    or set -U fish_color_status red
+
     set -l color_cwd
     set -l prefix
     set -l suffix
@@ -80,14 +82,9 @@ function fish_prompt --description 'Write out the prompt'
 
     printf '%s ' (fish_vcs_prompt)
 
-    set -l pipestatus_string (__fish_print_pipestatus "[" "] " "|" (set_color yellow) (set_color --bold yellow) $last_pipestatus)
-    echo -n "$pipestatus_string"
-
-    if not test $last_status -eq 0
-        set_color $fish_color_error
-        echo -n "[$last_status] "
-        set_color normal
-    end
+    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
 
     echo -n "$suffix "
 end