1 // Copyright 2020 The Tint Authors.
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 <unistd.h>
16 
17 #include <cstring>
18 
19 #include "src/diagnostic/printer.h"
20 
21 namespace tint {
22 namespace diag {
23 namespace {
24 
supports_colors(FILE * f)25 bool supports_colors(FILE* f) {
26   if (!isatty(fileno(f))) {
27     return false;
28   }
29 
30   const char* cterm = getenv("TERM");
31   if (cterm == nullptr) {
32     return false;
33   }
34 
35   std::string term = getenv("TERM");
36   if (term != "cygwin" && term != "linux" && term != "rxvt-unicode-256color" &&
37       term != "rxvt-unicode" && term != "screen-256color" && term != "screen" &&
38       term != "tmux-256color" && term != "tmux" && term != "xterm-256color" &&
39       term != "xterm-color" && term != "xterm") {
40     return false;
41   }
42 
43   return true;
44 }
45 
46 class PrinterLinux : public Printer {
47  public:
PrinterLinux(FILE * f,bool colors)48   PrinterLinux(FILE* f, bool colors)
49       : file(f), use_colors(colors && supports_colors(f)) {}
50 
write(const std::string & str,const Style & style)51   void write(const std::string& str, const Style& style) override {
52     write_color(style.color, style.bold);
53     fwrite(str.data(), 1, str.size(), file);
54     write_color(Color::kDefault, false);
55   }
56 
57  private:
color_code(Color color,bool bold)58   constexpr const char* color_code(Color color, bool bold) {
59     switch (color) {
60       case Color::kDefault:
61         return bold ? "\u001b[1m" : "\u001b[0m";
62       case Color::kBlack:
63         return bold ? "\u001b[30;1m" : "\u001b[30m";
64       case Color::kRed:
65         return bold ? "\u001b[31;1m" : "\u001b[31m";
66       case Color::kGreen:
67         return bold ? "\u001b[32;1m" : "\u001b[32m";
68       case Color::kYellow:
69         return bold ? "\u001b[33;1m" : "\u001b[33m";
70       case Color::kBlue:
71         return bold ? "\u001b[34;1m" : "\u001b[34m";
72       case Color::kMagenta:
73         return bold ? "\u001b[35;1m" : "\u001b[35m";
74       case Color::kCyan:
75         return bold ? "\u001b[36;1m" : "\u001b[36m";
76       case Color::kWhite:
77         return bold ? "\u001b[37;1m" : "\u001b[37m";
78     }
79     return "";  // unreachable
80   }
81 
write_color(Color color,bool bold)82   void write_color(Color color, bool bold) {
83     if (use_colors) {
84       auto* code = color_code(color, bold);
85       fwrite(code, 1, strlen(code), file);
86     }
87   }
88 
89   FILE* const file;
90   bool const use_colors;
91 };
92 
93 }  // namespace
94 
create(FILE * out,bool use_colors)95 std::unique_ptr<Printer> Printer::create(FILE* out, bool use_colors) {
96   return std::make_unique<PrinterLinux>(out, use_colors);
97 }
98 
99 }  // namespace diag
100 }  // namespace tint
101