Home | History | Annotate | Download | only in source
      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 "print.h"
     16 
     17 #if defined(SPIRV_ANDROID) || defined(SPIRV_LINUX) || defined(SPIRV_MAC) || defined(SPIRV_FREEBSD)
     18 namespace libspirv {
     19 
     20 clr::reset::operator const char*() { return "\x1b[0m"; }
     21 
     22 clr::grey::operator const char*() { return "\x1b[1;30m"; }
     23 
     24 clr::red::operator const char*() { return "\x1b[31m"; }
     25 
     26 clr::green::operator const char*() { return "\x1b[32m"; }
     27 
     28 clr::yellow::operator const char*() { return "\x1b[33m"; }
     29 
     30 clr::blue::operator const char*() { return "\x1b[34m"; }
     31 
     32 }  // namespace libspirv
     33 #elif defined(SPIRV_WINDOWS)
     34 #include <windows.h>
     35 
     36 namespace libspirv {
     37 
     38 static void SetConsoleForegroundColorPrimary(HANDLE hConsole, WORD color)
     39 {
     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 
     51 static void SetConsoleForegroundColor(WORD color)
     52 {
     53   SetConsoleForegroundColorPrimary(GetStdHandle(STD_OUTPUT_HANDLE), color);
     54   SetConsoleForegroundColorPrimary(GetStdHandle(STD_ERROR_HANDLE), color);
     55 }
     56 
     57 clr::reset::operator const char*() {
     58   SetConsoleForegroundColor(0xf);
     59   return "";
     60 }
     61 
     62 clr::grey::operator const char*() {
     63   SetConsoleForegroundColor(FOREGROUND_INTENSITY);
     64   return "";
     65 }
     66 
     67 clr::red::operator const char*() {
     68   SetConsoleForegroundColor(FOREGROUND_RED);
     69   return "";
     70 }
     71 
     72 clr::green::operator const char*() {
     73   SetConsoleForegroundColor(FOREGROUND_GREEN);
     74   return "";
     75 }
     76 
     77 clr::yellow::operator const char*() {
     78   SetConsoleForegroundColor(FOREGROUND_RED | FOREGROUND_GREEN);
     79   return "";
     80 }
     81 
     82 clr::blue::operator const char*() {
     83   // Blue all by itself is hard to see against a black background (the
     84   // default on command shell), or a medium blue background (the default
     85   // on PowerShell).  So increase its intensity.
     86   SetConsoleForegroundColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
     87   return "";
     88 }
     89 
     90 }  // namespace libspirv
     91 #else
     92 namespace libspirv {
     93 
     94 clr::reset::operator const char*() { return ""; }
     95 
     96 clr::grey::operator const char*() { return ""; }
     97 
     98 clr::red::operator const char*() { return ""; }
     99 
    100 clr::green::operator const char*() { return ""; }
    101 
    102 clr::yellow::operator const char*() { return ""; }
    103 
    104 clr::blue::operator const char*() { return ""; }
    105 
    106 }  // namespace libspirv
    107 #endif
    108