简体   繁体   English

如何在bash PROMPT中为当前的git分支添加颜色?

[英]How can I add color to the current git branch in my bash PROMPT?

I'm trying to add some color, in my terminal prompt, to the current git branch. 我试图在终端提示符下向当前的git分支添加一些颜色。

My prompt : 我的提示:

export PS1="
\e[32m\u@\h \e[36m\w \`parse_git_branch\`
\\e[0m$ "

So as you could probably guess, it displays something like that : user@host path/to/dir [git branch[ !]] . 因此,您可能会猜到,它显示如下内容: user@host path/to/dir [git branch[ !]] [git branch] is displayed only if the current folder is a git directory. 仅当当前文件夹是git目录时,才会显示[git branch]。 The exclamation mark ! 感叹号! is displayed only if there are changes on the current branch. 仅在当前分支上有更改时才显示。

So, I would like to add color (red) to the git branch, but only if there are changes to it. 因此,我想向git分支添加颜色(红色),但前提是要对其进行更改。 I can't add \\e[31m just before the parse_git_branch function in the prompt declaration, because it doesn't give me the possibility to use the default color when their are no changes, the name of the branch would always be displayed in red. 我不能在提示声明中的parse_git_branch函数之前添加\\e[31m ,因为当它们不变时,它不能给我提供使用默认颜色的可能性,因此分支的名称将始终显示为红色。 。

So I have to modify the function itself. 所以我必须修改函数本身。 Here it is : 这里是 :

function parse_git_branch() {
        BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
        if [ ! "${BRANCH}" == "" ]
        then
                STAT=`parse_git_dirty`
                echo "[${BRANCH}${STAT}]"
        else
                echo ""
        fi
}

I tried loads of different things. 我尝试了很多不同的事情。 Like declaring the color \\033[31m right before [${BRANCH}${STAT}] in the echo statement. 就像在echo语句中的[${BRANCH}${STAT}]之前声明\\033[31m颜色。 The problem with doing it that way is that, like before, the color will also be red when there are no changes. 这样做的问题在于,像以前一样,当没有更改时,颜色也会变为红色。

And here is the parse_git_dirty function : 这是parse_git_dirty函数:

function parse_git_dirty {
        status=`git status 2>&1 | tee`
        dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
        untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
        ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
        newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
        renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
        deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
        bits=''
        if [ "${renamed}" == "0" ]; then
                bits=">${bits}"
        fi
        if [ "${ahead}" == "0" ]; then
                bits="*${bits}"
        fi
        if [ "${newfile}" == "0" ]; then
                bits="+${bits}"
        fi
        if [ "${untracked}" == "0" ]; then
                bits="?${bits}"
        fi
        if [ "${deleted}" == "0" ]; then
                bits="x${bits}"
        fi
        if [ "${dirty}" == "0" ]; then
                bits="!${bits}"
        fi
        if [ ! "${bits}" == "" ]; then
                echo " ${bits}"
        else
                echo ""
        fi
}

What do you suggest I should try ? 您建议我应该尝试什么?

Thanks. 谢谢。

So, you are running parse_git_dirty and pretty much appending it to the branch name. 因此,您正在运行parse_git_dirty并将其几乎附加到分支名称。 Instead, you can provide a branch name to the function and wrap it with different "color code" strings based on the status. 相反,您可以为函数提供分支名称,并根据状态用不同的“颜色代码”字符串包装它。

这将更改您的提示,使其不仅显示您的工作目录,而且显示您当前的git分支。

export PS1="\\w:\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\$ "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM