Reputation: 511
Currently my terminal prompt includes git information.
Example clean directory: michaelespinosa:~/Sites/example.com [git:master]
Example dirty directory: michaelespinosa:~/Sites/example.com [git:master*]
I want also to color the git information (green if it's clean or red if it's dirty).
I thought I could add a function (parse_git_color) and set it's color accordingly with an if else statement based of whether or not an asterisk is present.
The problem is that it keeps returning green for both clean and dirty directories. I believe the problem has something to do with the if statement parse_git_dirty == "*" that compares the value value of parse_git_dirty to "*".
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_color {
if [ parse_git_dirty == "*" ]; then
echo '\033[0;31m\'
else
echo '\033[0;32m\'
fi
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/$(parse_git_color)[git:\1$(parse_git_dirty)]/"
}
Any help is appreciated! Thanks
Upvotes: 1
Views: 991
Reputation: 25736
Please note the \$(...)
in PS1. It is important because only with \$()
your command is called every time your prompt is printed. $()
is called only once (when PS1 was initialized).
function parse_git_dirty
{
[[ "$(git status 2> /dev/null | tail -n1)" != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_color
{
if [ "$(parse_git_dirty)" == "*" ] ; then
echo "0;31m"
else
echo "0;32m"
fi
}
function parse_git_branch
{
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[git:\1$(parse_git_dirty)]/"
}
export PS1="\[\033[\$(parse_git_color)\]\$(parse_git_branch) \[\033[0m\]"
Upvotes: 3