Re: [PATCH 19/19] bash prompt: alternative git prompt without command substitution
- From
SZEDER Gábor <szeder@ira.uka.de>
- Date
- May 9, 2012, 22:08 UTC
- Message-ID
- <20120509220844.GG6958@goldbirke>
- In-Reply-To
- <4FAAC7AE.3020002@pileofstuff.org>
Hi,
On Wed, May 09, 2012 at 08:38:22PM +0100, Andrew Sayers wrote:
Show 7 quoted lines
> On 09/05/12 01:44, SZEDER Gábor wrote: > > PROMPT_COMMAND=__git_prompt_command > > Rather than overwrite any existing PROMPT_COMMAND, it would be better to > do something like: > > PROMPT_COMMAND="__git_prompt_command; $PROMPT_COMMAND"
Show 6 quoted lines
> > +# Alternatively, to make the above Bash prompt a bit faster: > > +# PROMPT_COMMAND=__git_prompt_command > > As above, I'd recommend a simple documentation change: > PROMPT_COMMAND="__git_prompt_command; $PROMPT_COMMAND" > (to show people how to chain any other prompt commands they have)
That's a good idea. In fact I did manage to overwrite my $PROMPT_COMMAND and was wondering why did the title of my terminal windows disappear so suddenly...
Show 26 quoted lines
> > @@ -365,7 +371,18 @@ __git_ps1 ()
> > fi
> >
> > local f="$w$i$s$u"
> > - printf -- "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r$p"
> > + __git_ps1_string="$c${b##refs/heads/}${f:+ $f}$r$p"
> > +}
> > +
> > +# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
> > +# returns text to add to bash PS1 prompt (includes branch name)
> > +__git_ps1 ()
> > +{
> > + local __git_ps1_string
> > + __git_prompt_command
> > + if [ -n "$__git_ps1_string" ]; then
> > + printf -- "${1:- (%s)}" "$__git_ps1_string"
> > + fi
>
> How hard/appropriate would it be to export individual parts of the
> prompt here? Something like:
>
> __git_ps1_string_dirtystate="$i"
> __git_ps1_string_untrackedfiles="$u"
>
> There have been requests in the past to let people individually
> colourise different bits of the prompt, which this would make practical.We can't do that from __git_ps1(), because, as I mentioned in the commit message, it must be invoked in a command substitution from $PS1, and what's exported in a subshell that stays in that subshell.
Doing so from __git_prompt_command() would be quite simple: just rename the appropriate variables and don't declare them as local. It would even be more pleasing to the eyes than the current one-letter variable names.
Gábor