aboutsummaryrefslogtreecommitdiff
path: root/deps/inja/third_party/include/hayai/hayai_console.hpp
blob: 6639890b0c13b1457b89a7ecce2202b20deaba2f (plain)
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
#ifndef __HAYAI_CONSOLE
#define __HAYAI_CONSOLE

#include <iostream>

#if !defined(HAYAI_NO_COLOR)
#    if defined(_WIN32)
#        ifndef NOMINMAX
#            define NOMINMAX
#        endif
#        include <windows.h>
#    else
#        include <unistd.h>
#        include <cstdio>
#    endif
#endif


namespace hayai
{
    /// Static helper class for outputting to a terminal/console.
    class Console
    {
    public:
        /// Console text colors.
        enum TextColor
        {
            /// Default console color. Used for resets.
            TextDefault,

            /// Black.
            ///
            /// @warning Avoid using black unless it is absolutely necesssary.
            TextBlack,

            /// Blue.
            TextBlue,

            /// Green.
            TextGreen,

            /// Cyan.
            TextCyan,

            /// Red.
            TextRed,

            /// Purple.
            TextPurple,

            /// Yellow.
            TextYellow,

            /// White.
            ///
            /// @warning Avoid using white unless it is absolutely necessary.
            TextWhite
        };


        /// Get the singleton instance of @ref Console.

        /// @returns a reference to the singleton instance of the
        /// benchmarker execution controller.
        inline static Console& Instance()
        {
            static Console singleton;
            return singleton;
        }


        /// Test if formatting is enabled.
        inline static bool IsFormattingEnabled()
        {
            return Instance()._formattingEnabled;
        }


        /// Set whether formatting is enabled.
        inline static void SetFormattingEnabled(bool enabled)
        {
            Instance()._formattingEnabled = enabled;
        }
    private:
        inline Console()
            :   _formattingEnabled(true)
        {

        }


        bool _formattingEnabled;
    };

#if defined(_WIN32) && !defined(HAYAI_NO_COLOR) // Windows
    static inline WORD GetConsoleAttributes()
    {
        CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
                                   &consoleInfo);
        return consoleInfo.wAttributes;
    }

    inline std::ostream& operator <<(std::ostream& stream,
                                     const Console::TextColor& color)
    {
        static const WORD defaultConsoleAttributes =
            GetConsoleAttributes();
        WORD newColor;

        if ((!Console::IsFormattingEnabled()) ||
            ((stream.rdbuf() != std::cout.rdbuf()) &&
             (stream.rdbuf() != std::cerr.rdbuf())))
            return stream;

        switch(color)
        {
            case Console::TextDefault:
                newColor = defaultConsoleAttributes;
                break;
            case Console::TextBlack:
                newColor = 0;
                break;
            case Console::TextBlue:
                newColor = FOREGROUND_BLUE;
                break;
            case Console::TextGreen:
                newColor = FOREGROUND_GREEN;
                break;
            case Console::TextCyan:
                newColor = FOREGROUND_GREEN | FOREGROUND_BLUE;
                break;
            case Console::TextRed:
                newColor = FOREGROUND_RED;
                break;
            case Console::TextPurple:
                newColor = FOREGROUND_RED | FOREGROUND_BLUE;
                break;
            case Console::TextYellow:
                newColor =
                    FOREGROUND_RED |
                    FOREGROUND_GREEN |
                    FOREGROUND_INTENSITY;
                break;
            case Console::TextWhite:
                newColor =
                    FOREGROUND_RED |
                    FOREGROUND_GREEN |
                    FOREGROUND_BLUE |
                    FOREGROUND_INTENSITY;
                break;
        }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), newColor);
        return stream;
    }
#elif !defined(HAYAI_NO_COLOR) // Linux or others
    inline std::ostream& operator <<(std::ostream& stream,
                                     const Console::TextColor& color)
    {
        static const bool outputNoColor = isatty(fileno(stdout)) != 1;

        if ((!Console::IsFormattingEnabled()) ||
            (outputNoColor) ||
            ((stream.rdbuf() != std::cout.rdbuf()) &&
             (stream.rdbuf() != std::cerr.rdbuf())))
            return stream;

        const char* value = "";
        switch(color) {
            case Console::TextDefault:
                value = "\033[m"; break;
            case Console::TextBlack:
                value = "\033[0;30m"; break;
            case Console::TextBlue:
                value = "\033[0;34m"; break;
            case Console::TextGreen:
                value = "\033[0;32m"; break;
            case Console::TextCyan:
                value = "\033[0;36m"; break;
            case Console::TextRed:
                value = "\033[0;31m"; break;
            case Console::TextPurple:
                value = "\033[0;35m"; break;
            case Console::TextYellow:
                value = "\033[0;33m"; break;
            case Console::TextWhite:
                value = "\033[0;37m"; break;
        }
        return stream << value;
    }
#else // No color
    inline std::ostream& operator <<(std::ostream& stream,
                                     const Console::TextColor&)
    {
        return stream;
    }
#endif
}
#endif