* Replace Sys_AnsiColorify with Sys_AnsiColorPrint, a more simple means of

achieving the same feature
* Handle non-numeric color codes in Sys_AnsiColorPrint
This commit is contained in:
Tim Angus 2007-10-09 20:49:19 +00:00
parent 95f67c2c3e
commit a9eaefecab

View file

@ -220,96 +220,73 @@ void Sys_Init(void)
Cvar_Set( "username", Sys_GetCurrentUser( ) ); Cvar_Set( "username", Sys_GetCurrentUser( ) );
} }
static struct Q3ToAnsiColorTable_s
{
char Q3color;
char *ANSIcolor;
} CON_colorTable[ ] =
{
{ COLOR_BLACK, "30" },
{ COLOR_RED, "31" },
{ COLOR_GREEN, "32" },
{ COLOR_YELLOW, "33" },
{ COLOR_BLUE, "34" },
{ COLOR_CYAN, "36" },
{ COLOR_MAGENTA, "35" },
{ COLOR_WHITE, "0" }
};
static int CON_colorTableSize =
sizeof( CON_colorTable ) / sizeof( CON_colorTable[ 0 ] );
/* /*
================= =================
Sys_ANSIColorify Sys_AnsiColorPrint
Transform Q3 colour codes to ANSI escape sequences Transform Q3 colour codes to ANSI escape sequences
================= =================
*/ */
//XXX: function should behave like others that colorize strings static void Sys_AnsiColorPrint( const char *msg )
static void Sys_ANSIColorify( const char *msg, char *buffer, unsigned bufferSize )
{ {
int msgLength; static char buffer[ MAXPRINTMSG ];
int i, j; int length = 0;
char *escapeCode; 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
};
if( !msg || !buffer ) while( *msg )
return;
msgLength = strlen( msg );
i = 0;
buffer[ 0 ] = '\0';
while( i < msgLength )
{ {
if( msg[ i ] == '\n' ) if( Q_IsColorString( msg ) || *msg == '\n' )
{ {
unsigned len = 4; // First empty the buffer
if(bufferSize <= len) goto out; if( length > 0 )
strcpy( buffer, "\x1b[m\n");
buffer += len;
bufferSize -= len;
i++;
}
else if( msg[ i ] == Q_COLOR_ESCAPE )
{ {
i++; buffer[ length ] = '\0';
fputs( buffer, stderr );
if( i < msgLength ) length = 0;
{
escapeCode = NULL;
// XXX: no need for that loop
for( j = 0; j < CON_colorTableSize; j++ )
{
if( msg[ i ] == CON_colorTable[ j ].Q3color )
{
escapeCode = CON_colorTable[ j ].ANSIcolor;
break;
}
} }
if( escapeCode ) if( *msg == '\n' )
{ {
unsigned len = 3 + strlen(escapeCode); // Issue a reset and then the newline
if(bufferSize <= len) goto out; fputs( "\033[0m\n", stderr );
Com_sprintf( buffer, len+1, "\x1b[%sm", escapeCode ); msg++;
buffer += len;
bufferSize -= len;
} }
else
i++; {
// Print the color code
Com_sprintf( buffer, sizeof( buffer ), "\033[%dm",
q3ToAnsi[ ColorIndex( *( msg + 1 ) ) ] );
fputs( buffer, stderr );
msg += 2;
} }
} }
else else
{ {
if(bufferSize <= 1) goto out; if( length >= MAXPRINTMSG - 1 )
*buffer++ = msg[ i++ ]; break;
*buffer = 0;
--bufferSize; buffer[ length ] = *msg;
length++;
msg++;
} }
} }
out:
return; // Empty anything still left in the buffer
if( length > 0 )
{
buffer[ length ] = '\0';
fputs( buffer, stderr );
}
} }
/* /*
@ -324,11 +301,7 @@ void Sys_Print( const char *msg )
#endif #endif
if( com_ansiColor && com_ansiColor->integer ) if( com_ansiColor && com_ansiColor->integer )
{ Sys_AnsiColorPrint( msg );
char ansiColorString[ MAXPRINTMSG ];
Sys_ANSIColorify( msg, ansiColorString, MAXPRINTMSG );
fputs( ansiColorString, stderr );
}
else else
fputs(msg, stderr); fputs(msg, stderr);