aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/colorizer/colors.h
blob: 1ac3d689f6ae6953d3b1bef1b4511e0e5e1a626f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once

#include "fwd.h"

#include <util/generic/string.h>
#include <util/generic/strbuf.h> 

#include <cstdio>
 
namespace NColorizer {
    /** 
     * List of ECMA-48 colors. 
     * 
     * When printing elements of this enum via `operator<<`, `AutoColors()` (see below) function will be used 
     * to produce colors, i.e. nothing will be printed to non-tty streams. When converting elements of this enum 
     * via `ToString`, escape code is always returned. 
     * 
     * Note: as of now (2019-03), `ya make` strips out some escape codes from compiler output. 
     * It also inserts `RESET` before each color code. See https://st.yandex-team.ru/DEVTOOLS-5269 for details. 
     * For now, do not use `OLD`, `ST_*`, `FG_*` and `BG_*` in tools that run through `ya make`. 
     * 
     * Note: refrain from using black colors because there's high chance they'll not be visible on some terminals. 
     * Default windows and ubuntu color schemes shows them as black letters on black background. 
     * Also, white colors are barely visible in default OSX color scheme. Light black is usually fine though. 
     */ 
    enum EAnsiCode: i8 { 
        // Note: not using `GENERATE_ENUM_SERIALIZATION` because serialization generator depends on this library. 
 
        /// Does not change anything. 
        INVALID, 
 
        /// Reset all styles and colors. Safe to use in `ya make` tools. 
        RESET, 
 
        /// Change style, don't change anything else. 
        ST_LIGHT, 
        ST_DARK, 
        ST_NORMAL, 
 
        /// Additional styles. 
        ITALIC_ON, 
        ITALIC_OFF, 
        UNDERLINE_ON, 
        UNDERLINE_OFF, 
 
        /// Change foreground color, don't change anything else. 
        FG_DEFAULT, 
        FG_BLACK, 
        FG_RED, 
        FG_GREEN, 
        FG_YELLOW, 
        FG_BLUE, 
        FG_MAGENTA, 
        FG_CYAN, 
        FG_WHITE, 
 
        /// Change background color, don't change anything else. 
        BG_DEFAULT, 
        BG_BLACK, 
        BG_RED, 
        BG_GREEN, 
        BG_YELLOW, 
        BG_BLUE, 
        BG_MAGENTA, 
        BG_CYAN, 
        BG_WHITE, 
 
        /// Reset all styles and colors, then enable a (possibly light or dark) color. Safe to use in `ya make` tools. 
        DEFAULT, 
        BLACK, 
        RED, 
        GREEN, 
        YELLOW, 
        BLUE, 
        MAGENTA, 
        CYAN, 
        WHITE, 
        LIGHT_DEFAULT, 
        LIGHT_BLACK, 
        LIGHT_RED, 
        LIGHT_GREEN, 
        LIGHT_YELLOW, 
        LIGHT_BLUE, 
        LIGHT_MAGENTA, 
        LIGHT_CYAN, 
        LIGHT_WHITE, 
        DARK_DEFAULT, 
        DARK_BLACK, 
        DARK_RED, 
        DARK_GREEN, 
        DARK_YELLOW, 
        DARK_BLUE, 
        DARK_MAGENTA, 
        DARK_CYAN, 
        DARK_WHITE, 
    }; 
 
    /** 
     * Produces escape codes or empty stringbuf depending on settings. 
     * All color functions return zero-terminated stringbuf. 
     */ 
    class TColors { 
    public:
        static bool CalcIsTTY(FILE* file); 
 
    public: 
        explicit TColors(FILE* = stderr);
        explicit TColors(bool ontty);

        TStringBuf Reset() const noexcept; 

        TStringBuf StyleLight() const noexcept; 
        TStringBuf StyleDark() const noexcept; 
        TStringBuf StyleNormal() const noexcept; 

        TStringBuf ItalicOn() const noexcept; 
        TStringBuf ItalicOff() const noexcept; 
        TStringBuf UnderlineOn() const noexcept; 
        TStringBuf UnderlineOff() const noexcept; 
 
        TStringBuf ForeDefault() const noexcept; 
        TStringBuf ForeBlack() const noexcept; 
        TStringBuf ForeRed() const noexcept; 
        TStringBuf ForeGreen() const noexcept; 
        TStringBuf ForeYellow() const noexcept; 
        TStringBuf ForeBlue() const noexcept; 
        TStringBuf ForeMagenta() const noexcept; 
        TStringBuf ForeCyan() const noexcept; 
        TStringBuf ForeWhite() const noexcept; 
 
        TStringBuf BackDefault() const noexcept; 
        TStringBuf BackBlack() const noexcept; 
        TStringBuf BackRed() const noexcept; 
        TStringBuf BackGreen() const noexcept; 
        TStringBuf BackYellow() const noexcept; 
        TStringBuf BackBlue() const noexcept; 
        TStringBuf BackMagenta() const noexcept; 
        TStringBuf BackCyan() const noexcept; 
        TStringBuf BackWhite() const noexcept; 
 
        TStringBuf Default() const noexcept; 
        TStringBuf Black() const noexcept; 
        TStringBuf Red() const noexcept; 
        TStringBuf Green() const noexcept; 
        TStringBuf Yellow() const noexcept; 
        TStringBuf Blue() const noexcept; 
        TStringBuf Magenta() const noexcept; 
        TStringBuf Cyan() const noexcept; 
        TStringBuf White() const noexcept; 
 
        TStringBuf LightDefault() const noexcept; 
        TStringBuf LightBlack() const noexcept; 
        TStringBuf LightRed() const noexcept; 
        TStringBuf LightGreen() const noexcept; 
        TStringBuf LightYellow() const noexcept; 
        TStringBuf LightBlue() const noexcept; 
        TStringBuf LightMagenta() const noexcept; 
        TStringBuf LightCyan() const noexcept; 
        TStringBuf LightWhite() const noexcept; 
 
        TStringBuf DarkDefault() const noexcept; 
        TStringBuf DarkBlack() const noexcept; 
        TStringBuf DarkRed() const noexcept; 
        TStringBuf DarkGreen() const noexcept; 
        TStringBuf DarkYellow() const noexcept; 
        TStringBuf DarkBlue() const noexcept; 
        TStringBuf DarkMagenta() const noexcept; 
        TStringBuf DarkCyan() const noexcept; 
        TStringBuf DarkWhite() const noexcept; 
 
        /// Compatibility; prefer using methods without `Color` suffix in their names. 
        /// Note: these behave differently from their un-suffixed counterparts. 
        /// While functions declared above will reset colors completely, these will only reset foreground color and 
        /// style, without changing the background color and underline/italic settings. Also, names of these functions 
        /// don't conform with standard, e.g. `YellowColor` actually emits the `lite yellow` escape code. 
        TStringBuf OldColor() const noexcept; 
        TStringBuf BoldColor() const noexcept; 
        TStringBuf BlackColor() const noexcept; 
        TStringBuf BlueColor() const noexcept; 
        TStringBuf GreenColor() const noexcept; 
        TStringBuf CyanColor() const noexcept; 
        TStringBuf RedColor() const noexcept; 
        TStringBuf PurpleColor() const noexcept; 
        TStringBuf BrownColor() const noexcept; 
        TStringBuf LightGrayColor() const noexcept; 
        TStringBuf DarkGrayColor() const noexcept; 
        TStringBuf LightBlueColor() const noexcept; 
        TStringBuf LightGreenColor() const noexcept; 
        TStringBuf LightCyanColor() const noexcept; 
        TStringBuf LightRedColor() const noexcept; 
        TStringBuf LightPurpleColor() const noexcept; 
        TStringBuf YellowColor() const noexcept; 
        TStringBuf WhiteColor() const noexcept; 
 
        inline bool IsTTY() const noexcept {
            return IsTTY_;
        }

        inline void SetIsTTY(bool value) noexcept {
            IsTTY_ = value;
        }

        inline void Enable() noexcept {
            SetIsTTY(true);
        }

        inline void Disable() noexcept {
            SetIsTTY(false);
        }

    private:
        bool IsTTY_;
    };

    /// Singletone `TColors` instances for stderr/stdout. 
    TColors& StdErr();
    TColors& StdOut();

    /// Choose `TColors` depending on output stream. If passed stream is stderr/stdout, return a corresponding 
    /// singletone. Otherwise, return a disabled singletone (which you can, but should *not* enable). 
    TColors& AutoColors(IOutputStream& os);
 
    /// Calculate total length of all ANSI escape codes in the text. 
    size_t TotalAnsiEscapeCodeLen(TStringBuf text); 
}
 
TStringBuf ToStringBuf(NColorizer::EAnsiCode x); 
TString ToString(NColorizer::EAnsiCode x);