Skip to content

Instantly share code, notes, and snippets.

@zerowidth
Created September 22, 2009 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerowidth/191512 to your computer and use it in GitHub Desktop.
Save zerowidth/191512 to your computer and use it in GitHub Desktop.
bash promptery for git + custom colored dirty flags
# assuming you have the +bash_completion variant of git installed (macports)
# and are including bash completion's setup:
if [ -f /opt/local/etc/bash_completion ]; then
. /opt/local/etc/bash_completion
fi
TEXT_BLACK='\[\e[0;30m\]' # Black - Regular
TEXT_RED='\[\e[0;31m\]' # Red
TEXT_GREEN='\[\e[0;32m\]' # Green
TEXT_YELLOW='\[\e[0;33m\]' # Yellow
TEXT_BLUE='\[\e[0;34m\]' # Blue
TEXT_PURPLE='\[\e[0;35m\]' # Purple
TEXT_CYAN='\[\e[0;36m\]' # Cyan
TEXT_WHITE='\[\e[0;37m\]' # White
TEXT_RESET='\[\e[0m\]' # Text Reset
# default:
# PS1="\h:\W \u\$ "
previous_exit_status() {
if [ $1 -eq 0 ]; then
# HEAVY ROUND-TIPPED RIGHTWARDS ARROW
echo -n "➜"
else
# HEAVY BALLOT X
echo -n "${TEXT_RED}✘${TEXT_RESET}"
fi
}
# good bits are all via git-completion.bash from git.
# using custom function to allow for colors instead of the
# GIT_PS1_SHOWDIRTYSTATE
# GIT_PS1_SHOWSTASHSTATE
# GIT_PS1_SHOWUNTRACKEDFILES
# environment variables.
git_dirty_flag() {
if [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
# modifications of tracked files
# git diff-files --no-ext-diff --ignore-submodules --exit-code --quiet \
# now, from __git_ps1:
git diff --no-ext-diff --ignore-submodules --quiet --exit-code \
|| echo -n "${TEXT_YELLOW}*${TEXT_RESET}"
# staged hunks
if git rev-parse --quiet --verify HEAD >/dev/null; then
git diff-index --no-ext-diff --ignore-submodules --cached --exit-code HEAD --quiet \
|| echo -n "${TEXT_GREEN}+${TEXT_RESET}"
fi
# untracked files
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
echo -n "${TEXT_CYAN}?${TEXT_RESET}"
fi
# stashed changes
git rev-parse --verify refs/stash >/dev/null 2>&1 && echo -n "${TEXT_PURPLE}\$${TEXT_RESET}"
fi
}
set_prompt(){
previous=$?;
PS1="${TEXT_GREEN}\w${TEXT_RESET}$(__git_ps1)$(git_dirty_flag) $(previous_exit_status $previous) "
}
PROMPT_COMMAND=set_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment