Skip to content

Instantly share code, notes, and snippets.

@mrinalwadhwa
Last active August 3, 2016 17:22
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 mrinalwadhwa/a80c9a10ace1893673d089ac120fb0d5 to your computer and use it in GitHub Desktop.
Save mrinalwadhwa/a80c9a10ace1893673d089ac120fb0d5 to your computer and use it in GitHub Desktop.
function prompt_color(){
case $# in
3) echo -en "\[\033[$2\]\[\033[$3\]\[$1\033[0m\]";;
2) echo -en "\[\033[$2\]$1\[\033[0m\]";;
1) echo -en "$1";;
*) echo -en "";;
esac
}
function git_branch(){
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo "${ref#refs/heads/}"
}
function git_is_dirty(){
test -n "$(git status --porcelain)"
}
function build_prompt(){
local last_command=$? # Capture exit status of teh last command
PS1="\n" # Start with a line break
# If the last command failed
if [[ "$last_command" != "0" ]]; then
# Add a red prefix to the prompt
PS1+="$(prompt_color " " "31m" "41m" )"
fi
# If the current folder is a git repo
if [ "$(git_branch)" != "" ]; then
local fg2="30m"
local bg2="46m"
local fg21="36m"
local bg21="40m"
if git_is_dirty; then
# if there are uncommitted changes, make the git branch part brighter
bg2="47m"
fg21="37m"
fi
# Add the current git branch to the prompt
PS1+="$(prompt_color "  \$(git_branch) " "$fg2" "$bg2" )"
PS1+="$(prompt_color "" "$fg21" "$bg21" )"
fi
local fg1="m"
local bg1="40m"
local fg10="30m"
local fg0="m"
# Show the current path
PS1+="$(prompt_color " \w " "$fg1" "$bg1" )$(prompt_color "" "$fg10") "
PS1+="\n\n$(prompt_color "» " "$fg0")"
PS2="$(prompt_color "» " "$fg0")"
}
# http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html
# http://stackoverflow.com/a/16343345
export PROMPT_COMMAND='build_prompt'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment