Reputation: 40319
I'm trying to convince my bash prompt to add coloration when my current repo has uncommitted changes of any sort.
git_status() {
printf "%s : %s" $(git rev-parse --abbrev-ref HEAD) $(git rev-parse --short=10 HEAD)
}
has_changed() {
if [[ -n `git diff HEAD` ]]; then
printf " - %s Δ %s" ${Green} ${Color_Off}
fi
}
PS1=${Purple}"\w"${Color_Off}" @ \$(git_status) \$(has_changed)\n \$ "
This partially works, but the has_changed function returns escaped characters rather than colors:
~/projects/project @ master : 8675309 - \033[0;32m Δ \033[0m
Version of bash: GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
, on OSX Mavericks.
Upvotes: 1
Views: 520
Reputation: 4370
In order to get the status into your prompt you would need to use the PROMPT_COMMAND
environment variable.
PROMPT_COMMAND
If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).
Here is a simple example (tested and works) which you could adapt to your needs:
PROMPT_COMMAND='echo -ne $(if [[ -n `git diff HEAD` ]]; then echo true; fi)'
Also related: What is the difference between PS1 and PROMPT_COMMAND
Upvotes: 1