1

I have the following set in my .zshrc

autoload -Uz add-zsh-hook vcs_info
setopt prompt_subst
add-zsh-hook precmd vcs_info

zstyle ':vcs_info:git:*' formats '%b'

and this in my custom zsh theme :

vcs_info_wrapper() {
  vcs_info
  if [[ "${vcs_info_msg_0_}" == "master" ]]; then
    echo "%{$FG[196]%}"
  else
    echo "%{$fg[cyan]%}"
  fi
}

PROMPT=$'%B%{$FG[039]%}%n%b%{$fg_bold[white]%}@%m%{$FG[220]%} %{\x1b[3m%}%5~ %{$reset_color%}$(git_prompt_info)%{$reset_color%}%{\x1b[0m%} %(?.%{$fg[white]%}.%{$fg_bold[red]%}[%?])» %{$FG[010]%} ||$(vcs_info_wrapper)${vcs_info_msg_0_}|| '

I have both $(vcs_info_wrapper)${vcs_info_msg_0_} and $(git_prompt_info) to test the colour output. For some reason the former always works and has the correct colour, but the latter doesn't and once the colour changes it never resets. I've basically tried everything at this point. Any ideas are welcome

EDIT:

Thanks @Gairfowl to I have most of it working now with:

function my_precmd {
  vcs_info
  local user='%B%F{#00ACE6}%n%f%b'

  local host='%B%F{white}@%m%f%b'
  local path='%F{#FFD700}%4~%f'
  local rcAndArrow='%(?.%F{white}.%B%F{red}[%?])»%f%b'

  local git2color='cyan'
  local git2=""

  [[ "${vcs_info_msg_0_}" == "master" || "${vcs_info_msg_0_}" == "main" ]] && git2color='196'

  if [[  "${vcs_info_msg_0_}" != "" ]]
    then
      local git2="%B%F{${git2color}}($(git_prompt_info))%f%b "
  fi

  psvar[1]="${user}${host} ${path} ${git2}${rcAndArrow} "

However I don't get any git information from $(git_prompt_info) If I combine it with path (like this local path="%F{#FFD700}%4~%f $(git_prompt_info)") that seems to work.

Rhahkeem
  • 15
  • 5
  • I doubt this will fix your problem, but using `%F` and `%f` is simpler and cleaner than embedding terminal-specific escape sequences that need to be wrapped in `%{...%}`. – chepner Oct 23 '21 at 13:49
  • Could you give a little more detail. I'm mostly using `%F` and only using escape sequences for bolding the prompt. If there's somewhere I can improve on it I'll gladly try it out – Rhahkeem Oct 26 '21 at 20:50
  • You don't appear to be using `%F` at all. You're using, for example, `$FG[039]`, which I assume contains some sort of ANSI escape sequence. – chepner Oct 26 '21 at 21:00
  • ahh thanks for pointing that out. will work on that – Rhahkeem Oct 26 '21 at 21:25
  • There's a bit more on `%F` and `$fg` here: https://unix.stackexchange.com/a/580488/432774 and in the 'Expansion of prompt sequences' / 'visual effects' section of the `zshmisc` man page. – Gairfowl Oct 27 '21 at 00:41
  • thanks that was really helpful – Rhahkeem Oct 30 '21 at 22:28

1 Answers1

1

It's often much easier to read and debug a precmd function than to put everything in the PROMPT variable. Try building your prompt like the function below; you can comment out pieces and isolate the parts you're working on:

autoload -Uz add-zsh-hook vcs_info
setopt prompt_subst
add-zsh-hook precmd my_precmd

zstyle ':vcs_info:git:*' formats '%b'

function my_precmd {
  local theUser='%B%F{39}%n%f%b'
  local theHost='%B%F{white}@%m%f%b'
  local git1="%F{220}~%f$(git_prompt_info)"
  local rcAndArrow='%(?.%F{white}.%B%F{red}[%?])»%f%b'

  vcs_info
  local git2color='cyan'
  [[ "${vcs_info_msg_0_}" == "master" ]] && git2color='196'
  local git2="||%F{${git2color}}${vcs_info_msg_0_}%f||"

  psvar[1]="${theUser}${theHost} ${git1} ${rcAndArrow} "
  psvar[2]="${git2}"
}

PROMPT='${psvar[1]}'
RPROMPT='${psvar[2]}'
Gairfowl
  • 2,226
  • 6
  • 9
  • I'll try that out. Should this be in the `.zshrc` or in my theme file? (I assume the `.zshrc because of the autoload but wanna make sure) On that note is it better practice to have it in the `.zshrc` directly? – Rhahkeem Oct 26 '21 at 20:52
  • Start with everything in `~/.zshrc` - it'll be easier to debug. Theme files are useful if you frequently switch between multiple prompts and such, but IMO they're usually more effort to manage than they are worth. – Gairfowl Oct 27 '21 at 00:35
  • that was definitely helpful. I almost have it working. I noticed that `local git1="%F{#FFD700}%4~%f $(git_prompt_info)"` works when added to my prompt but separating it out like this : `local path='%F{#FFD700}%4~%f'` and `local git1="$(git_prompt_info)"` `psvar[1]="${user}${host} ${path} ${git1} ${rcAndArrow} "` doesn't. It leaves off any git info. Any idea why that would be? – Rhahkeem Oct 30 '21 at 22:30
  • `path` is a reserved variable in `zsh` - it's the array version of the `PATH` scalar variable. It's part of neat zsh-ism where variables can be 'tied' together with `typeset -T` (there's a [bit more info here](https://stackoverflow.com/a/18077919/9307265)). Your assignment is causing problems for the `get_prompt_info` code; use a different name (e.g. `thePath`) and it should be ok. (Also, I should have avoided `user` and `host` - I updated the answer). – Gairfowl Oct 31 '21 at 05:10
  • Yupp that was it. Amazing help! Thanks – Rhahkeem Oct 31 '21 at 16:12