BOLD="\[$(tput bold)\]"
BLACK="\[$(tput setaf 0)\]"
RED="\[$(tput setaf 1)\]"
GREEN="\[$(tput setaf 2)\]"
YELLOW="\[$(tput setaf 3)\]"
BLUE="\[$(tput setaf 4)\]"
MAGENTA="\[$(tput setaf 5)\]"
CYAN="\[$(tput setaf 6)\]"
WHITE="\[$(tput setaf 7)\]"
RESET="\[$(tput sgr0)\]"

function prompt_command {
    RET=$?
    if [[ "$(id -u)" -eq 0 ]]; then
        PS1="$BOLD$RED"
    else
        PS1="$GREEN"
    fi

    branch="$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/')"
    PS1+="\u$RESET:$YELLOW\w$RESET$CYAN$branch$RESET "

    if [[ "$RET" -eq 0 ]]; then
        PS1+="$BOLD$GREEN"
    else
        PS1+="$RET $BOLD$RED"
    fi
    PS1+="\\$ $RESET"
    export PS1
}
export PROMPT_COMMAND=prompt_command
