microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
ansi.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2//
3// SPDX-License-Identifier: BSD-2-Clause
4
5#pragma once
6
9#include "../microfmt.hpp"
10#include <cstdint>
11#include <string_view>
12
13namespace microfmt::ansi {
14
15// ============================================================================
16// ANSI Color and Attribute Bitflags
17// ============================================================================
18
19enum class color : uint8_t {
20 none = 0,
21 // Standard colors (30-37)
22 black = 30,
23 red = 31,
24 green = 32,
25 yellow = 33,
26 blue = 34,
27 magenta = 35,
28 cyan = 36,
29 white = 37,
30 // Bright / High-Intensity colors (90-97)
31 bright_black = 90, // Gray / Dark Gray
32 bright_red = 91,
33 bright_green = 92,
34 bright_yellow = 93,
35 bright_blue = 94,
36 bright_magenta = 95,
37 bright_cyan = 96,
38 bright_white = 97
39};
40
41enum class attribute : uint8_t {
42 none = 0,
43 bold = 1 << 0,
44 dim = 1 << 1,
45 italic = 1 << 2,
46 underline = 1 << 3,
47 blink = 1 << 4,
48 reverse = 1 << 5
49};
50
51// low-level formatting: raw character pointers and integer width conversions bounded by the buffer sizes checked here.
52RELOCO_BEGIN_UNSAFE_CODE
53[[nodiscard]] constexpr attribute operator|(attribute a, attribute b) noexcept {
54 return static_cast<attribute>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
55}
57
58[[nodiscard]] constexpr bool operator&(attribute a, attribute b) noexcept {
59 return (static_cast<uint8_t>(a) & static_cast<uint8_t>(b)) != 0;
60}
61
62// ============================================================================
63// Style Descriptor
64// ============================================================================
65
66struct MICROFMT_API_CLASS RELOCO_COPYABLE style {
67 color fg{color::none};
68 color bg{color::none};
69 attribute attr{attribute::none};
70
71 // NOLINTNEXTLINE(reloco-mutable-global): process-wide runtime toggle, set once at start-up before formatting
72 static bool colors_enabled; // Runtime toggle for serial/non-TTY sinks
73};
74
75// NOLINTNEXTLINE(reloco-mutable-global): see the declaration
76inline bool style::colors_enabled = true;
77
78// Reset
79inline constexpr style reset{};
80
81// Standard Foreground Colors
82inline constexpr style fg_black{color::black};
83inline constexpr style fg_red{color::red};
84inline constexpr style fg_green{color::green};
85inline constexpr style fg_yellow{color::yellow};
86inline constexpr style fg_blue{color::blue};
88inline constexpr style fg_cyan{color::cyan};
89inline constexpr style fg_white{color::white};
90
91// Bright Foreground Colors
101
102// Standard Background Colors
111
112// Bright Background Colors
122
123// Text Attributes & Modifiers
130
131// Semantic Status Styles
136
137// ============================================================================
138// Low-Level ANSI Code Emitter
139// ============================================================================
140
141MICROFMT_API void emit_style(const style &s, const sink &out) noexcept;
142MICROFMT_API void emit_reset(const sink &out) noexcept;
143
144// ============================================================================
145// Styled Value Wrapper
146// ============================================================================
147
148template <typename T> struct RELOCO_POINTER styled_view {
149 const T &value;
151};
152
153template <typename T> [[nodiscard]] constexpr styled_view<T> styled(const T &val, style st) noexcept {
154 return styled_view<T>{val, st};
155}
156
157template <typename T> [[nodiscard]] constexpr styled_view<T> red(const T &val) noexcept {
158 return styled_view<T>{val, fg_red};
159}
160
161template <typename T> [[nodiscard]] constexpr styled_view<T> green(const T &val) noexcept {
162 return styled_view<T>{val, fg_green};
163}
164
165template <typename T> [[nodiscard]] constexpr styled_view<T> yellow(const T &val) noexcept {
166 return styled_view<T>{val, fg_yellow};
167}
168
169template <typename T> [[nodiscard]] constexpr styled_view<T> blue(const T &val) noexcept {
170 return styled_view<T>{val, fg_blue};
171}
172
173template <typename T> [[nodiscard]] constexpr styled_view<T> magenta(const T &val) noexcept {
174 return styled_view<T>{val, fg_magenta};
175}
176
177template <typename T> [[nodiscard]] constexpr styled_view<T> cyan(const T &val) noexcept {
178 return styled_view<T>{val, fg_cyan};
179}
180
181template <typename T> [[nodiscard]] constexpr styled_view<T> white(const T &val) noexcept {
182 return styled_view<T>{val, fg_white};
183}
184
185template <typename T> [[nodiscard]] constexpr styled_view<T> gray(const T &val) noexcept {
186 return styled_view<T>{val, fg_gray};
187}
188
189} // namespace microfmt::ansi
190
191#if MICROFMT_SHARED_PROVIDE_DEFINITIONS
192#include "ansi.ipp"
193#endif
194
195// ============================================================================
196// Formatter Specializations
197// ============================================================================
198
199namespace microfmt {
200
201// Direct ANSI style control tokens: microfmt::format_to(out, "{}Text{}",
202// ansi::fg_red, ansi::reset);
203template <> struct formatter<ansi::style> {
204 constexpr void parse(format_parse_context &) noexcept {}
205
206 void format(const ansi::style &st, const sink &out) const noexcept { ansi::emit_style(st, out); }
207};
208
209// Automatic scoped styling: microfmt::format_to(out, "Status: {}",
210// ansi::red("FAILED"));
211template <typename T> struct formatter<ansi::styled_view<T>> {
212 constexpr void parse(format_parse_context &) noexcept {}
213
214 void format(const ansi::styled_view<T> &sv, const sink &out) const noexcept {
215 ansi::emit_style(sv.st, out);
216
217 // Format inner value
219 format_parse_context dummy_ctx("");
220 inner_fmt.parse(dummy_ctx);
221 inner_fmt.format(sv.value, out);
222
223 ansi::emit_reset(out);
224 }
225};
226
227} // namespace microfmt
Definition microfmt.hpp:881
#define MICROFMT_API_CLASS
Definition compat.hpp:181
#define MICROFMT_API
Definition compat.hpp:135
Definition ansi.hpp:13
constexpr style bg_yellow
Definition ansi.hpp:106
MICROFMT_API void emit_reset(const sink &out) noexcept
constexpr style fg_red
Definition ansi.hpp:83
constexpr style bg_blue
Definition ansi.hpp:107
constexpr style fg_green
Definition ansi.hpp:84
constexpr style fg_yellow
Definition ansi.hpp:85
constexpr styled_view< T > gray(const T &val) noexcept
Definition ansi.hpp:185
attribute
Definition ansi.hpp:41
constexpr style fg_bright_yellow
Definition ansi.hpp:96
constexpr style italic
Definition ansi.hpp:126
constexpr style bg_black
Definition ansi.hpp:103
constexpr style bg_bright_black
Definition ansi.hpp:114
constexpr style bg_bright_white
Definition ansi.hpp:121
constexpr style fg_bright_red
Definition ansi.hpp:94
constexpr style fg_cyan
Definition ansi.hpp:88
constexpr styled_view< T > styled(const T &val, style st) noexcept
Definition ansi.hpp:153
constexpr style bg_bright_yellow
Definition ansi.hpp:117
constexpr style bg_red
Definition ansi.hpp:104
color
Definition ansi.hpp:19
constexpr style bold
Definition ansi.hpp:124
constexpr style info_style
Definition ansi.hpp:135
constexpr style fg_bright_blue
Definition ansi.hpp:97
constexpr style fg_gray
Definition ansi.hpp:92
constexpr style fg_blue
Definition ansi.hpp:86
constexpr style bg_cyan
Definition ansi.hpp:109
RELOCO_BEGIN_UNSAFE_CODE constexpr attribute operator|(attribute a, attribute b) noexcept
Definition ansi.hpp:53
constexpr style fg_black
Definition ansi.hpp:82
constexpr style bg_magenta
Definition ansi.hpp:108
constexpr style fg_bright_cyan
Definition ansi.hpp:99
constexpr style reverse
Definition ansi.hpp:129
constexpr style fg_bright_white
Definition ansi.hpp:100
constexpr style fg_magenta
Definition ansi.hpp:87
constexpr style bg_bright_magenta
Definition ansi.hpp:119
constexpr style fg_white
Definition ansi.hpp:89
constexpr style warn_style
Definition ansi.hpp:133
RELOCO_END_UNSAFE_CODE constexpr bool operator&(attribute a, attribute b) noexcept
Definition ansi.hpp:58
constexpr style bg_green
Definition ansi.hpp:105
constexpr style blink
Definition ansi.hpp:128
constexpr style reset
Definition ansi.hpp:79
constexpr style fg_bright_green
Definition ansi.hpp:95
constexpr style bg_bright_blue
Definition ansi.hpp:118
constexpr style fg_bright_magenta
Definition ansi.hpp:98
constexpr style bg_gray
Definition ansi.hpp:113
constexpr style bg_bright_red
Definition ansi.hpp:115
constexpr style ok_style
Definition ansi.hpp:134
constexpr style fg_bright_black
Definition ansi.hpp:93
constexpr style bg_white
Definition ansi.hpp:110
constexpr style underline
Definition ansi.hpp:127
constexpr style bg_bright_cyan
Definition ansi.hpp:120
constexpr style bg_bright_green
Definition ansi.hpp:116
constexpr style dim
Definition ansi.hpp:125
constexpr style error_style
Definition ansi.hpp:132
MICROFMT_API void emit_style(const style &s, const sink &out) noexcept
Definition assert.hpp:19
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
RELOCO_END_UNSAFE_CODE
Definition remote_object.hpp:132
Definition ansi.hpp:66
static bool colors_enabled
Definition ansi.hpp:72
Definition ansi.hpp:148
const T & value
Definition ansi.hpp:149
style st
Definition ansi.hpp:150
void format(const ansi::style &st, const sink &out) const noexcept
Definition ansi.hpp:206
constexpr void parse(format_parse_context &) noexcept
Definition ansi.hpp:204
constexpr void parse(format_parse_context &) noexcept
Definition ansi.hpp:212
void format(const ansi::styled_view< T > &sv, const sink &out) const noexcept
Definition ansi.hpp:214
Definition microfmt.hpp:985