Skip to content

Instantly share code, notes, and snippets.

@ryanwalder
Created March 6, 2018 10:36
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 ryanwalder/e85f7aba7011e2e7ec1a152d2bc87314 to your computer and use it in GitHub Desktop.
Save ryanwalder/e85f7aba7011e2e7ec1a152d2bc87314 to your computer and use it in GitHub Desktop.
# vi: set ft=sh :
red="38;5;1"
light_red="38;5;9"
dark_red="38;5;124"
yellow="38;5;220"
light_yellow="38;5;11"
dark_yellow="38;5;142"
blue="38;5;4"
light_blue="38;5;12"
dark_blue="38;5;25"
green="38;5;34"
light_green="38;5;2"
dark_green="38;5;35"
magenta="38;5;165"
light_magenta="38;5;5"
dark_magenta="38;5;127"
orange="38;5;130"
light_orange="38;5;202"
dark_orange="38;5;94"
white="38;5;7"
black="38;5;0"
gray="38;5;248"
reset="\[\e[0m\]"
function _show_venv_status {
if [[ -z "$VIRTUAL_ENV" ]]; then
echo -n ""
else
echo -n " \[\033[[${green}m\]$(basename $VIRTUAL_ENV)]${reset}"
fi
}
function _show_git_status {
# Get the current git branch and colourize to indicate branch state
# branch_name+ indicates there are stash(es)
# branch_name? indicates there are untracked files
# branch_name! indicates your branches have diverged
local unknown untracked stash clean ahead behind staged unstaged diverged
unknown="${dark_blue}"
untracked="${dark_green}"
stash="${light_green}"
clean="${green}"
ahead="${yellow}"
behind="${dark_yellow}"
staged="${light_orange}"
unstaged="${orange}"
diverged="${red}"
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ -n "$branch" ]; then
git_status=$(git status 2> /dev/null)
# If nothing changes the colour, we can spot unhandled cases.
colour=$unknown
if echo $git_status | grep -qe 'Untracked files'; then
colour=$untracked
branch="${branch}?"
fi
if git stash show &>/dev/null; then
colour=$stash
branch="${branch}+"
fi
if echo $git_status | grep -qe 'working directory clean'; then
colour=$clean
fi
if echo $git_status | grep -qe 'Your branch is ahead'; then
colour=$ahead
fi
if echo $git_status | grep -qe 'Your branch is behind'; then
colour=$behind
fi
if echo $git_status | grep -qe 'Changes to be committed'; then
colour=$staged
fi
if echo $git_status | grep -qe 'Changed but not updated' \
-e 'Changes not staged'; then
colour=$unstaged
fi
if echo $git_status | grep -qe 'Your branch.*diverged'; then
colour=$diverged
branch="${branch}!"
fi
echo -n " \[\033[${colour}m\](${branch})\[\033[0m\]"
fi
return 0
}
function _build_prompt {
git_status=$(_show_git_status)
venv_status=$(_show_venv_status)
PS1="\u@\h${venv_status}${git_status} \w\$ "
return 0
}
PROMPT_COMMAND="_build_prompt; $PROMPT_COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment