IOSS 2.0

Ioss_TerminalColor.h
Go to the documentation of this file.
1 #ifndef IOSS__TRMCLR_H__
2 #define IOSS__TRMCLR_H__
3
4 // Copyright(C) 1999-2017 National Technology & Engineering Solutions
5 // of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
6 // NTESS, the U.S. Government retains certain rights in this software.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 //
12 // * Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // * Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided
18 // with the distribution.
19 //
20 // * Neither the name of NTESS nor the names of its
21 // contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 #include <cstdint>
37 #include <limits>
38 #include <ostream>
39 #include <sstream>
40 #include <string>
41
42 // ## Example
43
44 // ```c++
45 // #include "trmclr.hpp"
46 // #include <iostream>
47
48 // int main()
49 // {
50 // trmclr::Style fancyStyle(trmclr::Background::LIGHT_BLUE |
51 // trmclr::Foreground::WHITE |
52 // trmclr::Attribute::UNDERLINED |
53 // trmclr::Attribute::BOLD);
54
55 // trmclr::Style basicStyle(trmclr::Attribute::DEFAULT);
56
57 // std::cout << fancyStyle << "Hello "
58 // << basicStyle << "World!\n";
59
60 // return 0;
61 // }
62
63 // /*
64 // Note you can also do things like:
65 // auto bold = [](trmclr::Style style) { return trmclr::Style(style | trmclr::Attribute::BOLD); };
66 // */
67 // `
68
69 namespace Ioss {
70 namespace trmclr {
71
72 struct Style
73 {
74 explicit Style(uint32_t value) : _value(value) {}
75
76 operator uint32_t() const { return _value; }
77
78 uint32_t _value;
79 };
80
81 enum StyleTypes { FOREGROUND, ATTRIBUTE, BACKGROUND, N_STYLE_TYPES };
82
83 static const uint32_t STYLE_SHIFT = std::numeric_limits<uint32_t>::digits / N_STYLE_TYPES;
84
85 struct Attribute
86 {
87 static const uint32_t SHIFT = STYLE_SHIFT * ATTRIBUTE;
88
89 enum {
90 DEFAULT = 0x001 << SHIFT,
91 BOLD = 0x002 << SHIFT,
92 DIM = 0x004 << SHIFT,
93 UNDERLINED = 0x010 << SHIFT,
94 BLINK = 0x020 << SHIFT,
95 REVERSE = 0x080 << SHIFT,
96 HIDDEN = 0x100 << SHIFT
97 };
98 };
99
100 struct Foreground
101 {
102 static const uint32_t SHIFT = STYLE_SHIFT * FOREGROUND;
103
104 enum {
105 BLACK = 30 << SHIFT,
106 RED = 31 << SHIFT,
107 GREEN = 32 << SHIFT,
108 YELLOW = 33 << SHIFT,
109 BLUE = 34 << SHIFT,
110 MAGENTA = 35 << SHIFT,
111 CYAN = 36 << SHIFT,
112 LIGHT_GRAY = 37 << SHIFT,
113 DEFAULT = 39 << SHIFT,
114 DARK_GRAY = 90 << SHIFT,
115 LIGHT_RED = 91 << SHIFT,
116 LIGHT_GREEN = 92 << SHIFT,
117 LIGHT_YELLOW = 93 << SHIFT,
118 LIGHT_BLUE = 94 << SHIFT,
119 LIGHT_MAGENTA = 95 << SHIFT,
120 LIGHT_CYAN = 96 << SHIFT,
121 WHITE = 97 << SHIFT
122 };
123 };
124
125 struct Background
126 {
127 static const uint32_t SHIFT = STYLE_SHIFT * BACKGROUND;
128
129 enum {
130 BLACK = 40 << SHIFT,
131 RED = 41 << SHIFT,
132 GREEN = 42 << SHIFT,
133 YELLOW = 43 << SHIFT,
134 BLUE = 44 << SHIFT,
135 MAGENTA = 45 << SHIFT,
136 CYAN = 46 << SHIFT,
137 LIGHT_GRAY = 47 << SHIFT,
138 DEFAULT = 49 << SHIFT,
139 DARK_GRAY = 100 << SHIFT,
140 LIGHT_RED = 101 << SHIFT,
141 LIGHT_GREEN = 102 << SHIFT,
142 LIGHT_YELLOW = 103 << SHIFT,
143 LIGHT_BLUE = 104 << SHIFT,
144 LIGHT_MAGENTA = 105 << SHIFT,
145 LIGHT_CYAN = 106 << SHIFT,
146 WHITE = 107 << SHIFT
147 };
148 };
149
150 static Style black(Ioss::trmclr::Foreground::BLACK);
151 static Style red(Ioss::trmclr::Foreground::RED);
152 static Style green(Ioss::trmclr::Foreground::GREEN);
153 static Style yellow(Ioss::trmclr::Foreground::YELLOW);
154 static Style blue(Ioss::trmclr::Foreground::BLUE);
155 static Style magenta(Ioss::trmclr::Foreground::MAGENTA);
156 static Style cyan(Ioss::trmclr::Foreground::CYAN);
157 static Style normal(Ioss::trmclr::Attribute::DEFAULT);
158
159 inline std::ostream &operator<<(std::ostream &os, const Style &style)
160 {
161 const uint32_t base = 1 << STYLE_SHIFT;
162 uint32_t encoded = style / base;
163 uint32_t decoded = style % base;
164
165 os << "\x1B[" << (decoded ? decoded : Foreground::DEFAULT >> Foreground::SHIFT);
166
167 decoded = encoded % base;
168
169 for (uint32_t i = 0; decoded != 0; decoded >>= 1, i++) {
170 if (decoded & 1) {
171 os << ";" << i;
172 }
173 }
174
175 encoded = encoded / base;
176 decoded = encoded % base;
177
178 os << ";" << (decoded ? decoded : Background::DEFAULT >> Background::SHIFT) << "m";
179
180 return os;
181 }
182
183 } // namespace trmclr
184 } // namespace Ioss
185 #endif // end __TRMCLR_H__
Ioss::trmclr::Background::SHIFT
static const uint32_t SHIFT
Definition: Ioss_TerminalColor.h:127
Ioss::trmclr::operator<<
std::ostream & operator<<(std::ostream &os, const Style &style)
Definition: Ioss_TerminalColor.h:159
Ioss::trmclr::Foreground::GREEN
Definition: Ioss_TerminalColor.h:107
Ioss
The main namespace for the Ioss library.
Definition: Iocgns_DatabaseIO.h:50
Ioss::trmclr::Background
Definition: Ioss_TerminalColor.h:125
Ioss::trmclr::ATTRIBUTE
Definition: Ioss_TerminalColor.h:81
Ioss::trmclr::blue
static Style blue(Ioss::trmclr::Foreground::BLUE)
Ioss::trmclr::N_STYLE_TYPES
Definition: Ioss_TerminalColor.h:81
Ioss::trmclr::Background::DEFAULT
Definition: Ioss_TerminalColor.h:138
Ioss::trmclr::Foreground::BLACK
Definition: Ioss_TerminalColor.h:105
Ioss::trmclr::Foreground
Definition: Ioss_TerminalColor.h:100
Ioss::trmclr::Foreground::SHIFT
static const uint32_t SHIFT
Definition: Ioss_TerminalColor.h:102
Ioss::trmclr::Style::Style
Style(uint32_t value)
Definition: Ioss_TerminalColor.h:74
Ioss::trmclr::STYLE_SHIFT
static const uint32_t STYLE_SHIFT
Definition: Ioss_TerminalColor.h:83
Ioss::trmclr::Attribute::DEFAULT
Definition: Ioss_TerminalColor.h:90
Ioss::trmclr::FOREGROUND
Definition: Ioss_TerminalColor.h:81
Ioss::trmclr::StyleTypes
StyleTypes
Definition: Ioss_TerminalColor.h:81
Ioss::trmclr::Style::_value
uint32_t _value
Definition: Ioss_TerminalColor.h:78
Ioss::trmclr::yellow
static Style yellow(Ioss::trmclr::Foreground::YELLOW)
Ioss::trmclr::Foreground::YELLOW
Definition: Ioss_TerminalColor.h:108
Ioss::trmclr::Foreground::RED
Definition: Ioss_TerminalColor.h:106
Ioss::trmclr::BACKGROUND
Definition: Ioss_TerminalColor.h:81
Ioss::trmclr::Foreground::DEFAULT
Definition: Ioss_TerminalColor.h:113
Ioss::trmclr::black
static Style black(Ioss::trmclr::Foreground::BLACK)
Ioss::trmclr::Style
Definition: Ioss_TerminalColor.h:72
Ioss::trmclr::normal
static Style normal(Ioss::trmclr::Attribute::DEFAULT)
Ioss::trmclr::green
static Style green(Ioss::trmclr::Foreground::GREEN)
Ioss::trmclr::red
static Style red(Ioss::trmclr::Foreground::RED)
Ioss::trmclr::magenta
static Style magenta(Ioss::trmclr::Foreground::MAGENTA)
Iohb::DEFAULT
Definition: Iohb_DatabaseIO.h:74
Ioss::trmclr::Foreground::BLUE
Definition: Ioss_TerminalColor.h:109
Ioss::trmclr::Foreground::MAGENTA
Definition: Ioss_TerminalColor.h:110
Ioss::trmclr::Attribute
Definition: Ioss_TerminalColor.h:85
Ioss::trmclr::Foreground::CYAN
Definition: Ioss_TerminalColor.h:111
Ioss::trmclr::cyan
static Style cyan(Ioss::trmclr::Foreground::CYAN)