aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/dbg_output/colorscheme.h
blob: a5b9cf749a829f62c9ba5f695379227f3f09d8be (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
#pragma once

#include "engine.h"
#include <library/cpp/colorizer/output.h>

#ifndef DBG_OUTPUT_DEFAULT_COLOR_SCHEME
#define DBG_OUTPUT_DEFAULT_COLOR_SCHEME NDbgDump::NColorScheme::TPlain
#endif

#define DBG_OUTPUT_COLOR_HANDLER(NAME) \
    template <class S>                 \
    inline void NAME(S& stream)

namespace NDbgDump {
    namespace NColorScheme {
        /// Start by copying this one if you want to define a custom color scheme.
        struct TPlain {
            // Foreground color modifiers
            DBG_OUTPUT_COLOR_HANDLER(Markup) {
                Y_UNUSED(stream);
            }
            DBG_OUTPUT_COLOR_HANDLER(String) {
                Y_UNUSED(stream);
            }
            DBG_OUTPUT_COLOR_HANDLER(Literal) {
                Y_UNUSED(stream);
            }
            DBG_OUTPUT_COLOR_HANDLER(ResetType) {
                Y_UNUSED(stream);
            }

            // Background color modifiers
            DBG_OUTPUT_COLOR_HANDLER(Key) {
                Y_UNUSED(stream);
            }
            DBG_OUTPUT_COLOR_HANDLER(Value) {
                Y_UNUSED(stream);
            }
            DBG_OUTPUT_COLOR_HANDLER(ResetRole) {
                Y_UNUSED(stream);
            }
        };

        /// Use this one if you want colors but are lazy enough to define a custom color scheme.
        /// Be careful enough to use DumpRaw for avoiding an endless recursion.
        /// Enforce controls whether colors should be applied even if stdout is not a TTY.
        template <bool Enforce = false>
        class TEyebleed {
        public:
            TEyebleed() {
                if (Enforce) {
                    Colors.Enable();
                }
            }

            // Foreground color modifiers
            DBG_OUTPUT_COLOR_HANDLER(Markup) {
                stream << DumpRaw(Colors.LightGreenColor());
            }
            DBG_OUTPUT_COLOR_HANDLER(String) {
                stream << DumpRaw(Colors.YellowColor());
            }
            DBG_OUTPUT_COLOR_HANDLER(Literal) {
                stream << DumpRaw(Colors.LightRedColor());
            }
            DBG_OUTPUT_COLOR_HANDLER(ResetType) {
                stream << DumpRaw(Colors.OldColor());
            }

            // Background color modifiers
            // TODO: support backgrounds in library/cpp/colorizer
            DBG_OUTPUT_COLOR_HANDLER(Key) {
                if (Depth++ == 0 && Colors.IsTTY()) {
                    stream << DumpRaw(TStringBuf("\033[42m"));
                }
            }
            DBG_OUTPUT_COLOR_HANDLER(Value) {
                if (Depth++ == 0 && Colors.IsTTY()) {
                    stream << DumpRaw(TStringBuf("\033[44m"));
                }
            }
            DBG_OUTPUT_COLOR_HANDLER(ResetRole) {
                if (--Depth == 0 && Colors.IsTTY()) {
                    stream << DumpRaw(TStringBuf("\033[49m"));
                }
            }

        private:
            NColorizer::TColors Colors;
            size_t Depth = 0;
        };
    }
}

namespace NPrivate {
    template <typename CS>
    struct TColorSchemeContainer {
        CS ColorScheme;
    };
}