Thread (3 messages) flat view 3 messages, 3 authors, 2016-06-15

Re: [PATCH] Add __git_ps1_pc to use as PROMPT_COMMAND

From: Simon Oosthoek <hidden>
Date: 2016-06-15 22:54:55

On 10/02/2012 09:38 AM, Michael J Gruber wrote:
quoted
The longer I read your explanation, the less useful the "PC mode"
sounds like, at least to me.  So why does an user even want to use
such a mechanism, instead of PS1?  And even if the user wants to use
it by doing \w, \u etc. himself, she can do that with

	PROMPT_COMMAND='
		PS1=$(printf "\u \h \w %s$ " $(__git_ps1 "%s"))
         '

just fine, no?
Well, in that way, it doesn't work and setting PS1 directly would be 
better. But this way you cannot do colors.
quoted
Confused....
I know the feeling ;-)
The "problem" (as far as I see) is only: What user interface do we want
to expose to the user, or rather, do we want to expose the user to ;)

So far, we say:

#    1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
#    2) Add the following line to your .bashrc/.zshrc:
#        source ~/.git-prompt.sh
#    3) Change your PS1 to also show the current branch:
#         Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
#         ZSH:  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

(That's incomplete for zsh, but I'm digressing...). With the above, we
would change the bash instruction to

PROMPT_COMMAND='
	PS1=$(printf "\u \h \w %s$ " $(__git_ps1 "%s"))
        '

which may look more complicated to some. I think we can aim for
something like

PROMPT_COMMAND='__git_ps1_color "\u \h \w%s$ " " (%s)"'

so that the first arg is a PS1 string, %s there gets replaced by the git
prompt (if any), and the second arg defines the formatting of the git
prompt. This makes the "ui" (what you have to set the shells vars to) as
simple as possible.

As I said, I had done some fixes and refactoring to that effect already,
but I'm not going to race Simon.
I don't quite see how you're going to smurf the formatted string into 
the PS1 string at %s...

Wouldn't it be easier in this case to have the user provide 3 arguments:

PROMPT_COMMAND='__git_ps1 "\u@\h:\w " "(%s)" "\$ "'

The first and last can be directly assigned to PS1 and the format string 
can be put in the middle.

BUT. In this way, it is still impossible to change the colors inside the 
format string, unless you ignore that when the user requested color 
hints. Because the output of printf will not be interpreted by bash when 
put in via command substitution.

The whole point of this exercise (for me at least) was to be able to 
color different parts of the bit that __git_ps1 put in, based on the 
current state of the tree.

I think the user interface could be something like:

use either:
PROMPT_COMMAND='__git_ps1 "\u@\h:\w " "\$ "' # 2 args!
or
PS1="\u@\h:\w $(__git_ps1 '(%s)')\$ "

(I just realised that I still need to handle the case where both PS1 and 
PROMPT_COMMAND contain __git_ps1 ;-)

Note that in the PS1 case, no color codes should be put into the string, 
as they will mangle the prompt length and wrapping problems start happening.

Cheers

Simon.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help