-
Notifications
You must be signed in to change notification settings - Fork 2
/
trmclr.hpp
128 lines (106 loc) · 3.01 KB
/
trmclr.hpp
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
#ifndef __TRMCLR_H__
#define __TRMCLR_H__
#include <cstdint>
#include <ostream>
#include <sstream>
#include <limits>
#include <string>
namespace trmclr
{
struct Style
{
Style(uint32_t value) : _value(value) {}
operator uint32_t() const { return _value; }
uint32_t _value;
};
enum StyleTypes
{
FOREGROUND,
ATTRIBUTE,
BACKGROUND,
N_STYLE_TYPES
};
static const uint32_t STYLE_SHIFT = std::numeric_limits<uint32_t>::digits / N_STYLE_TYPES;
struct Attribute
{
static const uint32_t SHIFT = STYLE_SHIFT * ATTRIBUTE;
enum
{
DEFAULT = 0x001 << SHIFT,
BOLD = 0x002 << SHIFT,
DIM = 0x004 << SHIFT,
UNDERLINED = 0x010 << SHIFT,
BLINK = 0x020 << SHIFT,
REVERSE = 0x080 << SHIFT,
HIDDEN = 0x100 << SHIFT
};
};
struct Foreground
{
static const uint32_t SHIFT = STYLE_SHIFT * FOREGROUND;
enum
{
BLACK = 30 << SHIFT,
RED = 31 << SHIFT,
GREEN = 32 << SHIFT,
YELLOW = 33 << SHIFT,
BLUE = 34 << SHIFT,
MAGENTA = 35 << SHIFT,
CYAN = 36 << SHIFT,
LIGHT_GRAY = 37 << SHIFT,
DEFAULT = 39 << SHIFT,
DARK_GRAY = 90 << SHIFT,
LIGHT_RED = 91 << SHIFT,
LIGHT_GREEN = 92 << SHIFT,
LIGHT_YELLOW = 93 << SHIFT,
LIGHT_BLUE = 94 << SHIFT,
LIGHT_MAGENTA = 95 << SHIFT,
LIGHT_CYAN = 96 << SHIFT,
WHITE = 97 << SHIFT
};
};
struct Background
{
static const uint32_t SHIFT = STYLE_SHIFT * BACKGROUND;
enum
{
BLACK = 40 << SHIFT,
RED = 41 << SHIFT,
GREEN = 42 << SHIFT,
YELLOW = 43 << SHIFT,
BLUE = 44 << SHIFT,
MAGENTA = 45 << SHIFT,
CYAN = 46 << SHIFT,
LIGHT_GRAY = 47 << SHIFT,
DEFAULT = 49 << SHIFT,
DARK_GRAY = 100 << SHIFT,
LIGHT_RED = 101 << SHIFT,
LIGHT_GREEN = 102 << SHIFT,
LIGHT_YELLOW = 103 << SHIFT,
LIGHT_BLUE = 104 << SHIFT,
LIGHT_MAGENTA = 105 << SHIFT,
LIGHT_CYAN = 106 << SHIFT,
WHITE = 107 << SHIFT
};
};
std::ostream& operator<< (std::ostream& os, const Style& style)
{
const uint32_t base = 1 << STYLE_SHIFT;
uint32_t encoded = style / base;
uint32_t decoded = style % base;
os << "\x1B[" << (decoded ? decoded : Foreground::DEFAULT >> Foreground::SHIFT);
decoded = encoded % base;
for (uint32_t i = 0; decoded != 0; decoded >>= 1, i++)
{
if (decoded & 1)
{
os << ";" << i;
}
}
encoded = encoded / base;
decoded = encoded % base;
os << ";" << (decoded ? decoded : Background::DEFAULT >> Background::SHIFT) << "m";
return os;
}
} // end trmclr namespace
#endif // end __TRMCLR_H__