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_WINDOWS)
18 #include <windows.h>
19
20 namespace spvtools {
21
SetConsoleForegroundColorPrimary(HANDLE hConsole,WORD color)22 static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color) {
23 // Get screen buffer information from console handle
24 CONSOLE_SCREEN_BUFFER_INFO bufInfo;
25 GetConsoleScreenBufferInfo(hConsole, &bufInfo);
26
27 // Get background color
28 color = WORD(color | (bufInfo.wAttributes & 0xfff0));
29
30 // Set foreground color
31 SetConsoleTextAttribute(hConsole, color);
32 }
33
SetConsoleForegroundColor(WORD color)34 static void SetConsoleForegroundColor(WORD color) {
35 SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color);
36 SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color);
37 }
38
operator const char*()39 clr::reset::operator const char*() {
40 if (isPrint) {
41 SetConsoleForegroundColor(0xf);
42 return "";
43 }
44 return "\x1b[0m";
45 }
46
operator const char*()47 clr::grey::operator const char*() {
48 if (isPrint) {
49 SetConsoleForegroundColor(FOREGROUND_INTENSITY);
50 return "";
51 }
52 return "\x1b[1;30m";
53 }
54
operator const char*()55 clr::red::operator const char*() {
56 if (isPrint) {
57 SetConsoleForegroundColor(FOREGROUND_RED);
58 return "";
59 }
60 return "\x1b[31m";
61 }
62
operator const char*()63 clr::green::operator const char*() {
64 if (isPrint) {
65 SetConsoleForegroundColor(FOREGROUND_GREEN);
66 return "";
67 }
68 return "\x1b[32m";
69 }
70
operator const char*()71 clr::yellow::operator const char*() {
72 if (isPrint) {
73 SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN);
74 return "";
75 }
76 return "\x1b[33m";
77 }
78
operator const char*()79 clr::blue::operator const char*() {
80 // Blue all by itself is hard to see against a black background (the
81 // default on command shell), or a medium blue background (the default
82 // on PowerShell). So increase its intensity.
83
84 if (isPrint) {
85 SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
86 return "";
87 }
88 return "\x1b[94m";
89 }
90
91 } // namespace spvtools
92 #else
93 namespace spvtools {
94
operator const char*()95 clr::reset::operator const char*() { return "\x1b[0m"; }
96
operator const char*()97 clr::grey::operator const char*() { return "\x1b[1;30m"; }
98
operator const char*()99 clr::red::operator const char*() { return "\x1b[31m"; }
100
operator const char*()101 clr::green::operator const char*() { return "\x1b[32m"; }
102
operator const char*()103 clr::yellow::operator const char*() { return "\x1b[33m"; }
104
operator const char*()105 clr::blue::operator const char*() { return "\x1b[34m"; }
106
107 } // namespace spvtools
108 #endif
109