Bash colorize with Git hightlighting

Tip to show you current branch if in repository, applied and applied-modified files in commit and modified files, but not added to the commit.
function __prompt_newline {
    # get cursor position and add new line if we're not in first column
    exec < /dev/tty
    local OLDSTTY=$(stty -g)
    stty raw -echo min 0
    echo -en "\033[6n" > /dev/tty && read -sdR CURPOS
    stty $OLDSTTY
    [[ ${CURPOS##*;} -gt 1 ]] && echo -ne '\e[31;40m↵\n\e[0m'
}

function __git_info {
    GITBRANCH="$(git branch --no-color 2>/dev/null | awk '/^[*]/{print $2}')"
    if [ "x${GITBRANCH}x" == "xx" ]; then
        return;
    fi
    echo -ne ' \e[33;3m'$GITBRANCH'\e[0m'

    GITNEWFILE="$(git status --short | sed -n '/^[AM]/p' | wc -l)"
    GITADDMODIFIED="$(git status --short | sed -n '/^[AM]M/p' | wc -l)"
    GITMODIFIED="$(git status --short | sed -n '/^ [M]/p' | wc -l)"
    [[ ${GITNEWFILE} -gt 0 || ${GITMODIFIED} -gt 0 ]] && echo -n ' | '
    [[ ${GITNEWFILE} -gt 0     ]] && echo -ne '\e[32;3m'$GITNEWFILE'\e[0m'
    [[ ${GITADDMODIFIED} -gt 0 ]] && echo -ne '/\e[32;3m'$GITADDMODIFIED'\e[0m'

    [[ ${GITNEWFILE} -gt 0 && ${GITMODIFIED} -gt 0 ]] && echo -n '+'
    [[ ${GITMODIFIED} -gt 0 ]] && echo -ne '\e[31;3m'$GITMODIFIED'\e[0m'
    echo -ne ' '
}

export PS1="$(echo -n '$(__prompt_newline)[\e[32;1m\u\e[0m@\e[31;1m\h\e[0m \W]$(__git_info)\$\e[0m ')"

Leave a Reply

Your email address will not be published. Required fields are marked *