How to change color of prompt from last exit code in ZSH?
Asked Answered
B

1

6

I have this piece of code in my .zshrc:

autoload -U colors && colors

function change_color() {
    if [[ "$?" = 0 ]]; then
    return "green"
  else
    return "red"
  fi
}

PS1="%B%{$fg[red]%}[%{$fg[yellow]%}%n%{$fg[green]%}@%{$fg[blue]%}%M %{$fg[magenta]%}%~%{$fg[red]%}]% %{$fg[$changecolor]%}➜%b "

However, the character stays white, no matter what I do.

Button answered 14/1, 2022 at 1:35 Comment(2)
You are using a variable $changecolor, which never gets assigned. But even if you would it assign to some value, PS1 would get this value at the time the PS1 variable is defined, and would never change afterwards.Godfry
Instead of reinventing the wheel, you could for instance install spaceship or at least look at its source code to learn how the Prompt change based on the status is implemented there.Godfry
A
8

As a function, you need to call it, not expand it as a variable. Do that with a command substitution:

PS1="...%{$fg[\$(changecolor)]%}➜%b "

However, there's a lot of bash-like stuff in this prompt that you can replace with simpler, more robust zsh features. First, %F can be used to change the color directly, without using escape codes stored in an array. (Unlike raw escape codes, and like other zsh-defined escape sequences, %F{...} doesn't need to be wrapped in %{...%}.) Second, there is prompt escape specifically for producing one value or another depending on whether the last exit status was 0 or not.

PS1='%B%F{red}[%F{yellow}%n%F{green}@%F{blue}%M '
PS1+='%F{magenta}%~%F{red}] %(?.%F{green}.%F{red})➜%b%f '

%(?.FOO.BAR) expands to FOO if the last command succeeded, BAR otherwise. %f restores the color to the default, regardless of any preceding %F codes.


%(...) is, in fact, more general than just testing the exit code. ? is just one of 20 or so testable conditions. As a silly example, suppose your birthday is coming up on January 18. Here's an escape to put a cake in your prompt on your birthday.

%(0D.%(18d.🎂.).)

More information on prompt escape codes can be found in man zshmisc.

Argive answered 14/1, 2022 at 14:41 Comment(1)
Thank you so much, this really helped me and solves many hours or torment over this :). My prompt is perfect now, thank you so muchButton

© 2022 - 2024 — McMap. All rights reserved.