agkozak / agkozak-zsh-prompt

A fast, asynchronous Zsh prompt with color ASCII indicators of Git, exit, SSH, virtual environment, and vi mode status. Framework-agnostic and customizable.
MIT License
290 stars 26 forks source link

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

Open AndydeCleyre opened 3 years ago

AndydeCleyre commented 3 years ago

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.

agkozak commented 3 years ago

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

agkozak commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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