Pretty Diagnostics
Create your own pretty diagnostics
Loading...
Searching...
No Matches
color.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4
6
10enum class Code {
11 Reset = 0,
12 Bold = 1,
13 Dim = 2,
14 Italic = 3,
15 Underline = 4,
16 Blink = 5,
17 Reverse = 7,
18 Hidden = 8,
19 Strikethrough = 9,
20
21 FgBlack = 30,
22 FgRed = 31,
23 FgGreen = 32,
24 FgYellow = 33,
25 FgBlue = 34,
26 FgMagenta = 35,
27 FgCyan = 36,
28 FgWhite = 37,
29 FgDefault = 39,
30
31 BgBlack = 40,
32 BgRed = 41,
33 BgGreen = 42,
34 BgYellow = 43,
35 BgBlue = 44,
36 BgMagenta = 45,
37 BgCyan = 46,
38 BgWhite = 47,
39 BgDefault = 49,
40
41 FgBrightBlack = 90,
42 FgBrightRed = 91,
43 FgBrightGreen = 92,
44 FgBrightYellow = 93,
45 FgBrightBlue = 94,
46 FgBrightMagenta = 95,
47 FgBrightCyan = 96,
48 FgBrightWhite = 97,
49
50 BgBrightBlack = 100,
51 BgBrightRed = 101,
52 BgBrightGreen = 102,
53 BgBrightYellow = 103,
54 BgBrightBlue = 104,
55 BgBrightMagenta = 105,
56 BgBrightCyan = 106,
57 BgBrightWhite = 107,
58};
59
69std::ostream& operator<<(std::ostream& os, Code code);
70
79template <std::size_t N>
80struct SyledText {
81 std::array<Code, N> codes;
82 std::string_view text;
83};
84
96template <std::size_t N>
97std::ostream& operator<<(std::ostream& os, const SyledText<N>& text) {
98 for (const auto code : text.codes) {
99 os << code;
100 }
101 os << text.text << Code::Reset;
102 return os;
103}
104
115template <typename... Codes>
116requires (std::conjunction_v<std::is_same<Codes, Code>...>)
117[[nodiscard]] SyledText<sizeof...(Codes)> STYLE(std::string_view text, Codes... codes) {
118 return SyledText<sizeof...(Codes)>{
119 std::array<Code, sizeof...(Codes)>{codes...},
120 text
121 };
122}
123
131
140void set_color_enabled(std::ostream& os, bool enabled);
141
148bool is_color_enabled(std::ostream& os);
149
159bool is_colorable(const std::ostream& os);
160
169void auto_enable_color(std::ostream& os);
170
171} // namespace pretty_diagnostics::color
172
173// BSD 3-Clause License
174//
175// Copyright (c) 2025, Timo Behrend
176//
177// Redistribution and use in source and binary forms, with or without
178// modification, are permitted provided that the following conditions are met:
179//
180// 1. Redistributions of source code must retain the above copyright notice, this
181// list of conditions and the following disclaimer.
182//
183// 2. Redistributions in binary form must reproduce the above copyright notice,
184// this list of conditions and the following disclaimer in the documentation
185// and/or other materials provided with the distribution.
186//
187// 3. Neither the name of the copyright holder nor the names of its
188// contributors may be used to endorse or promote products derived from
189// this software without specific prior written permission.
190//
191// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
192// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
194// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
195// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
196// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
197// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
198// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
199// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
200// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition: color.hpp:5
bool is_colorable(const std::ostream &os)
Checks whether the given output stream supports colored output.
int color_enabled_index()
Returns a unique std::ios_base::xalloc index used to store per-stream color enable state.
SyledText< sizeof...(Codes)> STYLE(std::string_view text, Codes... codes)
Creates a styled text object from a text and a set of ANSI codes.
Definition: color.hpp:117
std::ostream & operator<<(std::ostream &os, Code code)
Outputs a single ANSI escape code to the given stream.
bool is_color_enabled(std::ostream &os)
Checks whether color output is enabled for the given stream.
Code
ANSI escape codes for text styling and coloring.
Definition: color.hpp:10
void auto_enable_color(std::ostream &os)
Enables or disables color output for a stream based on its capability.
void set_color_enabled(std::ostream &os, bool enabled)
Enables or disables color output for a specific output stream.
Holds a piece of text together with one or more ANSI style codes.
Definition: color.hpp:80
std::string_view text
Definition: color.hpp:82
std::array< Code, N > codes
Definition: color.hpp:81