Skip to content

Instantly share code, notes, and snippets.

@mdeous
Last active August 29, 2015 13:57
Show Gist options
  • Save mdeous/9628636 to your computer and use it in GitHub Desktop.
Save mdeous/9628636 to your computer and use it in GitHub Desktop.
#!/bin/bash
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
GIT_BRANCH_RE="^# On branch ([^${IFS}]*)"
GIT_REMOTE_RE="# Your branch is (.*) '"
GIT_GAP_RE="# Your branch is .* ([0-9]+) commits?\."
GIT_DIVERGE_RE="# Your branch and (.*) have diverged"
function gen_prompt {
if [ ! -d "$(pwd)/.git" ]; then
git_infos=""
else
git_status="$(git status 2> /dev/null)"
# Handle uncommited changes
if [[ ! ${git_status} =~ "working directory clean" ]]; then
state="${RED}⚡"
else
state=""
fi
# Handle commits gap with remote
if [[ ${git_status} =~ ${GIT_REMOTE_RE} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead of" ]]; then
remote="${YELLOW}↑"
remote_sign="+"
else
remote="${YELLOW}↓"
remote_sign="-"
fi
if [[ ${git_status} =~ ${GIT_GAP_RE} ]]; then
remote="${remote}${remote_sign}${BASH_REMATCH[1]}"
fi
else
remote=""
fi
# Handle diverged branches
if [[ ${git_status} =~ ${GIT_DIVERGE_RE} ]]; then
remote="${YELLOW}↕"
fi
# Generate git informations summary
if [[ ${git_status} =~ ${GIT_BRANCH_RE} ]]; then
branch=${BASH_REMATCH[1]}
git_infos="(${branch})${remote}${state}"
else
git_infos=""
fi
fi
# Last command's return value
if [ $? -eq 0 ]; then
last_ret="${GREEN}$?↩${COLOR_NONE}"
else
last_ret="${RED}$?↩${COLOR_NONE}"
fi
date_prefix="(${YELLOW}$(date +'%H:%M:%S')${COLOR_NONE})"
main_part="${YELLOW}\u@\h${LIGHT_GRAY}:${BLUE}\w"
PS1="${date_prefix}${LIGHT_GRAY}[${main_part}${GREEN}${git_infos}${LIGHT_GRAY}]${COLOR_NONE}${last_ret} \n\$ "
}
PROMPT_COMMAND="gen_prompt"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment