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
Simon Oosthoek <s.oosthoek@xs4all.nl>
Date
Oct 2, 2012, 19:50 UTC
Message-ID
<506B4598.1020206@xs4all.nl>
In-Reply-To
<7vhaqcajvt.fsf@alter.siamese.dyndns.org>
On 02/10/12 19:01, Junio C Hamano wrote:
Show 8 quoted lines
> If your goal is to use PROMPT_COMMAND and not PS1, then yes between
> the two definitions of PROMPT_COMMAND above, the latter may look
> simpler.  But that does not explain why you want to prefer it over
> PS1 in the first place, which was the central point of my question
> that still has not been answered.
> 
> Even more confused...
> 

The specific answer to your question is that without using PROMPT_COMMAND it doesn't seem to be possible to use colors based on the state of the git tree. The reason being that in order to prevent wrapping problems on the command line (specifically going up/down the history list in bash or line wrapping on a long command). This is prevented (and quite the norm in static PS1 strings) by enclosing the terminal code for color inside \[ and \] so bash doesn't count these and what is in between them in the length of the prompt string.

The only way to get the colors in without messing up normal functioning of the prompt is to use PROMPT_COMMAND to set PS1, with colors based dynamically on the state of the tree.

In my current version (which I haven't had time to properly send out, sorry!) it can do both using the same __git_ps1 function. But only color in the PROMPT_COMMAND mode for the reason laid out above.

Having said that, I think there's another benefit to using PROMPT_COMMAND; I think it is more elegant to use a function via PROMPT_COMMAND (now that I know of it), than using a function via command substitution inside the PS1.

I guess your (and my) confusion is more caused by unfamiliarity with this feature of bash than it being used. Is that correct?

*desperately trying to get out of confused mode*
Cheers
Simon
Previous: Junio C HamanoNext: Junio C Hamano
Message 23 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.