Skip to content

Commit

Permalink
Add ANSI colors option for gsc_printf()
Browse files Browse the repository at this point in the history
  • Loading branch information
voron00 committed Mar 9, 2017
1 parent 7223044 commit 2bbb599
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
80 changes: 79 additions & 1 deletion gsc_utils.cpp
Expand Up @@ -43,6 +43,81 @@ void gsc_utils_getarraykeys()
}
}

/*
=================
Sys_AnsiColorPrint
Transform Q3 colour codes to ANSI escape sequences
=================
*/
#define MAXPRINTMSG 1024
#define ColorIndex(c) (((c) - '0') & 0x07)
#define Q_COLOR_ESCAPE '^'
#define Q_IsColorString(p) ((p) && *(p) == Q_COLOR_ESCAPE && *((p)+1) && isalnum(*((p)+1))) // ^[0-9a-zA-Z]
void Sys_AnsiColorPrint( const char *msg )
{
static char buffer[ MAXPRINTMSG ];
int length = 0;
static int q3ToAnsi[ 8 ] =
{
30, // COLOR_BLACK
31, // COLOR_RED
32, // COLOR_GREEN
33, // COLOR_YELLOW
34, // COLOR_BLUE
36, // COLOR_CYAN
35, // COLOR_MAGENTA
0 // COLOR_WHITE
};

while( *msg )
{
if( Q_IsColorString( msg ) || *msg == '\n' )
{
// First empty the buffer
if( length > 0 )
{
buffer[ length ] = '\0';
fputs( buffer, stdout );
length = 0;
}

if( *msg == '\n' )
{
// Issue a reset and then the newline
fputs( "\033[0m\n", stdout );
msg++;
}
else
{
// Print the color code
Com_sprintf( buffer, sizeof( buffer ), "\033[1;%dm",
q3ToAnsi[ ColorIndex( *( msg + 1 ) ) ] );
fputs( buffer, stdout );
msg += 2;
}
}
else
{
if( length >= MAXPRINTMSG - 1 )
break;

buffer[ length ] = *msg;
length++;
msg++;
}
}

// Empty anything still left in the buffer
if( length > 0 )
{
buffer[ length ] = '\0';
fputs( buffer, stdout );
// Issue a reset at the end
fputs( "\033[0m", stdout );
}
}

extern cvar_t *colored_prints;
int stackPrintParam(int param)
{
if (param >= Scr_GetNumParam())
Expand All @@ -53,7 +128,10 @@ int stackPrintParam(int param)
case STACK_STRING:
char *str;
stackGetParamString(param, &str); // no error checking, since we know it's a string
printf("%s", str);
if (colored_prints->boolean)
Sys_AnsiColorPrint(str);
else
printf("%s", str);
return 1;

case STACK_VECTOR:
Expand Down
2 changes: 2 additions & 0 deletions libcod.cpp
Expand Up @@ -14,6 +14,7 @@ cvar_t *sv_allowDownload;
cvar_t *sv_pure;
cvar_t *developer;
cvar_t *rcon_password;
cvar_t *colored_prints;

#if COD_VERSION == COD2_1_2 || COD_VERSION == COD2_1_3
cvar_t *sv_wwwDownload;
Expand Down Expand Up @@ -1222,6 +1223,7 @@ class cCallOfDuty2Pro
sv_cracked = Cvar_RegisterBool("sv_cracked", 0, 0x1000u);
nodeveloper_errors = Cvar_RegisterBool("nodeveloper_errors", 0, 0x1000u);
nodeveloper_prints = Cvar_RegisterBool("nodeveloper_prints", 0, 0x1000u);
colored_prints = Cvar_RegisterBool("colored_prints", 1, 0x1000u);
g_playerCollision = Cvar_RegisterBool("g_playerCollision", 1, 0x1000u);
g_playerEject = Cvar_RegisterBool("g_playerEject", 1, 0x1000u);
fs_library = Cvar_RegisterString("fs_library", "", 0x1000u);
Expand Down

0 comments on commit 2bbb599

Please sign in to comment.