agkozak-zsh-prompt icon indicating copy to clipboard operation
agkozak-zsh-prompt copied to clipboard

Request: display pipestatus when any exit codes are non-zero

Open AndydeCleyre opened this issue 3 years ago • 5 comments

I'm looking for a change like the following (but this doesn't work and I'm not sure why):

- AGKOZAK[PROMPT]+='%(?..%B%F{${AGKOZAK_COLORS_EXIT_STATUS:-red}}(%?%)%f%b )'
+ AGKOZAK[PROMPT]+='${${pipestatus:#0}:+%B%F{${AGKOZAK_COLORS_EXIT_STATUS:-red}\}(${(j:|:)pipestatus})%f%b }'

Here's how something similar looks in p10k:

image

I don't know if it would be better to implement in a way that can be toggled or not; I would want this always enabled.

AndydeCleyre avatar Jun 03 '21 16:06 AndydeCleyre

I just got back from a trip; I'll try to come up with an answer for you in the next few days.

agkozak avatar Jun 12 '21 15:06 agkozak

@AndydeCleyre I just realized that I never got back to you about this! Forgive me.

I think what you're looking for is

AGKOZAK_CUSTOM_PROMPT='${${pipestatus#0}:+%B%F{red\}(${"${pipestatus[*]}"// /|})%f%b } '

In other words, if pipestatus is a single zero, print nothing; otherwise treat the array pipestatus as a space-delimited string, replace the spaces with vertical bars, and make the whole thing bold and red.

I like that so much that I think I'll use it in my custom prompt. If I still like it in a few weeks, perhaps I'll make it a standard feature.

agkozak avatar Oct 17 '21 21:10 agkozak

I just realized it's not that simple, is it? The prompt prints correctly, but then pipestatus becomes 0 again and the string disappears.

Let me look into this a bit more deeply.

agkozak avatar Oct 17 '21 21:10 agkozak

OK, here's the code you can use until I build this feature into the prompt:

_andy_pipestatus() {
    typeset -g ANDY_PIPESTATUS="${(%)${${pipestatus#0}:+%B%F{${AGKOZAK_COLORS_EXIT_STATUS}\}(${"${pipestatus[*]}"// /|})%f%b }}"
  }
 autoload -Uz add-zsh-hook
 add-zsh-hook precmd _andy_pipestatus

and then add to your custom prompt

AGKOZAK_CUSTOM_PROMPT='${ANDY_PIPESTATUS}'

Later I’ll make it so that it changes colors.

agkozak avatar Oct 17 '21 22:10 agkozak

Here we go: green when the last result is 0, red otherwise:

_andy_pipestatus() {
    typeset -g ANDY_PIPESTATUS="${${pipestatus#0}:+(${"${pipestatus[*]}"// /|})}"
    [[ -z $ANDY_PIPESTATUS ]] && return
    if [[ $ANDY_PIPESTATUS == *0\) ]]; then
      typeset -g ANDY_PIPESTATUS="%F{108}${ANDY_PIPESTATUS}%f " 
    else
      typeset -g ANDY_PIPESTATUS="%B%F{${AGKOZAK_COLORS_EXIT_STATUS}\}${ANDY_PIPESTATUS}%f%b "
    fi
  }
  autoload -Uz add-zsh-hook
  add-zsh-hook precmd _andy_pipestatus

agkozak avatar Oct 18 '21 02:10 agkozak