To get a prompt which indicates Git-branch in Zsh

前端 未结 9 1887
滥情空心
滥情空心 2020-12-04 09:12

I run the following codes separately as my prompt unsuccessfully in .zshrc. This suggests me that apparently I do not have a program called __git_ps1. It is not in MacPorts.

9条回答
  •  再見小時候
    2020-12-04 09:38

    I just redid mine since we have long branch names at work. This one will truncate with an ellipsis if it's more than 35 characters.

    parse_git_branch() {
        git_status="$(git status 2> /dev/null)"
        pattern="On branch ([^[:space:]]*)"
        if [[ ! ${git_status} =~ "(working (tree|directory) clean)" ]]; then
            state="*"
        fi
        if [[ ${git_status} =~ ${pattern} ]]; then
          branch=${match[1]}
          branch_cut=${branch:0:35}
          if (( ${#branch} > ${#branch_cut} )); then
              echo "(${branch_cut}…${state})"
          else
              echo "(${branch}${state})"
          fi
        fi
    }
    
    setopt PROMPT_SUBST
    PROMPT='%{%F{blue}%}%9c%{%F{none}%}$(parse_git_branch)$'
    

    (I'm embarrassed at how proud I am of this.)

提交回复
热议问题