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

Re: [PATCH 1/6] prompt: don't scream continuation state

From
Jeff King <peff@peff.net>
Date
Jun 4, 2013, 04:38 UTC
Message-ID
<20130604043836.GA3931@sigill.intra.peff.net>
In-Reply-To
<CALkWK0=5XecaMpPPQgvJZTwR2QUUWVqbZK5p7af9R1HjA0U=OQ@mail.gmail.com>
On Tue, Jun 04, 2013 at 09:14:23AM +0530, Ramkumar Ramachandra wrote:
Show 9 quoted lines
> Jeff King wrote:
> > It seems silly to argue about output formats when we are writing a
> > prompt in a convenient Turing-complete scripting language already.
> > What about something like:
> 
> Could you have a look at __git_ps1_colorize_gitstring from
> rr/zsh-color-prompt in pu?  In the general case, wouldn't the user
> need to re-implement this entire function (with so many variables) in
> her ~/.zshrc?  Isn't it horribly painful for too little gain?

It is a lot of ugly code, but then, complexity and flexibility are a tradeoff. You would not _have_ to use a custom function at all. But if we make it an option, and leave the existing code as the default function, then only those who want it have to use it.

Even better, we can hit a middle ground by abstracting away some of the complexity. You could probably write colorize() function to handle colorizing a particular string (and handle bash/zsh magic), and then pull the whole "which color is the branch" function into a colorize_branch(), and so forth.

I haven't looked that closely, so there may be hidden troubles awaiting, but I don't see why you couldn't ultimately let the user write something like:

  __git_ps1_printer() {
          echo ' ('
          test "$bare" = t && echo "BARE:"; # or "bare:" :)
          color_branch "$(short_ref $branch)" "$detached"
          test "$wt_changes" = t && colorize red "*"
          ...
          echo ')'
  }

And if that is too much, you can abstract bits of it out. The point is that if we can expose the lowest-level details, we can compose functions around them at whatever level is appropriate, and callers can mix and match for the balance of complexity and flexibility they want.

Perhaps it is overkill, but I also think it may make the code itself more readable. For example, I put a "..." above because the next line would handle this:

  if [ -n "$i" ]; then
          gitstring="$gitstring\[$ok_color\]$i"
  fi

I have no clue what that does, or what the global variable "$i" contains. Reading through __git_ps1, it looks like it will be "+" for index changes. Being able to write:

  test "$index_changes" = t && colorize green "+"
seems much more readable to me. And we could probably compose a:
  colorize_diff_state() {
          test "$wt_changes" = t && colorize red "*"
          test "$index_changes" = t && colorize green "+"
  }
for people who just want to take the default colors and characters.
-Peff
Previous: Ramkumar RamachandraNext: Junio C Hamano
Message 7 of 33 in “Minor prompt, completion cleanups”
  1. 0/6 Minor prompt, completion cleanupsRamkumar Ramachandra, Jun 2, 2013
  2. 1/6 prompt: don't scream continuation stateRamkumar Ramachandra, Jun 2, 2013
  3. Thomas RastJun 3, 2013
  4. Ramkumar RamachandraJun 3, 2013
  5. Jeff KingJun 3, 2013
  6. Ramkumar RamachandraJun 4, 2013
  7. Jeff KingJun 4, 2013
  8. Junio C HamanoJun 4, 2013
  9. Junio C HamanoJun 3, 2013
  10. 2/6 completion: add common options for rev-parseRamkumar Ramachandra, Jun 2, 2013
  11. Ramkumar RamachandraJun 7, 2013
  12. 3/6 completion: add common options for blameRamkumar Ramachandra, Jun 2, 2013
  13. Thomas RastJun 3, 2013
  14. Ramkumar RamachandraJun 3, 2013
  15. SZEDER GáborJun 3, 2013
  16. Junio C HamanoJun 3, 2013
  17. Peter KreftingJun 6, 2013
  18. Junio C HamanoJun 3, 2013
  19. 4/6 completion: correct completion for format-patchRamkumar Ramachandra, Jun 2, 2013
  20. Felipe ContrerasJun 2, 2013
  21. Ramkumar RamachandraJun 2, 2013
  22. 5/6 completion: clarify difftool completionRamkumar Ramachandra, Jun 2, 2013
  23. Junio C HamanoJun 3, 2013
  24. 6/6 completion: clarify ls-tree, archive, show completionRamkumar Ramachandra, Jun 2, 2013
  25. Junio C HamanoJun 3, 2013
  26. Ramkumar RamachandraJun 4, 2013
  27. Junio C HamanoJun 4, 2013
  28. Junio C HamanoJun 3, 2013
  29. SZEDER GáborJun 3, 2013
  30. Ramkumar RamachandraJun 7, 2013
  31. Junio C HamanoJun 7, 2013
  32. SZEDER GáborJun 7, 2013
  33. Junio C HamanoJun 9, 2013

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.