[MPlayer-dev-eng] [PATCH] Colorful output for Windows

Zuxy Meng zuxy.meng at gmail.com
Wed Mar 28 16:55:12 CEST 2007


Just for fun.

-- 
Zuxy
Beauty is truth,
While truth is beauty.
PGP KeyID: E8555ED6
-------------- next part --------------
Index: mp_msg.c
===================================================================
--- mp_msg.c	?????? 22834??
+++ mp_msg.c	????????????
@@ -24,6 +24,10 @@
 #endif
 #include "mp_msg.h"
 
+#if defined(__MINGW32__) && defined(MSG_USE_COLORS)
+#include <windows.h>
+#endif
+
 /* maximum message length of mp_msg */
 #define MSGSIZE_MAX 3072
 
@@ -89,6 +93,12 @@
 void mp_msg(int mod, int lev, const char *format, ... ){
     va_list va;
     char tmp[MSGSIZE_MAX];
+    FILE *stream= (lev) <= MSGL_WARN ? stderr : stdout;
+#if defined (__MINGW32__) && defined(MSG_USE_COLORS)
+    HANDLE hd = lev <= MSGL_WARN ? GetStdHandle(STD_ERROR_HANDLE):
+        GetStdHandle(STD_OUTPUT_HANDLE);
+    WORD orig_attrib;
+#endif
     
     if (!mp_msg_test(mod, lev)) return; // do not display
     va_start(va, format);
@@ -144,7 +154,12 @@
       flag=0;
     }
 #endif    
-    {   unsigned char v_colors[10]={9,1,3,15,7,2,2,8,8,8};
+    {
+#ifndef __MINGW32__
+        unsigned char v_colors[10]={9,1,3,15,7,2,2,8,8,8};
+#else
+        unsigned char v_colors[10]={12,4,6,15,7,2,2,8,8,8};
+#endif
         static const char *mod_text[MSGT_MAX]= {
                                 "GLOBAL",
                                 "CPLAYER",
@@ -196,18 +211,33 @@
         int c=v_colors[lev];
         int c2=(mod+1)%15+1;
         static int header=1;
-        FILE *stream= (lev) <= MSGL_WARN ? stderr : stdout;
+#ifdef __MINGW32__
+        CONSOLE_SCREEN_BUFFER_INFO buf_info;
+
+        if (GetConsoleScreenBufferInfo(hd, &buf_info))
+            orig_attrib = buf_info.wAttributes;
+        else
+            orig_attrib = 7;    // 7 means white on black
+#endif
         if(header){
+#ifndef __MINGW32__
             fprintf(stream, "\033[%d;3%dm%9s\033[0;37m: ",c2>>3,c2&7, mod_text[mod]);
+#else
+            SetConsoleTextAttribute(hd, c2);
+            fprintf(stream, "%9s: ", mod_text[mod]);
+#endif
         }
+#ifndef __MINGW32__
         fprintf(stream, "\033[%d;3%dm",c>>3,c&7);
+#else
+        SetConsoleTextAttribute(hd, c);
+#endif
         header=    tmp[strlen(tmp)-1] == '\n'
                  ||tmp[strlen(tmp)-1] == '\r';
     }
 #endif
-    if (lev <= MSGL_WARN){
-        fprintf(stderr, "%s", tmp);fflush(stderr);
-    } else {
-        printf("%s", tmp);fflush(stdout);
-    }
+    fprintf(stream, "%s", tmp);fflush(stream);
+#if defined(__MINGW32__) && defined(MSG_USE_COLORS)
+    SetConsoleTextAttribute(hd, orig_attrib);
+#endif
 }


More information about the MPlayer-dev-eng mailing list