Specs
Beautiful C++ Test Framework
Loading...
Searching...
No Matches
PrintColor.h
Go to the documentation of this file.
1#pragma once
2
3#include <string_format.h>
4
5#include <cstdint>
6#include <iostream>
7#include <string>
8#include <string_view>
9
11
12 // TODO this shouldn't use the ANSI escape codes on Windows
13 enum class Color {
14 None = 0,
15 Black = 30,
16 Red = 31,
17 Green = 32,
18 Yellow = 33,
19 Blue = 34,
20 Purple = 35,
21 Cyan = 36,
22 LightGray = 37,
23 DarkGray = 90,
24 LightRed = 91,
25 LightGreen = 92,
26 LightYellow = 93,
27 LightBlue = 94,
28 LightPurple = 95,
29 LightCyan = 96,
30 White = 97
31 };
32
33 namespace Unix {
34 enum class Style : uint8_t {
35 Normal = 0,
36 Bold = 1,
37 Dim = 2,
38 Italic = 3,
39 Underline = 4,
40 SlowBlink = 5,
41 RapidBlink = 6,
42 Inverted = 7,
43 Conceal = 8,
44 CrossedOut = 9
45 };
46
47 enum class ForegroundColor : uint8_t {
48 Black = 30,
49 Red = 31,
50 Green = 32,
51 Yellow = 33,
52 Blue = 34,
53 Purple = 35,
54 Cyan = 36,
55 LightGray = 37,
56 DarkGray = 90,
57 LightRed = 91,
58 LightGreen = 92,
59 LightYellow = 93,
60 LightBlue = 94,
61 LightPurple = 95,
62 LightCyan = 96,
63 White = 97
64 };
65
66 enum class BackgroundColor : uint8_t {
67 Black = 40,
68 Red = 41,
69 Green = 42,
70 Yellow = 43,
71 Blue = 44,
72 Purple = 45,
73 Cyan = 46,
74 LightGray = 47,
75 DarkGray = 100,
76 LightRed = 101,
77 LightGreen = 102,
78 LightYellow = 103,
79 LightBlue = 104,
80 LightPurple = 105,
81 LightCyan = 106,
82 White = 107
83 };
84 }
85
86 inline void PrintColor(
87 std::string_view text, Color foreground = Color::None, Color background = Color::None,
89 ) {
90 using namespace Unix;
91
92 std::string output = "\033[";
93
94 if (style != Style::Normal) output += std::to_string(static_cast<std::uint8_t>(style));
95
96 if (foreground != Color::None) {
97 if (style != Style::Normal) output += ";";
98 output += std::to_string(static_cast<std::uint8_t>(foreground));
99 }
100
101 if (background != Color::None) {
102 if (style != Style::Normal || foreground != Color::None) output += ";";
103 output += std::to_string(static_cast<std::uint8_t>(background));
104 }
105
106 output += string_format("m{}\033[0m", text);
107
108 std::cout << output;
109 }
110
111}
void PrintColor(std::string_view text, Color foreground=Color::None, Color background=Color::None, Unix::Style style=Unix::Style::Normal)
Definition PrintColor.h:86