1 // Copyright (c) 2015-2016 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "source/print.h"
16 
17 #if defined(SPIRV_ANDROID) || defined(SPIRV_LINUX) || defined(SPIRV_MAC) || \
18     defined(SPIRV_FREEBSD)
19 namespace spvtools {
20 
operator const char*()21 clr::reset::operator const char*() { return "\x1b[0m"; }
22 
operator const char*()23 clr::grey::operator const char*() { return "\x1b[1;30m"; }
24 
operator const char*()25 clr::red::operator const char*() { return "\x1b[31m"; }
26 
operator const char*()27 clr::green::operator const char*() { return "\x1b[32m"; }
28 
operator const char*()29 clr::yellow::operator const char*() { return "\x1b[33m"; }
30 
operator const char*()31 clr::blue::operator const char*() { return "\x1b[34m"; }
32 
33 }  // namespace spvtools
34 #elif defined(SPIRV_WINDOWS)
35 #include <windows.h>
36 
37 namespace spvtools {
38 
SetConsoleForegroundColorPrimary(HANDLE hConsole,WORD color)39 static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color) {
40   // Get screen buffer information from console handle
41   CONSOLE_SCREEN_BUFFER_INFO bufInfo;
42   GetConsoleScreenBufferInfo(hConsole, &bufInfo);
43 
44   // Get background color
45   color = WORD(color | (bufInfo.wAttributes & 0xfff0));
46 
47   // Set foreground color
48   SetConsoleTextAttribute(hConsole, color);
49 }
50 
SetConsoleForegroundColor(WORD color)51 static void SetConsoleForegroundColor(WORD color) {
52   SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color);
53   SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color);
54 }
55 
operator const char*()56 clr::reset::operator const char*() {
57   if (isPrint) {
58     SetConsoleForegroundColor(0xf);
59     return "";
60   }
61   return "\x1b[0m";
62 }
63 
operator const char*()64 clr::grey::operator const char*() {
65   if (isPrint) {
66     SetConsoleForegroundColor(FOREGROUND_INTENSITY);
67     return "";
68   }
69   return "\x1b[1;30m";
70 }
71 
operator const char*()72 clr::red::operator const char*() {
73   if (isPrint) {
74     SetConsoleForegroundColor(FOREGROUND_RED);
75     return "";
76   }
77   return "\x1b[31m";
78 }
79 
operator const char*()80 clr::green::operator const char*() {
81   if (isPrint) {
82     SetConsoleForegroundColor(FOREGROUND_GREEN);
83     return "";
84   }
85   return "\x1b[32m";
86 }
87 
operator const char*()88 clr::yellow::operator const char*() {
89   if (isPrint) {
90     SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN);
91     return "";
92   }
93   return "\x1b[33m";
94 }
95 
operator const char*()96 clr::blue::operator const char*() {
97   // Blue all by itself is hard to see against a black background (the
98   // default on command shell), or a medium blue background (the default
99   // on PowerShell).  So increase its intensity.
100 
101   if (isPrint) {
102     SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
103     return "";
104   }
105   return "\x1b[94m";
106 }
107 
108 }  // namespace spvtools
109 #else
110 namespace spvtools {
111 
operator const char*()112 clr::reset::operator const char*() { return ""; }
113 
operator const char*()114 clr::grey::operator const char*() { return ""; }
115 
operator const char*()116 clr::red::operator const char*() { return ""; }
117 
operator const char*()118 clr::green::operator const char*() { return ""; }
119 
operator const char*()120 clr::yellow::operator const char*() { return ""; }
121 
operator const char*()122 clr::blue::operator const char*() { return ""; }
123 
124 }  // namespace spvtools
125 #endif
126