Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 2 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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 |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 6 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 8 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 14 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 15 | #include "source/print.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 16 | |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 17 | #if defined(SPIRV_ANDROID) || defined(SPIRV_LINUX) || defined(SPIRV_MAC) || \ |
Assiduous | 74e8105 | 2021-06-29 12:14:38 -0700 | [diff] [blame] | 18 | defined(SPIRV_IOS) || defined(SPIRV_TVOS) || defined(SPIRV_FREEBSD) || \ |
Brad Smith | 5f45f79 | 2022-09-02 15:30:09 -0400 | [diff] [blame^] | 19 | defined(SPIRV_OPENBSD) || defined(SPIRV_EMSCRIPTEN) || \ |
| 20 | defined(SPIRV_FUCHSIA) || defined(SPIRV_GNU) |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 21 | namespace spvtools { |
David Neto | 0165636 | 2015-11-20 10:44:41 -0500 | [diff] [blame] | 22 | |
Andrew Woloszyn | 4c657bf | 2016-03-18 14:13:16 -0400 | [diff] [blame] | 23 | clr::reset::operator const char*() { return "\x1b[0m"; } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 24 | |
Andrew Woloszyn | 4c657bf | 2016-03-18 14:13:16 -0400 | [diff] [blame] | 25 | clr::grey::operator const char*() { return "\x1b[1;30m"; } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 26 | |
Andrew Woloszyn | 4c657bf | 2016-03-18 14:13:16 -0400 | [diff] [blame] | 27 | clr::red::operator const char*() { return "\x1b[31m"; } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 28 | |
Andrew Woloszyn | 4c657bf | 2016-03-18 14:13:16 -0400 | [diff] [blame] | 29 | clr::green::operator const char*() { return "\x1b[32m"; } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 30 | |
Andrew Woloszyn | 4c657bf | 2016-03-18 14:13:16 -0400 | [diff] [blame] | 31 | clr::yellow::operator const char*() { return "\x1b[33m"; } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 32 | |
Andrew Woloszyn | 4c657bf | 2016-03-18 14:13:16 -0400 | [diff] [blame] | 33 | clr::blue::operator const char*() { return "\x1b[34m"; } |
David Neto | 0165636 | 2015-11-20 10:44:41 -0500 | [diff] [blame] | 34 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 35 | } // namespace spvtools |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 36 | #elif defined(SPIRV_WINDOWS) |
David Neto | 7c58c1d | 2016-02-29 01:01:04 -0500 | [diff] [blame] | 37 | #include <windows.h> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 38 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 39 | namespace spvtools { |
David Neto | 0165636 | 2015-11-20 10:44:41 -0500 | [diff] [blame] | 40 | |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 41 | static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color) { |
Lukas Hermanns | 4fe8e38 | 2017-08-20 14:11:50 +0200 | [diff] [blame] | 42 | // Get screen buffer information from console handle |
| 43 | CONSOLE_SCREEN_BUFFER_INFO bufInfo; |
| 44 | GetConsoleScreenBufferInfo(hConsole, &bufInfo); |
| 45 | |
| 46 | // Get background color |
Andrey Tuganov | 32cf85d | 2017-08-28 18:36:52 -0400 | [diff] [blame] | 47 | color = WORD(color | (bufInfo.wAttributes & 0xfff0)); |
Lukas Hermanns | 4fe8e38 | 2017-08-20 14:11:50 +0200 | [diff] [blame] | 48 | |
| 49 | // Set foreground color |
| 50 | SetConsoleTextAttribute(hConsole, color); |
| 51 | } |
| 52 | |
Diego Novillo | d2938e4 | 2017-11-08 12:40:02 -0500 | [diff] [blame] | 53 | static void SetConsoleForegroundColor(WORD color) { |
Lukas Hermanns | 4fe8e38 | 2017-08-20 14:11:50 +0200 | [diff] [blame] | 54 | SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color); |
| 55 | SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color); |
| 56 | } |
| 57 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 58 | clr::reset::operator const char*() { |
Lei Zhang | 1379535 | 2017-11-30 16:35:22 -0500 | [diff] [blame] | 59 | if (isPrint) { |
| 60 | SetConsoleForegroundColor(0xf); |
| 61 | return ""; |
| 62 | } |
| 63 | return "\x1b[0m"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 66 | clr::grey::operator const char*() { |
Lei Zhang | 1379535 | 2017-11-30 16:35:22 -0500 | [diff] [blame] | 67 | if (isPrint) { |
| 68 | SetConsoleForegroundColor(FOREGROUND_INTENSITY); |
| 69 | return ""; |
| 70 | } |
| 71 | return "\x1b[1;30m"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 74 | clr::red::operator const char*() { |
Lei Zhang | 1379535 | 2017-11-30 16:35:22 -0500 | [diff] [blame] | 75 | if (isPrint) { |
| 76 | SetConsoleForegroundColor(FOREGROUND_RED); |
| 77 | return ""; |
| 78 | } |
| 79 | return "\x1b[31m"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 82 | clr::green::operator const char*() { |
Lei Zhang | 1379535 | 2017-11-30 16:35:22 -0500 | [diff] [blame] | 83 | if (isPrint) { |
| 84 | SetConsoleForegroundColor(FOREGROUND_GREEN); |
| 85 | return ""; |
| 86 | } |
| 87 | return "\x1b[32m"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 90 | clr::yellow::operator const char*() { |
Lei Zhang | 1379535 | 2017-11-30 16:35:22 -0500 | [diff] [blame] | 91 | if (isPrint) { |
| 92 | SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN); |
| 93 | return ""; |
| 94 | } |
| 95 | return "\x1b[33m"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 98 | clr::blue::operator const char*() { |
David Neto | 0167758 | 2017-08-24 10:34:00 -0400 | [diff] [blame] | 99 | // Blue all by itself is hard to see against a black background (the |
| 100 | // default on command shell), or a medium blue background (the default |
| 101 | // on PowerShell). So increase its intensity. |
Lei Zhang | 1379535 | 2017-11-30 16:35:22 -0500 | [diff] [blame] | 102 | |
| 103 | if (isPrint) { |
| 104 | SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY); |
| 105 | return ""; |
| 106 | } |
| 107 | return "\x1b[94m"; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 108 | } |
David Neto | 0165636 | 2015-11-20 10:44:41 -0500 | [diff] [blame] | 109 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 110 | } // namespace spvtools |
Ben Vanik | 01c8d7a | 2015-11-22 08:32:53 -0800 | [diff] [blame] | 111 | #else |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 112 | namespace spvtools { |
Ben Vanik | 01c8d7a | 2015-11-22 08:32:53 -0800 | [diff] [blame] | 113 | |
| 114 | clr::reset::operator const char*() { return ""; } |
| 115 | |
| 116 | clr::grey::operator const char*() { return ""; } |
| 117 | |
| 118 | clr::red::operator const char*() { return ""; } |
| 119 | |
| 120 | clr::green::operator const char*() { return ""; } |
| 121 | |
| 122 | clr::yellow::operator const char*() { return ""; } |
| 123 | |
| 124 | clr::blue::operator const char*() { return ""; } |
| 125 | |
dan sinclair | 3dad1cd | 2018-07-07 09:38:00 -0400 | [diff] [blame] | 126 | } // namespace spvtools |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 127 | #endif |