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#include <vector>
5
7
11enum class Code {
12 Reset = 0,
13 Bold = 1,
14 Dim = 2,
15 Italic = 3,
16 Underline = 4,
17 Blink = 5,
18 Reverse = 7,
19 Hidden = 8,
20 Strikethrough = 9,
21
22 FgBlack = 30,
23 FgRed = 31,
24 FgGreen = 32,
25 FgYellow = 33,
26 FgBlue = 34,
27 FgMagenta = 35,
28 FgCyan = 36,
29 FgWhite = 37,
30 FgDefault = 39,
31
32 BgBlack = 40,
33 BgRed = 41,
34 BgGreen = 42,
35 BgYellow = 43,
36 BgBlue = 44,
37 BgMagenta = 45,
38 BgCyan = 46,
39 BgWhite = 47,
40 BgDefault = 49,
41
42 FgBrightBlack = 90,
43 FgBrightRed = 91,
44 FgBrightGreen = 92,
45 FgBrightYellow = 93,
46 FgBrightBlue = 94,
47 FgBrightMagenta = 95,
48 FgBrightCyan = 96,
49 FgBrightWhite = 97,
50
51 BgBrightBlack = 100,
52 BgBrightRed = 101,
53 BgBrightGreen = 102,
54 BgBrightYellow = 103,
55 BgBrightBlue = 104,
56 BgBrightMagenta = 105,
57 BgBrightCyan = 106,
58 BgBrightWhite = 107,
59};
60
70std::ostream& operator<<(std::ostream& os, Code code);
71
78struct SyledText {
79 std::vector<Code> style;
80 std::string_view text;
81};
82
93std::ostream& operator<<(std::ostream& os, const SyledText& text);
94
105[[nodiscard]] SyledText style(std::string_view text, const std::vector<Code>& codes);
106
114
123void set_color_enabled(std::ostream& os, bool enabled);
124
131bool is_color_enabled(std::ostream& os);
132
142bool is_colorable(const std::ostream& os);
143
152void auto_enable_color(std::ostream& os);
153
154} // namespace pretty_diagnostics::color
155
156// BSD 3-Clause License
157//
158// Copyright (c) 2025, Timo Behrend
159//
160// Redistribution and use in source and binary forms, with or without
161// modification, are permitted provided that the following conditions are met:
162//
163// 1. Redistributions of source code must retain the above copyright notice, this
164// list of conditions and the following disclaimer.
165//
166// 2. Redistributions in binary form must reproduce the above copyright notice,
167// this list of conditions and the following disclaimer in the documentation
168// and/or other materials provided with the distribution.
169//
170// 3. Neither the name of the copyright holder nor the names of its
171// contributors may be used to endorse or promote products derived from
172// this software without specific prior written permission.
173//
174// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
175// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
176// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
177// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
178// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
179// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
180// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
181// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
182// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
183// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition: color.hpp:6
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.
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.
SyledText style(std::string_view text, const std::vector< Code > &codes)
Creates a styled text object from a text and a set of ANSI codes.
Code
ANSI escape codes for text styling and coloring.
Definition: color.hpp:11
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:78
std::vector< Code > style
Definition: color.hpp:79
std::string_view text
Definition: color.hpp:80