繁体   English   中英

快好了! 如何在bash提示中添加另一种git status颜色?

[英]Almost there! How can I add another git status colour to bash prompt?

多数情况下,我可以根据git状态将颜色添加到bash提示中。 如果所有内容都已提交,则(来源/主)将为绿色,否则,我知道我尚未提交。 但是我正在尝试添加其他颜色,例如黄色表示“未跟踪的变化”,红色表示如果没有做任何事情。

到目前为止,这是我在.bashrc中的各种帖子中所拼凑的内容

Bash版本:4.3.48(1)-发布

操作系统:Linux Mint 18.2

## trim to two dir depth
PROMPT_DIRTRIM=2

## green user@hostname, then blue dirs, then colours for git branch

COLOURGREEN="\033[01;32m"
COLOURBLUE="\033[01;34m"
COLOURPLAIN="\033[m"
COLOURRED="\033[1;31m"
COLOURYELLOW="\033[1;33m"

## This works fine on it's own, I see the (origin/master) in prompt
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

## green works when all files committed but not other colours
git_colour() {

    local gitstatus="$(git status 2> /dev/null)"

    if [[ ! $gitstatus =~ "working directory clean" ]]; then
        echo -e $COLOURRED
    elif [[ $gitstatus =~ "Untracked files:" ]]; then
        echo -e $COLOURYELLOW
    elif [[ $gitstatus =~ "nothing to commit" ]]; then
        echo -e $COLOURGREEN
    else
        echo -e $COLOURPLAIN
fi
}

## working export without colour on git
# Example: user@hostname ~/.../dir3/dir4 (origin/master)
# export PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[m\]\$(parse_git_branch) $ "

## works only when green
export PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\$(git_colour)\]\$(parse_git_branch)\[\033[m\] $ "

git_colour()只是一点帮助,或者如果我搞砸了bash颜色代码? 谢谢

通过使用本杰明W的git status --porcelain建议,然后重做if语句以将COLOURGREEN用作其他词,我现在有一个提示,可以根据git status更改颜色! 哈萨 将上面的git_colour()更改为:

git_colour() {


    local gitstatus="$(git status --porcelain 2> /dev/null)"

    if [[ $gitstatus =~ "??" ]]; then
        echo -e $COLOURRED  
    elif [[ $gitstatus =~ "A" ]]; then
        echo -e $COLOURYELLOW
    else
        echo -e $COLOURGREEN
    fi
}

暂无
暂无

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

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