Merge pull request #916 from 0lvin/color_console

Unix: Add color output for console
This commit is contained in:
Yamagi 2022-11-05 09:49:00 +01:00 committed by GitHub
commit d58116d717
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 1 deletions

View file

@ -53,6 +53,9 @@ static void *game_library;
// Evil hack to determine if stdin is available
qboolean stdin_active = true;
// Terminal supports colors
static qboolean color_active = false;
// Config dir
char cfgdir[MAX_OSPATH] = CFGDIR;
@ -107,6 +110,37 @@ Sys_Quit(void)
void
Sys_Init(void)
{
char *envvar;
envvar = getenv("TERM");
if (envvar && strstr(envvar, "color"))
{
char buf[256];
color_active = true;
snprintf(buf, sizeof(buf),
"\2Terminal supports colors: TERM='%s'\n", envvar);
Sys_ConsoleOutput(buf);
return;
}
envvar = getenv("COLORTERM");
if (envvar && strlen(envvar))
{
char buf[256];
color_active = true;
snprintf(buf, sizeof(buf),
"\2Terminal supports colors: COLORTERM='%s'\n", envvar);
Sys_ConsoleOutput(buf);
return;
}
Sys_ConsoleOutput("Terminal has no colors support.\n");
}
/* ================================================================ */
@ -160,6 +194,29 @@ Sys_ConsoleInput(void)
void
Sys_ConsoleOutput(char *string)
{
if ((string[0] == 0x01) || (string[0] == 0x02))
{
if (color_active)
{
if (string[0] == 0x01)
{
/* red */
fputs("\033[31;1m", stdout);
}
else
{
/* green */
fputs("\033[32;1m", stdout);
}
fputs(string + 1, stdout);
/* reset to default terminal settings */
fputs("\033[0m", stdout);
return;
}
}
fputs(string, stdout);
}

View file

@ -236,6 +236,12 @@ Sys_ConsoleOutput(char *string)
char text[256];
DWORD dummy;
if ((string[0] == 0x01) || (string[0] == 0x02))
{
// remove color marker
string[0] = ' ';
}
if (!dedicated || !dedicated->value)
{
fputs(string, stdout);

View file

@ -34,6 +34,7 @@ FILE *logfile;
cvar_t *logfile_active; /* 1 = buffer log, 2 = flush after each print */
jmp_buf abortframe; /* an ERR_DROP occured, exit the entire frame */
int server_state;
cvar_t *color_terminal;
static int rd_target;
static char *rd_buffer;
@ -105,10 +106,22 @@ Com_VPrintf(int print_level, const char *fmt, va_list argptr)
Con_Print(msg);
#endif
if ((msg[0] == 0x01 || msg[0] == 0x02) &&
color_terminal && color_terminal->value)
{
// skip color marker
i = 1;
}
else
{
i = 0;
}
// remove unprintable characters
for(i=0; i<msgLen; ++i)
for(; i<msgLen; ++i)
{
char c = msg[i];
if(c < ' ' && (c < '\t' || c > '\r'))
{
switch(c)
@ -143,6 +156,12 @@ Com_VPrintf(int print_level, const char *fmt, va_list argptr)
{
char name[MAX_OSPATH];
if ((msg[0] == 0x01) || (msg[0] == 0x02))
{
// remove color marker
msg[0] = ' ';
}
if (!logfile)
{
Com_sprintf(name, sizeof(name), "%s/qconsole.log", FS_Gamedir());

View file

@ -35,6 +35,7 @@ cvar_t *fixedtime;
cvar_t *cl_maxfps;
cvar_t *dedicated;
extern cvar_t *color_terminal;
extern cvar_t *logfile_active;
extern jmp_buf abortframe; /* an ERR_DROP occured, exit the entire frame */
extern zhead_t z_chain;
@ -353,6 +354,7 @@ Qcommon_Init(int argc, char **argv)
developer = Cvar_Get("developer", "0", 0);
fixedtime = Cvar_Get("fixedtime", "0", 0);
color_terminal = Cvar_Get("colorterminal", "1", CVAR_ARCHIVE);
logfile_active = Cvar_Get("logfile", "1", CVAR_ARCHIVE);
modder = Cvar_Get("modder", "0", 0);
timescale = Cvar_Get("timescale", "1", 0);