这大部分都有效,我能够根据git状态为我的bash提示添加颜色。如果一切都被提交,(原点/主人)将是绿色的,如果没有,那么我知道我还没有做出承诺。但是,我尝试添加其他颜色,例如黄色以及未跟踪的更改'如果我没有做任何事情,那就是红色。
这是我迄今为止在我的.bashrc中的各个帖子拼凑到的东西
Bash版本:4.3.48(1)-release
OS: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颜色代码?感谢
答案 0 :(得分:0)
通过使用来自Benjamin W的git status --porcelain建议然后重做if语句将COLOURGREEN作为最终的其他内容,我现在有一个根据git状态改变颜色的提示!好哇。将上面的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
}