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