GUI: Check console color support before using it

This commit is contained in:
TellowKrinkle 2021-09-10 22:11:54 -05:00 committed by tellowkrinkle
parent 0f4f09c597
commit ffbfd2c844
1 changed files with 20 additions and 2 deletions

View File

@ -26,8 +26,22 @@ static DeclareTls(int) conlog_Indent(0);
static DeclareTls(ConsoleColors) conlog_Color(DefaultConsoleColor); static DeclareTls(ConsoleColors) conlog_Color(DefaultConsoleColor);
#ifdef __POSIX__ #ifdef __POSIX__
#include <unistd.h>
static FILE* stdout_fp = stdout; static FILE* stdout_fp = stdout;
static bool checkSupportsColor()
{
if (!isatty(fileno(stdout_fp)))
return false;
char* term = getenv("TERM");
if (!term || (0 == strcmp(term, "dumb")))
return false;
return true; // Probably supports color
}
static bool supports_color = checkSupportsColor();
void Console_SetStdout(FILE* fp) void Console_SetStdout(FILE* fp)
{ {
stdout_fp = fp; stdout_fp = fp;
@ -177,6 +191,8 @@ static void __concall ConsoleStdout_Newline()
static void __concall ConsoleStdout_DoSetColor(ConsoleColors color) static void __concall ConsoleStdout_DoSetColor(ConsoleColors color)
{ {
#if defined(__POSIX__) #if defined(__POSIX__)
if (!supports_color)
return;
fprintf(stdout_fp, "\033[0m%s", GetLinuxConsoleColor(color)); fprintf(stdout_fp, "\033[0m%s", GetLinuxConsoleColor(color));
fflush(stdout_fp); fflush(stdout_fp);
#endif #endif
@ -185,8 +201,10 @@ static void __concall ConsoleStdout_DoSetColor(ConsoleColors color)
static void __concall ConsoleStdout_SetTitle(const wxString& title) static void __concall ConsoleStdout_SetTitle(const wxString& title)
{ {
#if defined(__POSIX__) #if defined(__POSIX__)
if (supports_color)
fputs("\033]0;", stdout_fp); fputs("\033]0;", stdout_fp);
fputs(title.utf8_str(), stdout_fp); fputs(title.utf8_str(), stdout_fp);
if (supports_color)
fputs("\007", stdout_fp); fputs("\007", stdout_fp);
#endif #endif
} }