]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Clever hack to determine terminal color support for *nix. Uses zero libraries :)
authorDale Weiler <killfieldengine@gmail.com>
Wed, 14 Nov 2012 19:30:07 +0000 (19:30 +0000)
committerDale Weiler <killfieldengine@gmail.com>
Wed, 14 Nov 2012 19:30:07 +0000 (19:30 +0000)
con.c

diff --git a/con.c b/con.c
index 623cb991d548adb7befef7a7963ed978cf5adfa3..3e6c20dbc2f7ca3e7d2747729931bc274f900e2f 100644 (file)
--- a/con.c
+++ b/con.c
@@ -199,6 +199,45 @@ static void con_enablecolor() {
         console.color_err = !!(isatty(STDERR_FILENO));
     if (console.handle_out == stderr || console.handle_out == stdout)
         console.color_out = !!(isatty(STDOUT_FILENO));
+        
+    #ifndef _WIN32
+    {
+        char buf[4];
+        
+        /*
+         * This is such a hack.  But I'm not linking in any libraries to
+         * do this stupidity.  It's insane there is simply not a ttyhascolor
+         * in unistd.h
+         */
+        FILE *tput = popen("tput colors", "r");
+        if  (!tput) {
+            /*
+             * disable colors since we can't determine without tput
+             * which should be guranteed on all *nix OSes
+             */
+            console.color_err = 0;
+            console.color_out = 0;
+        }
+        
+        /*
+         * Handle to tput was a success, lets read in the amount of
+         * color support.  It should be at minimal 8.
+         */
+        fread(buf, sizeof(buf), 1, tput);
+        buf[3] = '\0';
+        
+        if (atoi(buf) < 8) {
+            console.color_err = 0;
+            console.color_out = 0;
+        }
+        
+        /*
+         * We made it this far, which means we support colors in the
+         * terminal.
+         */
+        fclose(tput);
+    }
+    #endif
 }
 
 /*