1

I want to have two different format for displaying vcs_info on my zsh prompt based on the vcs_info variable hook_com[staged]

Here is the important part of my .zshrc

plugins=(git)
autoload -Uz vcs_info

tag=""
zstyle ':vcs_info:git:*' check-for-staged-changes true
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked

+vi-git-untracked(){
    if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
        git status --porcelain | grep ' M ' &> /dev/null ; then
        hook_com[staged]+='M'
        tag="OK"
    else
        tag=""
    fi
}
precmd() {
  vcs_info
    if [[ -n ${tag} ]] ; then
      zstyle ':vcs_info:git:*' formats "[%s@%b(%c%m)]"
    else
      zstyle ':vcs_info:git:*' formats "[%s@%b]"
    fi
}

The expected result is to have the vcs information (for git) look like:

  • [git@master] // No modified files
  • [git@master(M)] // Modified files

Which work fine unless I just made the change:

[20/01-10:31]<zmx@bgtian:~/misc[git@master]>% echo "aa" >> README.md
[20/01-10:32]<zmx@bgtian:~/misc[git@master]>% cd . // on this line I should have seen the change.
[20/01-10:32]<zmx@bgtian:~/misc[git@master(M)]>%

Did I miss something with the precmd or the hooks on vcs_info ?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • If you don't do that `cd .` but just hit enter, does it change? Also you don't need the whole oh-my-zsh , you can just use `zsh-git-prompt`, which is pretty nice – ChatterOne Jan 20 '20 at 13:04
  • I rolled back your latest edit; your question should remain strictly a question. Perhaps you want to add this as an answer instead: *"My prompt was set with double quote intead of single quote. I don't know why yet, but this fixed my issue."* – tripleee Jan 20 '20 at 13:23
  • With double quotes, any variables and command interpolations are evaluated immediately. Single quotes prevent the shell from modifying the strings. See also https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash (though it is tagged Bash). – tripleee Jan 20 '20 at 13:25

2 Answers2

0

I'm not expert, but have you try to put vcs_info after your conditions like this:

precmd() {
    if [[ -n ${tag} ]] ; then
      zstyle ':vcs_info:git:*' formats "[%s@%b(%c%m)]"
    else
      zstyle ':vcs_info:git:*' formats "[%s@%b]"
    fi
 vcs_info
}
WaM
  • 1
  • Sadly, I got the exact same result. I'm looking at ommyzsh right now but I would prefer to have a "vanilla" solution. – Tristan Lar Jan 20 '20 at 12:51
0

My prompt was set with double quote intead of single quote. I don't know why yet, but this fixed my issue.