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

Re: [PATCH] Add __git_ps1_pc to use as PROMPT_COMMAND

From
Michael J Gruber <git@drmicha.warpmail.net>
Date
Oct 2, 2012, 07:38 UTC
Message-ID
<506A99DE.7080503@drmicha.warpmail.net>
In-Reply-To
<7va9w5c31w.fsf@alter.siamese.dyndns.org>
Junio C Hamano venit, vidit, dixit 01.10.2012 23:09:
Show 31 quoted lines
> Simon Oosthoek <s.oosthoek@xs4all.nl> writes:
> 
>> It's possible to set PS1 to nothing and print a string from
>> PROMPT_COMMAND, but then you miss out on all the features of the PS1
>> interpretation by bash and compared to the use of __git_ps1 at the
>> moment, it has to put out quite a different string. Because if you like
>> to see user@host+workdir (git-status)[$#]
>> the current users of __git_ps1 say PS1="\u@host+\w $(__git_ps1 "%s")\$
>> ", but all __git+ps1 has to put out is "(branch)" or "(branch *)", etc.
>>
>> If it has to print the same prompt in PC mode, it has to add all the
>> user/host/workdir/[$#] data as well, withouth being able to use the bash
>> internal interpretation (because that is only working when PS1 is set).
> 
> 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?
> 
> So I still do not see the problem, even taking your "Set PS1 in the
> command, without spitting anything out of the command" use case into
> account.
> 
> Confused....
> 

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.

Michael
Previous: Junio C HamanoNext: Simon Oosthoek
Message 20 of 49 in “bash completion with colour hints”
  1. Simon OosthoekSep 26, 2012
  2. Ramkumar RamachandraSep 26, 2012
  3. Simon OosthoekSep 26, 2012
  4. Junio C HamanoSep 27, 2012
  5. Michael J GruberSep 27, 2012
  6. Michael J GruberSep 27, 2012
  7. Simon OosthoekSep 27, 2012
  8. Andreas SchwabSep 27, 2012
  9. Simon OosthoekSep 27, 2012
  10. Add __git_ps1_pc to use as PROMPT_COMMANDSimon Oosthoek, Sep 28, 2012
  11. Junio C HamanoSep 28, 2012
  12. Simon OosthoekOct 1, 2012
  13. Junio C HamanoOct 1, 2012
  14. Simon OosthoekOct 1, 2012
  15. Junio C HamanoOct 1, 2012
  16. Simon OosthoekOct 1, 2012
  17. Junio C HamanoOct 1, 2012
  18. Simon OosthoekOct 1, 2012
  19. Junio C HamanoOct 1, 2012
  20. Michael J GruberOct 2, 2012
  21. Simon OosthoekOct 2, 2012
  22. Junio C HamanoOct 2, 2012
  23. Simon OosthoekOct 2, 2012
  24. Junio C HamanoOct 2, 2012
  25. 1/2 Allow __git_ps1 to be used in PROMPT_COMMANDSimon Oosthoek, Oct 5, 2012
  26. Junio C HamanoOct 8, 2012
  27. Simon OosthoekOct 8, 2012
  28. Junio C HamanoOct 8, 2012
  29. 1/2 Allow __git_ps1 to be used in PROMPT_COMMANDSimon Oosthoek, Oct 10, 2012
  30. Junio C HamanoOct 10, 2012
  31. 2/2 show color hints based on state of the git treeSimon Oosthoek, Oct 10, 2012
  32. 1/2 Allow __git_ps1 to be used in PROMPT_COMMANDSimon Oosthoek, Oct 10, 2012
  33. 2/2 show color hints based on state of the git treeSimon Oosthoek, Oct 10, 2012
  34. 2/2 show color hints based on state of the git treeSimon Oosthoek, Oct 5, 2012
  35. Michael J GruberOct 15, 2012
  36. Simon OosthoekOct 15, 2012
  37. Michael J GruberOct 15, 2012
  38. Simon OosthoekOct 15, 2012
  39. Simon OosthoekOct 15, 2012
  40. Michael J GruberOct 15, 2012
  41. Simon OosthoekOct 8, 2012
  42. Simon OosthoekOct 15, 2012
  43. Junio C HamanoOct 15, 2012
  44. 3/3 Change colors to be based on git status -sb in color modeSimon Oosthoek, Oct 16, 2012
  45. Junio C HamanoOct 16, 2012
  46. 3/3 Change colors to be based on git status -sb in color modeSimon Oosthoek, Oct 16, 2012
  47. Junio C HamanoOct 16, 2012
  48. Junio C HamanoOct 16, 2012
  49. Simon OosthoekOct 17, 2012

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.