Aprepro  5.0x
terminal_color.h
Go to the documentation of this file.
1 #ifndef __TRMCLR_H__
2 #define __TRMCLR_H__
3 
4 // Copyright (c) 2016 Camille Brugel
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgement in the product documentation would be
17 // appreciated but is not required.
18 // 2. Altered source versions must be plainly marked as such, and must not be
19 // misrepresented as being the original software.
20 // 3. This notice may not be removed or altered from any source distribution.
21 
22 #include <cstdint>
23 #include <limits>
24 #include <ostream>
25 #include <sstream>
26 #include <string>
27 
28 // ## Example
29 
30 // ```c++
31 // #include "trmclr.hpp"
32 // #include <iostream>
33 
34 // int main()
35 // {
36 // trmclr::Style fancyStyle(trmclr::Background::LIGHT_BLUE |
37 // trmclr::Foreground::WHITE |
38 // trmclr::Attribute::UNDERLINED |
39 // trmclr::Attribute::BOLD);
40 
41 // trmclr::Style basicStyle(trmclr::Attribute::DEFAULT);
42 
43 // std::cout << fancyStyle << "Hello "
44 // << basicStyle << "World!\n";
45 
46 // return 0;
47 // }
48 
49 // /*
50 // Note you can also do things like:
51 // auto bold = [](trmclr::Style style) { return trmclr::Style(style | trmclr::Attribute::BOLD); };
52 // */
53 // `
54 
55 namespace trmclr {
56 
57  struct Style
58  {
59  Style(uint32_t value) : _value(value) {}
60 
61  operator uint32_t() const { return _value; }
62 
63  uint32_t _value;
64  };
65 
67 
68  static const uint32_t STYLE_SHIFT = std::numeric_limits<uint32_t>::digits / N_STYLE_TYPES;
69 
70  struct Attribute
71  {
72  static const uint32_t SHIFT = STYLE_SHIFT * ATTRIBUTE;
73 
74  enum {
75  DEFAULT = 0x01 << SHIFT,
76  BOLD = 0x02 << SHIFT,
77  DIM = 0x04 << SHIFT,
78  UNDERLINED = 0x08 << SHIFT,
79  BLINK = 0x10 << SHIFT,
80  REVERSE = 0x20 << SHIFT,
81  HIDDEN = 0x40 << SHIFT
82  };
83  };
84 
85  struct Foreground
86  {
87  static const uint32_t SHIFT = STYLE_SHIFT * FOREGROUND;
88 
89  enum {
90  BLACK = 30 << SHIFT,
91  RED = 31 << SHIFT,
92  GREEN = 32 << SHIFT,
93  YELLOW = 33 << SHIFT,
94  BLUE = 34 << SHIFT,
95  MAGENTA = 35 << SHIFT,
96  CYAN = 36 << SHIFT,
97  LIGHT_GRAY = 37 << SHIFT,
98  DEFAULT = 39 << SHIFT,
99  DARK_GRAY = 90 << SHIFT,
100  LIGHT_RED = 91 << SHIFT,
103  LIGHT_BLUE = 94 << SHIFT,
105  LIGHT_CYAN = 96 << SHIFT,
106  WHITE = 97 << SHIFT
107  };
108  };
109 
110  struct Background
111  {
112  static const uint32_t SHIFT = STYLE_SHIFT * BACKGROUND;
113 
114  enum {
115  BLACK = 40 << SHIFT,
116  RED = 41 << SHIFT,
117  GREEN = 42 << SHIFT,
118  YELLOW = 43 << SHIFT,
119  BLUE = 44 << SHIFT,
120  MAGENTA = 45 << SHIFT,
121  CYAN = 46 << SHIFT,
122  LIGHT_GRAY = 47 << SHIFT,
123  DEFAULT = 49 << SHIFT,
124  DARK_GRAY = 100 << SHIFT,
125  LIGHT_RED = 101 << SHIFT,
126  LIGHT_GREEN = 102 << SHIFT,
127  LIGHT_YELLOW = 103 << SHIFT,
128  LIGHT_BLUE = 104 << SHIFT,
130  LIGHT_CYAN = 106 << SHIFT,
131  WHITE = 107 << SHIFT
132  };
133  };
134 
135  static Style black(trmclr::Foreground::BLACK);
136  static Style red(trmclr::Foreground::RED);
137  static Style green(trmclr::Foreground::GREEN);
138  static Style yellow(trmclr::Foreground::YELLOW);
139  static Style blue(trmclr::Foreground::BLUE);
140  static Style magenta(trmclr::Foreground::MAGENTA);
141  static Style cyan(trmclr::Foreground::CYAN);
142  static Style normal(trmclr::Attribute::DEFAULT);
143 
144  inline std::ostream &operator<<(std::ostream &os, const Style &style)
145  {
146  const uint32_t base = 1 << STYLE_SHIFT;
147  uint32_t encoded = style / base;
148  uint32_t decoded = style % base;
149 
150  os << "\x1B[" << (decoded ? decoded : Foreground::DEFAULT >> Foreground::SHIFT);
151 
152  decoded = encoded % base;
153 
154  for (uint32_t i = 0; decoded != 0; decoded >>= 1, i++) {
155  if (decoded & 1) {
156  os << ";" << i;
157  }
158  }
159 
160  encoded = encoded / base;
161  decoded = encoded % base;
162 
163  os << ";" << (decoded ? decoded : Background::DEFAULT >> Background::SHIFT) << "m";
164 
165  return os;
166  }
167 
168 } // namespace trmclr
169 #endif // end __TRMCLR_H__
trmclr::Attribute::DEFAULT
Definition: terminal_color.h:75
trmclr::Foreground::SHIFT
static const uint32_t SHIFT
Definition: terminal_color.h:87
trmclr::normal
static Style normal(trmclr::Attribute::DEFAULT)
trmclr::Foreground::LIGHT_GRAY
Definition: terminal_color.h:97
trmclr::Attribute::BLINK
Definition: terminal_color.h:79
trmclr::N_STYLE_TYPES
Definition: terminal_color.h:66
trmclr::Background::LIGHT_GREEN
Definition: terminal_color.h:126
trmclr::Style
Definition: terminal_color.h:57
trmclr::Background::LIGHT_CYAN
Definition: terminal_color.h:130
trmclr::Foreground::BLACK
Definition: terminal_color.h:90
trmclr::red
static Style red(trmclr::Foreground::RED)
trmclr::FOREGROUND
Definition: terminal_color.h:66
trmclr::green
static Style green(trmclr::Foreground::GREEN)
trmclr::Background::LIGHT_MAGENTA
Definition: terminal_color.h:129
trmclr::Foreground::DEFAULT
Definition: terminal_color.h:98
trmclr::Background::MAGENTA
Definition: terminal_color.h:120
trmclr::ATTRIBUTE
Definition: terminal_color.h:66
trmclr::Attribute::UNDERLINED
Definition: terminal_color.h:78
trmclr::Foreground::CYAN
Definition: terminal_color.h:96
trmclr::Foreground::RED
Definition: terminal_color.h:91
trmclr::Foreground::LIGHT_YELLOW
Definition: terminal_color.h:102
trmclr::cyan
static Style cyan(trmclr::Foreground::CYAN)
trmclr::Foreground::LIGHT_BLUE
Definition: terminal_color.h:103
trmclr::Background::LIGHT_RED
Definition: terminal_color.h:125
trmclr::Background::RED
Definition: terminal_color.h:116
trmclr::operator<<
std::ostream & operator<<(std::ostream &os, const Style &style)
Definition: terminal_color.h:144
trmclr::Background::GREEN
Definition: terminal_color.h:117
trmclr::Background::LIGHT_YELLOW
Definition: terminal_color.h:127
trmclr::Foreground::LIGHT_GREEN
Definition: terminal_color.h:101
trmclr::Background::WHITE
Definition: terminal_color.h:131
trmclr
Definition: terminal_color.h:55
trmclr::Style::Style
Style(uint32_t value)
Definition: terminal_color.h:59
trmclr::Foreground::MAGENTA
Definition: terminal_color.h:95
trmclr::Attribute::DIM
Definition: terminal_color.h:77
trmclr::Background::LIGHT_GRAY
Definition: terminal_color.h:122
trmclr::Background
Definition: terminal_color.h:110
trmclr::Foreground::LIGHT_RED
Definition: terminal_color.h:100
trmclr::Attribute::BOLD
Definition: terminal_color.h:76
trmclr::blue
static Style blue(trmclr::Foreground::BLUE)
trmclr::Background::BLACK
Definition: terminal_color.h:115
trmclr::black
static Style black(trmclr::Foreground::BLACK)
trmclr::Background::CYAN
Definition: terminal_color.h:121
trmclr::Attribute
Definition: terminal_color.h:70
trmclr::Foreground
Definition: terminal_color.h:85
trmclr::Background::DEFAULT
Definition: terminal_color.h:123
trmclr::Background::BLUE
Definition: terminal_color.h:119
trmclr::BACKGROUND
Definition: terminal_color.h:66
trmclr::Foreground::DARK_GRAY
Definition: terminal_color.h:99
trmclr::Foreground::GREEN
Definition: terminal_color.h:92
trmclr::Foreground::WHITE
Definition: terminal_color.h:106
trmclr::Foreground::YELLOW
Definition: terminal_color.h:93
trmclr::Style::_value
uint32_t _value
Definition: terminal_color.h:63
trmclr::Foreground::LIGHT_MAGENTA
Definition: terminal_color.h:104
trmclr::Attribute::SHIFT
static const uint32_t SHIFT
Definition: terminal_color.h:72
trmclr::yellow
static Style yellow(trmclr::Foreground::YELLOW)
trmclr::Background::LIGHT_BLUE
Definition: terminal_color.h:128
trmclr::Background::DARK_GRAY
Definition: terminal_color.h:124
trmclr::magenta
static Style magenta(trmclr::Foreground::MAGENTA)
trmclr::Foreground::BLUE
Definition: terminal_color.h:94
trmclr::Background::SHIFT
static const uint32_t SHIFT
Definition: terminal_color.h:112
trmclr::Attribute::REVERSE
Definition: terminal_color.h:80
trmclr::StyleTypes
StyleTypes
Definition: terminal_color.h:66
trmclr::Attribute::HIDDEN
Definition: terminal_color.h:81
trmclr::STYLE_SHIFT
static const uint32_t STYLE_SHIFT
Definition: terminal_color.h:68
trmclr::Foreground::LIGHT_CYAN
Definition: terminal_color.h:105
trmclr::Background::YELLOW
Definition: terminal_color.h:118