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

Re: [PATCH 2/4] git-prompt.sh: refactor colored prompt code

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Jun 23, 2013, 14:51 UTC
Message-ID
<20130623145157.GN20052@goldbirke>
In-Reply-To
<CAOz-D1+LoAnoRLgnyRYtq5LQR32RyXp4RR2M5pPTaxaGcXM4yg@mail.gmail.com>
On Sat, Jun 22, 2013 at 01:45:38PM -0300, Eduardo D'Avila wrote:
Show 16 quoted lines
> 2013/6/22 Øystein Walle <oystwa@gmail.com>:
> > I've gotten the impression it's better to use tput to generate the escape
> > sequences instead of hardcoding them. So something like:
> >
> > local c_red='\['"$(tput setaf 1)"'\]'
> > local c_green='\['"$(tput setaf 2)"'\]'
> > local c_green='\['"$(tput setaf 4)"'\]'
> > local c_clear='\['"$(tput sgr0)"'\]'
> >
> > which is technically cleaner, if not visually.
> >
> > The problem with that approach is that tput will be run several times for
> > each prompt, so it would be best if the color variables were global. Another
> > thing is that you rely on tput being available.
> 
> Are there any guidelines regarding global shell script variables?

There are a couple of global variables in the completion script, they are all prefixed with "__git_", e.g. $__git_porcelain_commands. In the prompt script we don't have any global variables at the moment, but if we were to create some, then they should be prefixed similarly. Or perhaps with "__git_ps1_". Anyway, in case of a global variable I'd spell out "color" completely instead of abbreviating it as "c".

> I'm considering doing this:
>    __git_c_red="\[$(tput setaf 1 || echo -e '\e[31m')\]"
> which handles the case where tput is not available.
To me 'tput setaf 1' is just a little bit less greek than '\e[31m'.

I'm wary of relying on tput's availability. It's part of ncurses, which is an essential package in many (most? all?) linux distros, but I don't know how it is with other supported platforms. So I think we'd have to stick to the hard-coded escape sequences as a fallback anyway. And if we can't get rid of these escape sequences completely, then I don't really see the point of using tput, especially considering the additional delay that would be caused by fork()ing four subshells and fork()+exec()ing four external commands on Windows.

However, I don't know much about the caveats of terminals, so I can't judge the benefits of using tput instead of the escape sequences.

Gábor
Previous: Eduardo D'AvilaNext: Eduardo R. D'Avila
Message 10 of 15 in “git-prompt: cleaning and improvement”
  1. 0/4 git-prompt: cleaning and improvementEduardo R. D'Avila, Jun 21, 2013
  2. 1/4 t9903: add tests for git-prompt pcmodeEduardo R. D'Avila, Jun 21, 2013
  3. SZEDER GáborJun 22, 2013
  4. Eduardo D'AvilaJun 22, 2013
  5. Junio C HamanoJun 23, 2013
  6. SZEDER GáborJun 24, 2013
  7. 2/4 git-prompt.sh: refactor colored prompt codeEduardo R. D'Avila, Jun 21, 2013
  8. Øystein WalleJun 22, 2013
  9. Eduardo D'AvilaJun 22, 2013
  10. SZEDER GáborJun 23, 2013
  11. Eduardo R. D'AvilaJun 25, 2013
  12. 3/4 git-prompt.sh: do not print duplicate clean color codeEduardo R. D'Avila, Jun 21, 2013
  13. SZEDER GáborJun 22, 2013
  14. 4/4 git-prompt.sh: add missing information in commentsEduardo R. D'Avila, Jun 21, 2013
  15. SZEDER GáborJun 22, 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.