2

I'm using a slightly modified version of Gary Bernhardt's (destroyallsoftware.com) .bashrc with homebrew bash 3.2.51.

    #  . ~/bin/bash_colors.sh
    . ~/bin/colorize.sh

[... snipped ...]

    # Git prompt components
    function minutes_since_last_commit {
        now=`date +%s`
        last_commit=`git log --pretty=format:'%at' -1`
        seconds_since_last_commit=$((now-last_commit))
        minutes_since_last_commit=$((seconds_since_last_commit/60))
        echo $minutes_since_last_commit
    }
    grb_git_prompt() {
        local g="$(__gitdir)"
        if [ -n "$g" ]; then
            local MINUTES_SINCE_LAST_COMMIT=`minutes_since_last_commit`
            if [ "$MINUTES_SINCE_LAST_COMMIT" -gt 30 ]; then
                local COLOR=${txtred}
            elif [ "$MINUTES_SINCE_LAST_COMMIT" -gt 10 ]; then
                local COLOR=${txtylw}
            else
                local COLOR=${txtgrn}
            fi
            local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)${reset}"
            # The __git_ps1 function inserts the current git branch where %s is
            # *** richard -- added purple to branch name


             local GIT_PROMPT=`__git_ps1 "[\033${txtgrn}%s\033${reset}|\033${SINCE_LAST_COMMIT}]"`
            echo ${GIT_PROMPT}
        fi
    }
    PS1="\[${txtblu}\u\[${txtmag}@\h\[${reset}:\[\W\]\[\$(grb_git_prompt)\$ "

[... snipped ...]

For some reason, my prompt does this:

enter image description here

Until I do:

$ bash

at which point it does this:

enter image description here

This exact same .bashrc and associated scripts work perfectly on my arch box.

$ echo $PS1
\[\u\[@\h\[:\[\W\]\[$(grb_git_prompt)$

The same thing happens in both Terminal.app and iTerm2. Any ideas?

2
  • Perhaps you could comment pieces of the .bashrc file to find the culprit. Could you also post the contents of the files that are being sourced from your .bashrc?
    – mtak
    May 1, 2014 at 11:18
  • As @glennjackman spotted in his answer, you make a second unnecessary call to minutes_since_last_commit which can be replaced by the contents of variable $MINUTES_SINCE_LAST_COMMIT. This should halve the response time of your bash prompt.
    – Lqueryvg
    May 1, 2014 at 16:32

2 Answers 2

0

Without knowing exactly the contents of your colour variables, I think you need to change this

PS1="\[${txtblu}\u\[${txtmag}@\h\[${reset}:\[\W\]\[\$(grb_git_prompt)\$ "

to this

PS1="\[\033${txtblu}\]\u\[\033${txtmag}\]@\h\[\033${reset}\]:\W\$(grb_git_prompt)\$ "

And change this

local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)${reset}"
local GIT_PROMPT=`__git_ps1 "[\033${txtgrn}%s\033${reset}|\033${SINCE_LAST_COMMIT}]"`
echo ${GIT_PROMPT}

to

local SINCE_LAST_COMMIT="\[\033${COLOR}\]${MINUTES_SINCE_LAST_COMMIT}\[\033${reset}\]"
echo "$(__git_ps1 "\[\033${txtgrn}\]%s\[\033${reset}\]|${SINCE_LAST_COMMIT}")"

The colour codes all have to be individually surrounded by \[...\]

1
  • 1
    Note that @glenn jackman changed $(minutes_since_last_commit) to ${MINUTES_SINCE_LAST_COMMIT} in his answer. This is a good thing. It should improve the prompt response time.
    – Lqueryvg
    May 1, 2014 at 16:43
0

My suspicion is that your colour definitions (which I can't see in your question) already have the surrounding \[ and \] characters. I.e. your yellow is something like this:

txtylw="\[\033[0;37m\]"

And this is what's causing the confusion and the problem.

The PS1 prompt needs those surrounding characters, but the colours returned by your grb_git_prompt() function must not, because this function gets called every time you press Enter and it's output will get inserted literally into your prompt.

Put these at the top of your grb_git_prompt() function to see if I'm right:

local txtred="\033[0;31m"
local txtylw="\033[0;37m"
local txtgrn="\033[0;32m"
local reset="\033[0m"

P.S. I'm not sure if my actual colour numbers are correct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .