The Sparta Modeling Framework
Loading...
Searching...
No Matches
Colors.hpp
Go to the documentation of this file.
1// <Colors> -*- C++ -*-
2
3
9#pragma once
11#include <vector>
18#define SPARTA_UNMANAGED_COLOR_NORMAL "\033[0;0m"
19#define SPARTA_UNMANAGED_COLOR_BOLD "\033[0;1m"
20#define SPARTA_UNMANAGED_COLOR_RED "\033[0;31m"
21#define SPARTA_UNMANAGED_COLOR_GREEN "\033[0;32m"
22#define SPARTA_UNMANAGED_COLOR_YELLOW "\033[0;33m"
23#define SPARTA_UNMANAGED_COLOR_BLUE "\033[0;34m"
24#define SPARTA_UNMANAGED_COLOR_MAGENTA "\033[0;35m"
25#define SPARTA_UNMANAGED_COLOR_CYAN "\033[0;36m"
26#define SPARTA_UNMANAGED_COLOR_BRIGHT_RED "\033[1;31m"
27#define SPARTA_UNMANAGED_COLOR_BRIGHT_GREEN "\033[1;32m"
28#define SPARTA_UNMANAGED_COLOR_BRIGHT_YELLOW "\033[1;33m"
29#define SPARTA_UNMANAGED_COLOR_BRIGHT_BLUE "\033[1;34m"
30#define SPARTA_UNMANAGED_COLOR_BRIGHT_MAGENTA "\033[1;35m"
31#define SPARTA_UNMANAGED_COLOR_BRIGHT_CYAN "\033[1;36m"
32#define SPARTA_UNMANAGED_COLOR_BG_RED "\033[0;41m"
33#define SPARTA_UNMANAGED_COLOR_BG_GREEN "\033[0;42m"
34#define SPARTA_UNMANAGED_COLOR_BG_YELLOW "\033[0;43m"
35#define SPARTA_UNMANAGED_COLOR_BG_BLUE "\033[0;44m"
36#define SPARTA_UNMANAGED_COLOR_BG_MAGENTA "\033[0;45m"
37#define SPARTA_UNMANAGED_COLOR_BG_CYAN "\033[0;46m"
38
40#define SPARTA_CURRENT_COLOR_NORMAL sparta::color::ColorScheme::getDefaultScheme().color(Color::Normal)
41#define SPARTA_CURRENT_COLOR_BRIGHT_NORMAL sparta::color::ColorScheme::getDefaultScheme().color(Color::BrightNormal)
42#define SPARTA_CURRENT_COLOR_BG_NORMAL sparta::color::ColorScheme::getDefaultScheme().color(Color::BgNormal)
43#define SPARTA_CURRENT_COLOR_BOLD sparta::color::ColorScheme::getDefaultScheme().color(Color::Bold)
44#define SPARTA_CURRENT_COLOR_BRIGHT_BOLD sparta::color::ColorScheme::getDefaultScheme().color(Color::BrightBold)
45#define SPARTA_CURRENT_COLOR_BG_BOLD sparta::color::ColorScheme::getDefaultScheme().color(Color::BgBold)
46#define SPARTA_CURRENT_COLOR_RED sparta::color::ColorScheme::getDefaultScheme().color(Color::Red)
47#define SPARTA_CURRENT_COLOR_BRIGHT_RED sparta::color::ColorScheme::getDefaultScheme().color(Color::BrightRed)
48#define SPARTA_CURRENT_COLOR_BG_RED sparta::color::ColorScheme::getDefaultScheme().color(Color::BgRed)
49#define SPARTA_CURRENT_COLOR_GREEN sparta::color::ColorScheme::getDefaultScheme().color(Color::Green)
50#define SPARTA_CURRENT_COLOR_BRIGHT_GREEN sparta::color::ColorScheme::getDefaultScheme().color(Color::BrightGreen)
51#define SPARTA_CURRENT_COLOR_BG_GREEN sparta::color::ColorScheme::getDefaultScheme().color(Color::BgGreen)
52#define SPARTA_CURRENT_COLOR_YELLOW sparta::color::ColorScheme::getDefaultScheme().color(Color::Yellow)
53#define SPARTA_CURRENT_COLOR_BRIGHT_YELLOW sparta::color::ColorScheme::getDefaultScheme().color(Color::BrightYellow)
54#define SPARTA_CURRENT_COLOR_BG_YELLOW sparta::color::ColorScheme::getDefaultScheme().color(Color::BgYellow)
55#define SPARTA_CURRENT_COLOR_BLUE sparta::color::ColorScheme::getDefaultScheme().color(Color::Blue)
56#define SPARTA_CURRENT_COLOR_BRIGHT_BLUE sparta::color::ColorScheme::getDefaultScheme().color(Color::BrightBlue)
57#define SPARTA_CURRENT_COLOR_BG_BLUE sparta::color::ColorScheme::getDefaultScheme().color(Color::BgBlue)
58#define SPARTA_CURRENT_COLOR_MAGENTA sparta::color::ColorScheme::getDefaultScheme().color(Color::Magenta)
59#define SPARTA_CURRENT_COLOR_BRIGHT_MAGENTA sparta::color::ColorScheme::getDefaultScheme().color(Color::BrightMagenta)
60#define SPARTA_CURRENT_COLOR_BG_MAGENTA sparta::color::ColorScheme::getDefaultScheme().color(Color::BgMagenta)
61#define SPARTA_CURRENT_COLOR_CYAN sparta::color::ColorScheme::getDefaultScheme().color(Color::Cyan)
62#define SPARTA_CURRENT_COLOR_BRIGHT_CYAN sparta::color::ColorScheme::getDefaultScheme().color(Color::BrightCyan)
63#define SPARTA_CURRENT_COLOR_BG_CYAN sparta::color::ColorScheme::getDefaultScheme().color(Color::BgCyan)
64
65
66static constexpr char const * ALL_COLORS[] = {
68 SPARTA_UNMANAGED_COLOR_BOLD,
69 SPARTA_UNMANAGED_COLOR_RED,
70 SPARTA_UNMANAGED_COLOR_GREEN,
71 SPARTA_UNMANAGED_COLOR_YELLOW,
72 SPARTA_UNMANAGED_COLOR_BLUE,
73 SPARTA_UNMANAGED_COLOR_MAGENTA,
74 SPARTA_UNMANAGED_COLOR_CYAN,
75 SPARTA_UNMANAGED_COLOR_BRIGHT_RED,
76 SPARTA_UNMANAGED_COLOR_BRIGHT_GREEN,
77 SPARTA_UNMANAGED_COLOR_BRIGHT_YELLOW,
78 SPARTA_UNMANAGED_COLOR_BRIGHT_BLUE,
79 SPARTA_UNMANAGED_COLOR_BRIGHT_MAGENTA,
80 SPARTA_UNMANAGED_COLOR_BRIGHT_CYAN,
81 SPARTA_UNMANAGED_COLOR_BG_RED,
82 SPARTA_UNMANAGED_COLOR_BG_GREEN,
83 SPARTA_UNMANAGED_COLOR_BG_YELLOW,
84 SPARTA_UNMANAGED_COLOR_BG_BLUE,
85 SPARTA_UNMANAGED_COLOR_BG_MAGENTA,
86 SPARTA_UNMANAGED_COLOR_BG_CYAN
87
88};
89
91enum class Color {
92 Normal, Bold, Red, Green, Yellow, Blue, Magenta, Cyan,
93 BrightRed, BrightGreen, BrightYellow, BrightBlue,
94 BrightMagenta, BrightCyan, BgRed, BgGreen, BgYellow, BgBlue,
95 BgMagenta, BgCyan
96};
97
98
99
100
101#define SPARTA_CMDLINE_COLOR_NORMAL "" // SPARTA_UNMANAGED_COLOR_NORMAL
102#define SPARTA_CMDLINE_COLOR_ERROR "" // SPARTA_UNMANAGED_COLOR_ERROR
103#define SPARTA_CMDLINE_COLOR_WARNING "" // SPARTA_UNMANAGED_COLOR_YELLOW
104#define SPARTA_CMDLINE_COLOR_GOOD "" // SPARTA_UNMANAGED_COLOR_GOOD
105
106namespace sparta {
107
108namespace color {
109
110
121 public:
124 private:
131 virtual const char* value_(Color color)
132
133 {
134 if(enabled_)
135 {
136 return all_colors_.at((uint32_t)color);
137 }
138 else{
139 return "";
140 }
141 }
142
143 public:
144 virtual ~ColorScheme() {}
146 // color scheme.
148 {
149 return *_GBL_COLOR_SCHEME;
150 }
151
152
154 {
155 // load all the colors.
156 for (char const * c : ALL_COLORS)
157 {
158 all_colors_.emplace_back(c);
159 }
160 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_RED);
161 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_GREEN);
162 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_YELLOW);
163 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_BLUE);
164 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_MAGENTA);
165 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_CYAN);
166 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_BRIGHT_RED);
167 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_BRIGHT_GREEN);
168 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_BRIGHT_YELLOW);
169 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_BRIGHT_BLUE);
170 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_BRIGHT_MAGENTA);
171 basic_colors_.emplace_back(SPARTA_UNMANAGED_COLOR_BRIGHT_CYAN);
172 basic_colors_.emplace_back(static_cast<char const*>(nullptr));
173 }
174
182 virtual void enabled(bool enabled)
183 {
184 enabled_ = enabled;
185 }
186
191 const char* const * nextBasicColor(const char* const * color=nullptr)
192 {
193 if(enabled_)
194 {
195 if (color == nullptr || color == empty_){
196 return basic_colors_.data();
197 }
198
199 const char * const * next = color + 1;
200 if (*next == nullptr){
201 return basic_colors_.data();
202 }
203 return next;
204 }
205 else
206 {
207 return empty_;
208 }
209 }
210
212 const char* color(Color c)
213 {
214 return value_(c);
215 }
216 protected:
217 bool enabled_ = true;
218 char const * empty_[1]={""};
219 std::vector<const char*> all_colors_;
220 std::vector<const char*> basic_colors_;
221 };
222
223
224
225
226} // namespace color
227} // namespace sparta
Color
Define enums for accessing the different colors via a ColorScheme.
Definition Colors.hpp:91
#define SPARTA_UNMANAGED_COLOR_NORMAL
Definition Colors.hpp:18
Exception class for all of Sparta.
Helpers for enforcing StaticInitialization order.
Accessor methods for obtaining color code strings.
Definition Colors.hpp:120
std::vector< const char * > all_colors_
A dummy empty string array to make nextBasicColor work.
Definition Colors.hpp:219
std::vector< const char * > basic_colors_
A list of colors in order.
Definition Colors.hpp:220
const char *const * nextBasicColor(const char *const *color=nullptr)
Find the next basic color after the color inputted.
Definition Colors.hpp:191
virtual void enabled(bool enabled)
would we like to enable or disable colors. By invoking this function with false, all color accessor f...
Definition Colors.hpp:182
static ColorScheme * _GBL_COLOR_SCHEME
The default color scheme singleton.
Definition Colors.hpp:123
char const * empty_[1]
Whether or not we are returning real colors.
Definition Colors.hpp:218
static ColorScheme & getDefaultScheme()
Return the default instance to the global.
Definition Colors.hpp:147
const char * color(Color c)
The accessors that should always be used for colors.
Definition Colors.hpp:212
Macros for handling exponential backoff.