Skip to content

Disable color on non-tty output #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions textcolor.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>


#if __WIN32__
Expand Down Expand Up @@ -207,37 +208,35 @@ void text_color_init (int enable_color)
g_enable_color = enable_color;


if (g_enable_color == 0 || isatty(fileno(stdout)) == 0) {
return;
}
#if __WIN32__


if (g_enable_color) {

HANDLE h;
CONSOLE_SCREEN_BUFFER_INFO csbi;
WORD attr = BACKGROUND_WHITE;
DWORD length;
COORD coord;
DWORD nwritten;
HANDLE h;
CONSOLE_SCREEN_BUFFER_INFO csbi;
WORD attr = BACKGROUND_WHITE;
DWORD length;
COORD coord;
DWORD nwritten;

h = GetStdHandle(STD_OUTPUT_HANDLE);
if (h != NULL && h != INVALID_HANDLE_VALUE) {
h = GetStdHandle(STD_OUTPUT_HANDLE);
if (h != NULL && h != INVALID_HANDLE_VALUE) {

GetConsoleScreenBufferInfo (h, &csbi);
GetConsoleScreenBufferInfo (h, &csbi);

length = csbi.dwSize.X * csbi.dwSize.Y;
coord.X = 0;
coord.Y = 0;
FillConsoleOutputAttribute (h, attr, length, coord, &nwritten);
}
length = csbi.dwSize.X * csbi.dwSize.Y;
coord.X = 0;
coord.Y = 0;
FillConsoleOutputAttribute (h, attr, length, coord, &nwritten);
}

#else
if (g_enable_color) {
//printf ("%s", clear_eos);
printf ("%s", background_white);
printf ("%s", clear_eos);
printf ("%s", black);
}
//printf ("%s", clear_eos);
printf ("%s", background_white);
printf ("%s", clear_eos);
printf ("%s", black);
#endif
}

Expand All @@ -252,7 +251,7 @@ void text_color_set ( enum dw_color_e c )
WORD attr;
HANDLE h;

if (g_enable_color == 0) {
if (g_enable_color == 0 || isatty(fileno(stdout)) == 0) {
return;
}

Expand Down Expand Up @@ -296,7 +295,7 @@ void text_color_set ( enum dw_color_e c )
void text_color_set ( enum dw_color_e c )
{

if (g_enable_color == 0) {
if (g_enable_color == 0 || isatty(fileno(stdout)) == 0) {
return;
}

Expand Down