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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux