[英]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.