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

Re: [msysGit] Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Aug 15, 2007, 15:10 UTC
Message-ID
<Pine.LNX.4.64.0708151708570.19222@wbgn129.biozentrum.uni-wuerzburg.de>
In-Reply-To
<3f4fd2640708150032l7441b285mc2cc9e22702bce21@mail.gmail.com>
Hi,
On Wed, 15 Aug 2007, Reece Dunn wrote:
Show 77 quoted lines
> On 15/08/07, Dmitry Kakurin wrote:
> > Here are the facts:
> >
> > 'git branch --color' produces garbage:
> > $ git branch --color
> >   devel??[m
> >   dima??[m
> >   dmitryk??[m
> > * ??[32mmaster??[m
> >   mob??[m
> >   next??[m
> >
> > 'git branch --color | cat' produces expected colored output.
> >
> > I've traced it down to printf statement in gdb and it sends the right
> > esc-sequence.
> > Where should I look next?
> 
> Windows doesn't recognise the *nix printf colour codes.
> 
> Piping through cat will be going through cygwin/mingw emulation,
> translating the colour codes to the correct API calls.
> 
> You need to call the SetConsoleTextAttribute Win32 API. For example:
> 
> #ifdef defined(WIN32) || defined(WIN64)
> 
> typedef WORD color_t;
> 
> color_t red = FOREGROUND_INTENSITY | FOREGROUND_RED;
> color_t green = FOREGROUND_INTENSITY | FOREGROUND_GREEN;
> color_t blue = FOREGROUND_INTENSITY | FOREGROUND_BLUE;
> 
> color_t white = red | green | blue;
> 
> void set_color( color_t color )
> {
>     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color );
> }
> 
> #else
> 
> typedef const char * color_t;
> 
> color_t red = ...;
> ...
> 
> void set_color( color_t color ){ printf( color ); }
> 
> #endif
> 
> That way, you can do things like:
>     set_color( red );
>     printf( ... );
>     set_color( blue );
> 
> This is not as pretty as the existing codebase, so another possibility
> would be to create wrappers around the console output functions (i.e.
> printf) and call SetConsoleTextAttribute there. This way, you can
> restore the old colour when a restore settings sequence is
> intercepted. It is also possible to reuse the GetStdHandle return
> value.
> 
> NOTE: There isn't a GetConsoleTextAttribute in the Windows API, but
> Google found this:
> 
> #if ( (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__) )
> && defined(_CONSOLE)
> 
> static WORD GetConsoleTextAttribute(HANDLE Console)
> {
>     CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo;
>     GetConsoleScreenBufferInfo(Console, &ConsoleInfo);
>     return ConsoleInfo.wAttributes;
> }
> 
> #endif

Hmm. Somehow I doubt that this hack works _outside_ of the Windows console. I.e. if you call git in rxvt, it will fail, if you ssh into Windows, it will fail.

Ciao, Dscho

Previous: Dmitry KakurinNext: Rogan Dawes
Message 6 of 10 in “Need your help with MinGW Issue 17: --color options don't work (produce garbage)”
  1. Dmitry KakurinAug 15, 2007
  2. Reece DunnAug 15, 2007
  3. Dmitry KakurinAug 15, 2007
  4. Reece DunnAug 15, 2007
  5. Dmitry KakurinAug 15, 2007
  6. Johannes SchindelinAug 15, 2007
  7. Rogan DawesAug 15, 2007
  8. Junio C HamanoAug 15, 2007
  9. Rogan DawesAug 15, 2007
  10. Rogan DawesAug 21, 2007

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.