int Q_ColorCharOffset( const char *s, int charcount ) { const char *start = s; char c; while( *s && charcount ) { int gc = Q_GrabCharFromColorString( &s, &c, NULL ); if( gc == GRABCHAR_CHAR ) charcount--; else if( gc == GRABCHAR_COLOR ) ; else if( gc == GRABCHAR_END ) break; else assert( 0 ); } return s - start; }
static int Q_ColorStrLastColor( const char *s, int byteofs ) { char c; const char *end = s + byteofs; int lastcolor = ColorIndex(COLOR_WHITE), colorindex; while( s < end ) { int gc = Q_GrabCharFromColorString( &s, &c, &colorindex ); if( gc == GRABCHAR_CHAR ) ; else if( gc == GRABCHAR_COLOR ) lastcolor = colorindex; else if( gc == GRABCHAR_END ) break; else assert( 0 ); } return lastcolor; }
int Q_ColorCharCount( const char *s, int byteofs ) { char c; const char *end = s + byteofs; int charcount = 0; while( s < end ) { int gc = Q_GrabCharFromColorString( &s, &c, NULL ); if( gc == GRABCHAR_CHAR ) charcount++; else if( gc == GRABCHAR_COLOR ) ; else if( gc == GRABCHAR_END ) break; else assert( 0 ); } return charcount; }
static void PrintColoredText( const char *s ) { char c; int colorindex; DWORD dummy; while( *s ) { int gc = Q_GrabCharFromColorString( &s, &c, &colorindex ); if( gc == GRABCHAR_CHAR ) { if( c == '\n' ) SetConsoleTextAttribute( houtput, 7 ); // I hope it's not too slow to output char by char WriteFile( houtput, &c, 1, &dummy, NULL ); } else if( gc == GRABCHAR_COLOR ) { switch( colorindex ) { case 0: colorindex = 3; break; // dark cyan instead of black to keep it visible case 1: colorindex = 12; break; case 2: colorindex = 10; break; case 3: colorindex = 14; break; case 4: colorindex = 9; break; case 5: colorindex = 11; break; // note that cyan and magenta are case 6: colorindex = 13; break; // not where one might expect case 8: colorindex = 6; break; case 9: colorindex = 8; break; default: case 7: colorindex = 7; break; // 15 would be bright white }; SetConsoleTextAttribute( houtput, colorindex ); } else if( gc == GRABCHAR_END ) break; else assert( 0 ); } }
static void Sys_AnsiColorPrint( const char *msg ) { static char buffer[2096]; 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 ) { char c = *msg; int colorindex; int gc = Q_GrabCharFromColorString( &msg, &c, &colorindex ); if( gc == GRABCHAR_COLOR || (gc == GRABCHAR_CHAR && c == '\n') ) { // First empty the buffer if( length > 0 ) { buffer[length] = '\0'; fputs( buffer, stdout ); length = 0; } if( c == '\n' ) { // Issue a reset and then the newline fputs( "\033[0m\n", stdout ); } else { // Print the color code Q_snprintfz( buffer, sizeof( buffer ), "\033[%dm", q3ToAnsi[ colorindex ] ); fputs( buffer, stdout ); } } else if( gc == GRABCHAR_END ) break; else { if( length >= sizeof( buffer ) - 1 ) break; buffer[length++] = c; } } // Empty anything still left in the buffer if( length > 0 ) { buffer[length] = '\0'; fputs( buffer, stdout ); } }