git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Add colors to the prompt for status indicators

From
Kevin Ballard <kevin@sb.org>
Date
Nov 15, 2010, 23:14 UTC
Message-ID
<F7A9874A-738C-4BEA-8EDF-0ED1FA7D6ACF@sb.org>
In-Reply-To
<19363118-D147-4946-8973-B5DDA4B00985@sb.org>
On Nov 15, 2010, at 2:52 PM, Kevin Ballard wrote:
Show 7 quoted lines
>> 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
Previous: Kevin BallardNext: Sebastien Douche
Message 5 of 9 in “Add colors to the prompt for status indicators”
  1. Sebastien DoucheOct 31, 2010
  2. Kevin BallardOct 31, 2010
  3. Andrew SayersNov 14, 2010
  4. Kevin BallardNov 15, 2010
  5. Kevin BallardNov 15, 2010
  6. Sebastien DoucheNov 16, 2010
  7. Kevin BallardNov 16, 2010
  8. Michael J GruberNov 16, 2010
  9. Kevin BallardNov 16, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.