git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Need your help with MinGW Issue 17: --color options don't work (produce garbage)
@ 2007-08-15  6:29 Dmitry Kakurin
  2007-08-15  7:32 ` Reece Dunn
  0 siblings, 1 reply; 10+ messages in thread
From: Dmitry Kakurin @ 2007-08-15  6:29 UTC (permalink / raw)
  To: msysGit, Git Mailing List

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?

(little bit) more info here:
http://code.google.com/p/msysgit/issues/detail?id=17

-- 
- Dmitry

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)
  2007-08-15  6:29 Need your help with MinGW Issue 17: --color options don't work (produce garbage) Dmitry Kakurin
@ 2007-08-15  7:32 ` Reece Dunn
  2007-08-15  8:03   ` [msysGit] " Dmitry Kakurin
  2007-08-15 15:10   ` Johannes Schindelin
  0 siblings, 2 replies; 10+ messages in thread
From: Reece Dunn @ 2007-08-15  7:32 UTC (permalink / raw)
  To: msysGit, Git Mailing List

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

HTH,
- Reece

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [msysGit] Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)
  2007-08-15  7:32 ` Reece Dunn
@ 2007-08-15  8:03   ` Dmitry Kakurin
  2007-08-15  8:31     ` Reece Dunn
  2007-08-15 15:10   ` Johannes Schindelin
  1 sibling, 1 reply; 10+ messages in thread
From: Dmitry Kakurin @ 2007-08-15  8:03 UTC (permalink / raw)
  To: msclrhd; +Cc: msysGit, Git Mailing List

On 8/15/07, Reece Dunn <msclrhd@googlemail.com> wrote:
>
> 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.

That's my question. If there is a way to build cat.exe to do this kind
of emulation under MinGW then I should be able to do the same for
git.exe.
I hope I just need to #define something while building Git.
But what is it?
-- 
- Dmitry

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [msysGit] Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)
  2007-08-15  8:03   ` [msysGit] " Dmitry Kakurin
@ 2007-08-15  8:31     ` Reece Dunn
  2007-08-15 22:07       ` Dmitry Kakurin
  0 siblings, 1 reply; 10+ messages in thread
From: Reece Dunn @ 2007-08-15  8:31 UTC (permalink / raw)
  To: Dmitry Kakurin; +Cc: msysGit, Git Mailing List

On 15/08/07, Dmitry Kakurin <dmitry.kakurin@gmail.com> wrote:
> On 8/15/07, Reece Dunn <msclrhd@googlemail.com> wrote:
> >
> > 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.
>
> That's my question. If there is a way to build cat.exe to do this kind
> of emulation under MinGW then I should be able to do the same for
> git.exe.
> I hope I just need to #define something while building Git.
> But what is it?

You will need to implement cat (or something similar) on MinGW that
will process the *nix colour codes and then pass that to
SetConsoleTextAttributes. For example:

    int ch = 0;
    while(( ch = getch()) != EOF )
    {
        if( /*ch is part of a colour sequence*/ )
        {
            SetConsoleTextAttribute( GetStdHandle(...),
win_color_from_color_sequence(...));
        }
        else putc( ch );
    }

I don't believe that MinGW has got this working for cat, that is why
one that supports colours on Windows needs to be written.

- Reece

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [msysGit] Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)
  2007-08-15  7:32 ` Reece Dunn
  2007-08-15  8:03   ` [msysGit] " Dmitry Kakurin
@ 2007-08-15 15:10   ` Johannes Schindelin
  2007-08-15 21:22     ` Rogan Dawes
  1 sibling, 1 reply; 10+ messages in thread
From: Johannes Schindelin @ 2007-08-15 15:10 UTC (permalink / raw)
  To: Reece Dunn; +Cc: msysGit, Git Mailing List

Hi,

On Wed, 15 Aug 2007, Reece Dunn wrote:

> 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

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [msysGit] Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)
  2007-08-15 15:10   ` Johannes Schindelin
@ 2007-08-15 21:22     ` Rogan Dawes
  2007-08-15 21:34       ` Junio C Hamano
  0 siblings, 1 reply; 10+ messages in thread
From: Rogan Dawes @ 2007-08-15 21:22 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Reece Dunn, msysGit, Git Mailing List

Johannes Schindelin wrote:

> 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
> 

I'd say that since Windows doesn't have the concept of terminfo or 
terminal types, and since SSH is mostly only supported through 
cygwin-style "hacks", (or F-Secure, but I have no experience with that) 
that we should not be _too_ concerned about that.

Users that *do* need to use rxvt or SSH should simply disable the color 
mode, or alternatively, use the cygwin version. Color, while useful, is 
hardly critical functionality.

My R0.02.

Regards,

Rogan

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [msysGit] Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)
  2007-08-15 21:22     ` Rogan Dawes
@ 2007-08-15 21:34       ` Junio C Hamano
  2007-08-15 22:04         ` Rogan Dawes
  2007-08-21 14:08         ` Rogan Dawes
  0 siblings, 2 replies; 10+ messages in thread
From: Junio C Hamano @ 2007-08-15 21:34 UTC (permalink / raw)
  To: Rogan Dawes; +Cc: Johannes Schindelin, Reece Dunn, msysGit, Git Mailing List

Rogan Dawes <lists@dawes.za.net> writes:

> Users that *do* need to use rxvt or SSH should simply disable the
> color mode, or alternatively, use the cygwin version. Color, while
> useful, is hardly critical functionality.

Heh, that almost suggests that the native Windows command.com
support can disable the color without upsetting anybody ;-)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [msysGit] Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)
  2007-08-15 21:34       ` Junio C Hamano
@ 2007-08-15 22:04         ` Rogan Dawes
  2007-08-21 14:08         ` Rogan Dawes
  1 sibling, 0 replies; 10+ messages in thread
From: Rogan Dawes @ 2007-08-15 22:04 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Johannes Schindelin, Reece Dunn, msysGit, Git Mailing List

Junio C Hamano wrote:
> Rogan Dawes <lists@dawes.za.net> writes:
> 
>> Users that *do* need to use rxvt or SSH should simply disable the
>> color mode, or alternatively, use the cygwin version. Color, while
>> useful, is hardly critical functionality.
> 
> Heh, that almost suggests that the native Windows command.com
> support can disable the color without upsetting anybody ;-)
> 

If that is the easiest way, yes.

I *do* think that trying to get color working on the DOS cmdline is a 
worthy goal. I just don't think it is worth overcomplicating the issue 
and trying to handle rare corner cases such as someone SSHing into a 
Windows box, or running CMD inside rxvt.

One approach that might be more compatible with the existing escape code 
approach is to define the appropriate ANSI color escape sequences. Ah, a 
bit more googling shows that cmd.exe doesn't handle ANSI sequences in 
Windows NT and later.

Regards,

Rogan

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [msysGit] Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)
  2007-08-15  8:31     ` Reece Dunn
@ 2007-08-15 22:07       ` Dmitry Kakurin
  0 siblings, 0 replies; 10+ messages in thread
From: Dmitry Kakurin @ 2007-08-15 22:07 UTC (permalink / raw)
  To: Reece Dunn; +Cc: msysGit, Git Mailing List

On 8/15/07, Reece Dunn <msclrhd@googlemail.com> wrote:
> I don't believe that MinGW has got this working for cat, that is why
> one that supports colours on Windows needs to be written.

MSys' cat does support colors even when I run it from cmd.exe.
I've looked at sources for cat on mingw.sourceforge.com and (as
expected) there is no code to handle esc-sequences.
That would be insane - too many utilities like ls, diff, cat, less
etc. do that. So there is obviously a library for this.
I've tried to "parse" makefiles for cat, but they are so huge and I
know about gcc/make on Unix so little that I was unable to find the
secret sauce.
I've also asked this question on MinGW list so I'm waiting for reply.

-- 
- Dmitry

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [msysGit] Re: Need your help with MinGW Issue 17: --color options don't work (produce garbage)
  2007-08-15 21:34       ` Junio C Hamano
  2007-08-15 22:04         ` Rogan Dawes
@ 2007-08-21 14:08         ` Rogan Dawes
  1 sibling, 0 replies; 10+ messages in thread
From: Rogan Dawes @ 2007-08-21 14:08 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Johannes Schindelin, Reece Dunn, msysGit, Git Mailing List

Junio C Hamano wrote:
> Rogan Dawes <lists@dawes.za.net> writes:
> 
>> Users that *do* need to use rxvt or SSH should simply disable the
>> color mode, or alternatively, use the cygwin version. Color, while
>> useful, is hardly critical functionality.
> 
> Heh, that almost suggests that the native Windows command.com
> support can disable the color without upsetting anybody ;-)
> 

Following my other comment in this regard, another reason that colour in 
the cmd line tools is less important, is because my impression of the 
intent of the Windows port (end goal, that is), is that the user will be 
able to use a Windows GUI front-end to do whatever it is they want to.

And so the GUI front-end will be responsible for implementing the color 
anyway, most likely through pattern matching and a state machine.

Regards,

Rogan

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2007-08-21 14:09 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-08-15  6:29 Need your help with MinGW Issue 17: --color options don't work (produce garbage) Dmitry Kakurin
2007-08-15  7:32 ` Reece Dunn
2007-08-15  8:03   ` [msysGit] " Dmitry Kakurin
2007-08-15  8:31     ` Reece Dunn
2007-08-15 22:07       ` Dmitry Kakurin
2007-08-15 15:10   ` Johannes Schindelin
2007-08-15 21:22     ` Rogan Dawes
2007-08-15 21:34       ` Junio C Hamano
2007-08-15 22:04         ` Rogan Dawes
2007-08-21 14:08         ` Rogan Dawes

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).