Skip to content

Commit

Permalink
Merge pull request #3767 from yebblies/ddmdcolor
Browse files Browse the repository at this point in the history
[DDMD] Make color ddmd-friendly
  • Loading branch information
9rnsr committed Jul 16, 2014
1 parent 6c71d58 commit 16e5e2a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/color.c
Expand Up @@ -14,7 +14,6 @@
#include <stdio.h>

#if _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>
#endif
Expand All @@ -26,26 +25,32 @@
#endif

#if _WIN32
static CONSOLE_SCREEN_BUFFER_INFO sbi;
static const BOOL sbi_inited = GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &sbi);
static CONSOLE_SCREEN_BUFFER_INFO *consoleAttributes()
{
static CONSOLE_SCREEN_BUFFER_INFO sbi;
static bool sbi_inited = false;
if (!sbi_inited)
sbi_inited = GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &sbi) != FALSE;
return &sbi;
}
#endif

int isConsoleColorSupported()
bool isConsoleColorSupported()
{
#if _WIN32
return _isatty(_fileno(stderr));
return _isatty(_fileno(stderr)) != 0;
#elif __linux__ || __APPLE__ || __FreeBSD__ || __OpenBSD__ || __sun
const char *term = getenv("TERM");
return isatty(STDERR_FILENO) && term && term[0] && 0 != strcmp(term, "dumb");
#else
return 0;
return false;
#endif
}

void setConsoleColorBright()
{
#if _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), sbi.wAttributes | FOREGROUND_INTENSITY);
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), consoleAttributes()->wAttributes | FOREGROUND_INTENSITY);
#else
fprintf(stderr, "\033[1m");
#endif
Expand All @@ -55,7 +60,7 @@ void setConsoleColorError()
{
#if _WIN32
enum { FOREGROUND_WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE };
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), (sbi.wAttributes & ~FOREGROUND_WHITE) | FOREGROUND_RED | FOREGROUND_INTENSITY);
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), (consoleAttributes()->wAttributes & ~FOREGROUND_WHITE) | FOREGROUND_RED | FOREGROUND_INTENSITY);
#else
fprintf(stderr, "\033[1;31m");
#endif
Expand All @@ -64,7 +69,7 @@ void setConsoleColorError()
void resetConsoleColor()
{
#if _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), sbi.wAttributes);
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), consoleAttributes()->wAttributes);
#else
fprintf(stderr, "\033[m");
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/color.h
Expand Up @@ -9,7 +9,7 @@
* https://github.com/D-Programming-Language/dmd/blob/master/src/mars.c
*/

extern int isConsoleColorSupported();
extern bool isConsoleColorSupported();
extern void setConsoleColorBright();
extern void setConsoleColorError();
extern void resetConsoleColor();
Expand Down

0 comments on commit 16e5e2a

Please sign in to comment.