PATCH: Allow both foreground and background color changes on Windows

Yaron Keren yaron.keren at gmail.com
Sun Nov 16 08:53:18 PST 2014


Allow both foreground and background text color changes on Windows.

Currently, change of foreground will discard the existing background and
vice verse.
This patch preserves them.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141116/122a27af/attachment.html>
-------------- next part --------------
Index: lib/Support/Windows/Process.inc
===================================================================
--- lib/Support/Windows/Process.inc	(revision 222085)
+++ lib/Support/Windows/Process.inc	(working copy)
@@ -370,20 +370,34 @@
   return 0;
 }
 
+static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
+  CONSOLE_SCREEN_BUFFER_INFO info;
+  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
+  return info.wAttributes;
+}
+
+static const WORD foreground_mask =
+    FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
+static const WORD background_mask =
+    BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
+
 const char *Process::OutputColor(char code, bool bold, bool bg) {
   if (UseANSI) return colorcodes[bg?1:0][bold?1:0][code&7];
 
-  WORD colors;
+  WORD colors = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
+
   if (bg) {
-    colors = ((code&1) ? BACKGROUND_RED : 0) |
-      ((code&2) ? BACKGROUND_GREEN : 0 ) |
-      ((code&4) ? BACKGROUND_BLUE : 0);
+    colors &= foreground_mask;
+    colors |= ((code & 1) ? BACKGROUND_RED : 0) |
+              ((code & 2) ? BACKGROUND_GREEN : 0) |
+              ((code & 4) ? BACKGROUND_BLUE : 0);
     if (bold)
       colors |= BACKGROUND_INTENSITY;
   } else {
-    colors = ((code&1) ? FOREGROUND_RED : 0) |
-      ((code&2) ? FOREGROUND_GREEN : 0 ) |
-      ((code&4) ? FOREGROUND_BLUE : 0);
+    colors &= background_mask;
+    colors |= ((code & 1) ? FOREGROUND_RED : 0) |
+              ((code & 2) ? FOREGROUND_GREEN : 0) |
+              ((code & 4) ? FOREGROUND_BLUE : 0);
     if (bold)
       colors |= FOREGROUND_INTENSITY;
   }
@@ -391,12 +405,6 @@
   return 0;
 }
 
-static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
-  CONSOLE_SCREEN_BUFFER_INFO info;
-  GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
-  return info.wAttributes;
-}
-
 const char *Process::OutputReverse() {
   if (UseANSI) return "\033[7m";
 
@@ -403,10 +411,6 @@
   const WORD attributes
    = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
 
-  const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
-    FOREGROUND_RED | FOREGROUND_INTENSITY;
-  const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
-    BACKGROUND_RED | BACKGROUND_INTENSITY;
   const WORD color_mask = foreground_mask | background_mask;
 
   WORD new_attributes =


More information about the llvm-commits mailing list