r165 - trunk/code/unix

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Tue Oct 18 21:51:19 EDT 2005


Author: tma
Date: 2005-10-18 21:51:19 -0400 (Tue, 18 Oct 2005)
New Revision: 165

Modified:
   trunk/code/unix/unix_main.c
Log:
* Silly little patch to convert Q3 style color codes to ANSI escape codes. Note
  this doesn't work too well with baseq3 since it logs color strings with no
  color termination.


Modified: trunk/code/unix/unix_main.c
===================================================================
--- trunk/code/unix/unix_main.c	2005-10-18 02:46:20 UTC (rev 164)
+++ trunk/code/unix/unix_main.c	2005-10-19 01:51:19 UTC (rev 165)
@@ -85,6 +85,9 @@
 
 static field_t tty_con;
 
+static cvar_t *ttycon_ansicolor = NULL;
+static qboolean ttycon_color_on = qfalse;
+
 // history
 // NOTE TTimo this is a bit duplicate of the graphical console history
 //   but it's safer and faster to write our own here
@@ -519,6 +522,12 @@
     tc.c_cc[VTIME] = 0;
     tcsetattr (0, TCSADRAIN, &tc);    
     ttycon_on = qtrue;
+
+    ttycon_ansicolor = Cvar_Get( "ttycon_ansicolor", "0", CVAR_ARCHIVE );
+    if( ttycon_ansicolor && ttycon_ansicolor->value )
+    {
+      ttycon_color_on = qtrue;
+    }
   } else
     ttycon_on = qfalse;
 }
@@ -1178,13 +1187,89 @@
   return NULL;
 }
 
+static struct Q3ToAnsiColorTable_s
+{
+  char Q3color;
+  char *ANSIcolor;
+} tty_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 tty_colorTableSize =
+  sizeof( tty_colorTable ) / sizeof( tty_colorTable[ 0 ] );
+
+void Sys_ANSIColorify( const char *msg, char *buffer, int bufferSize )
+{
+  int   msgLength, pos;
+  int   i, j;
+  char  *escapeCode;
+
+  if( !msg || !buffer )
+    return;
+
+  msgLength = strlen( msg );
+  pos = 0;
+  i = 0;
+  buffer[ 0 ] = '\0';
+
+  while( i < msgLength )
+  {
+    if( msg[ i ] == '\n' )
+    {
+      strncat( buffer, va( "%c[0m\n", 0x1B ), bufferSize );
+      i++;
+    }
+    else if( msg[ i ] == Q_COLOR_ESCAPE )
+    {
+      i++;
+
+      if( i < msgLength )
+      {
+        escapeCode = NULL;
+        for( j = 0; j < tty_colorTableSize; j++ )
+        {
+          if( msg[ i ] == tty_colorTable[ j ].Q3color )
+          {
+            escapeCode = tty_colorTable[ j ].ANSIcolor;
+            break;
+          }
+        }
+
+        if( escapeCode )
+          strncat( buffer, va( "%c[%sm", 0x1B, escapeCode ), bufferSize );
+
+        i++;
+      }
+    }
+    else
+      strncat( buffer, va( "%c", msg[ i++ ] ), bufferSize );
+  }
+}
+
 void  Sys_Print( const char *msg )
 {
   if (ttycon_on)
   {
     tty_Hide();
   }
-  fputs(msg, stderr);
+
+  if( ttycon_on && ttycon_color_on )
+  {
+    char ansiColorString[ MAXPRINTMSG ];
+    Sys_ANSIColorify( msg, ansiColorString, MAXPRINTMSG );
+    fputs( ansiColorString, stderr );
+  }
+  else
+    fputs(msg, stderr);
+
   if (ttycon_on)
   {
     tty_Show();




More information about the quake3-commits mailing list