Re: Add colors to the prompt for status indicators

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Nov 15, 2010, at 2:52 PM, Kevin Ballard wrote:

>> As a fan of colourful prompts, I'd be very happy if you found a way
>> around this for parts of a script.  But as a fan of fast prompts, I'd
>> prefer not to call __git_ps1 more than once :)
> 
> I don't think there is any way around this, besides patching bash to be
> intelligent and determine which PS1 characters are printable itself without
> relying on \[ and \].

I take it back. We can use PROMPT_COMMAND for this, to set up variables
containing what you want.

Something like the following should work:

function __populate_git_ps1_vars () {
    # this relies on a bashism, so make sure you're actually using bash.
    # specifically it relies on <<<"word".
    local status
    local prompt="$(__git_ps1 "%s")"
    # empty out the vars
    __git_ps1_branch=""
    __git_ps1_staged=""
    __git_ps1_unstaged=""
    __git_ps1_stash=""
    __git_ps1_untracked=""
    __git_ps1_upstream=""
    __git_ps1_left=""
    __git_ps1_right=""
    if test -z "$prompt"; then
        # return now
        return
    fi
    __git_ps1_left=" ("
    __git_ps1_right=")"
    __git_ps1_branch="$(git rev-parse --symbolic-full-name --abbrev-ref=loose HEAD)"
    status="${prompt#$__git_ps1_branch}"
    while read -N 1 char; do
        case "$char" in
            \*) __git_ps1_unstaged="$char" ;;
            +) __git_ps1_staged="$char" ;;
            $) __git_ps1_stash="$char" ;;
            %) __git_ps1_untracked="$char" ;;
            \<|\>) __git_ps1_upstream="${__git_ps1_upstream}$char" ;;
        esac
    done <<<"$status"
}

PROMPT_COMMAND=__populate_git_ps1_vars

PS1='\w${__git_ps1_left}${__git_ps1_branch}\[\e[31m\]${__git_ps1_staged}\[\e[34m\]${__git_ps1_unstaged}\[\e[32m\]${__git_ps1_stash}\[\e[1;34m\]${__git_ps1_untracked}\[\e[31m\]${__git_ps1_upstream}\[\e[m\]${__git_ps1_right}> '

-Kevin Ballard--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]