108 result.format_flags_ = format_flags_ | other.format_flags_;
112 result.fg_predefined_ = other.fg_predefined_;
113 result.fg_rgb_ = other.fg_rgb_;
114 result.has_fg_rgb_ = other.has_fg_rgb_;
117 result.fg_predefined_ = fg_predefined_;
118 result.fg_rgb_ = fg_rgb_;
119 result.has_fg_rgb_ = has_fg_rgb_;
124 result.bg_predefined_ = other.bg_predefined_;
125 result.bg_rgb_ = other.bg_rgb_;
126 result.has_bg_rgb_ = other.has_bg_rgb_;
129 result.bg_predefined_ = bg_predefined_;
130 result.bg_rgb_ = bg_rgb_;
131 result.has_bg_rgb_ = has_bg_rgb_;
142 return (format_flags_ & bold_flag) != 0;
150 return (format_flags_ & faint_flag) != 0;
158 return (format_flags_ & italic_flag) != 0;
166 return (format_flags_ & underline_flag) != 0;
174 return (format_flags_ & blink_flag) != 0;
233 return fg_predefined_;
242 return bg_predefined_;
246 static constexpr std::uint8_t bold_flag = 1 << 0;
247 static constexpr std::uint8_t faint_flag = 1 << 1;
248 static constexpr std::uint8_t italic_flag = 1 << 2;
249 static constexpr std::uint8_t underline_flag = 1 << 3;
250 static constexpr std::uint8_t blink_flag = 1 << 4;
252 std::uint8_t format_flags_{0};
255 RgbColor fg_rgb_{0, 0, 0};
256 RgbColor bg_rgb_{0, 0, 0};
257 bool has_fg_rgb_{
false};
258 bool has_bg_rgb_{
false};
262 enum class FgColorTag { predefined, rgb };
263 enum class BgColorTag { predefined, rgb };
265 constexpr explicit Style(FormatFlagTag tag)
268 case FormatFlagTag::bold:
269 format_flags_ = bold_flag;
271 case FormatFlagTag::faint:
272 format_flags_ = faint_flag;
274 case FormatFlagTag::italic:
275 format_flags_ = italic_flag;
277 case FormatFlagTag::underline:
278 format_flags_ = underline_flag;
280 case FormatFlagTag::blink:
281 format_flags_ = blink_flag;
284 panic(
"unhandled FormatFlagTag value");
292 constexpr explicit Style(FgColorTag, std::uint8_t r, std::uint8_t g, std::uint8_t b)
293 : fg_rgb_{r, g, b}, has_fg_rgb_{true}
297 constexpr explicit Style(BgColorTag, std::uint8_t r, std::uint8_t g, std::uint8_t b)
298 : bg_rgb_{r, g, b}, has_bg_rgb_{true}
303 friend constexpr Style rgb_fg_style(std::uint8_t r, std::uint8_t g, std::uint8_t b);
304 friend constexpr Style rgb_bg_style(std::uint8_t r, std::uint8_t g, std::uint8_t b);
323 return Style{Style::FgColorTag::rgb, r, g, b};
342 return Style{Style::BgColorTag::rgb, r, g, b};
355[[nodiscard]]
constexpr Style rgb_style(std::uint8_t r, std::uint8_t g, std::uint8_t b)
401 template <
typename T>
403 !std::is_same_v<std::decay_t<T>, std::string> && !std::is_same_v<std::decay_t<T>,
FormatParam> &&
404 std::formattable<std::decay_t<T>,
char>)
418 template <
typename T>
420 !std::is_same_v<std::decay_t<T>, std::string> && !std::is_same_v<std::decay_t<T>,
FormatParam> &&
421 std::formattable<std::decay_t<T>,
char>)
426 [[nodiscard]]
const std::string &
text()
const
449 template <
typename T>
450 static std::string resolve_string(T &&value)
452 return std::format(
"{}", std::forward<T>(value));
483 [[nodiscard]]
virtual std::string
style(
const std::string &text,
Style styles)
const = 0;
500 [[nodiscard]]
virtual std::string
501 format(
const std::string &format_str,
const std::vector<FormatParam> ¶ms)
const;
524 template <
typename FirstParam,
typename... RestParams>
526 !std::is_same_v<std::decay_t<FirstParam>, std::vector<FormatParam>> &&
527 std::is_constructible_v<FormatParam, FirstParam> &&
528 (std::is_constructible_v<FormatParam, RestParams> && ...))
529 [[nodiscard]] std::string
format(
const std::string &format_str, FirstParam &&first, RestParams &&...rest)
const
531 std::vector<FormatParam> param_vector;
532 param_vector.reserve(1 +
sizeof...(RestParams));
533 param_vector.emplace_back(std::forward<FirstParam>(first));
534 (param_vector.emplace_back(std::forward<RestParams>(rest)), ...);
535 return this->
format(format_str, param_vector);
Text styling class supporting formatting, foreground colors, and background colors.
constexpr bool is_bg_rgb() const
Checks if the background color is in RGB mode.
friend constexpr Style rgb_bg_style(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Creates a Style value with a custom RGB background color.
constexpr bool has_faint() const
Checks if this Style has faint formatting.
static const Style bg_green
Green background color.
static const Style white
White foreground color.
static const Style bg_blue
Blue background color.
static const Style faint
Faint text formatting.
static const Style underline
Underline text formatting.
static const Style bg_yellow
Yellow background color.
static const Style italic
Italic text formatting.
static const Style none
No styling applied.
constexpr Style operator|(const Style &other) const
Combines two Style values using the | operator.
static const Style black
Black foreground color.
static const Style bg_black
Black background color.
constexpr bool has_fg_color() const
Checks if this Style has a foreground color set.
constexpr bool has_blink() const
Checks if this Style has blink formatting.
constexpr RgbColor fg_rgb() const
Returns the foreground RGB color.
constexpr bool has_underline() const
Checks if this Style has underline formatting.
constexpr bool has_italic() const
Checks if this Style has italic formatting.
static const Style yellow
Yellow foreground color.
constexpr PredefinedColor bg_predefined() const
Returns the predefined background color.
static const Style bg_white
White background color.
static const Style bg_magenta
Magenta background color.
constexpr PredefinedColor fg_predefined() const
Returns the predefined foreground color.
static const Style magenta
Magenta foreground color.
static const Style bg_cyan
Cyan background color.
constexpr bool has_bold() const
Checks if this Style has bold formatting.
static const Style red
Red foreground color.
static const Style cyan
Cyan foreground color.
static const Style blue
Blue foreground color.
constexpr RgbColor bg_rgb() const
Returns the background RGB color.
static const Style bold
Bold text formatting.
friend constexpr Style rgb_fg_style(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Creates a Style value with a custom RGB foreground color.
constexpr bool is_fg_rgb() const
Checks if the foreground color is in RGB mode.
constexpr bool has_bg_color() const
Checks if this Style has a background color set.
static const Style bg_red
Red background color.
constexpr Style()=default
Default constructor creating a style with no attributes.
static const Style green
Green foreground color.
static const Style blink
Blink text formatting.
Abstract base class for applying text styling with context-aware formatting.
virtual std::string style(const std::string &text, Style styles) const =0
Applies styling to a text string.
virtual ~TextFormatter()=default
virtual std::string format(const std::string &format_str, const std::vector< FormatParam > ¶ms) const
Formats a string with styled parameters using fmtlib syntax.
std::string format(const std::string &format_str, FirstParam &&first, RestParams &&...rest) const
Formats a string with styled parameters using variadic template syntax.
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
constexpr Style rgb_fg_style(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Creates a Style value with a custom RGB foreground color.
constexpr Style rgb_bg_style(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Creates a Style value with a custom RGB background color.
PredefinedColor
Predefined color options for text styling.
std::function< std::vector< std::string >(const TextFormatter &)> FormattedMessageBuilder
Function type for building formatted messages with TextFormatter access.
constexpr Style rgb_style(std::uint8_t r, std::uint8_t g, std::uint8_t b)
Creates a Style value with a custom RGB foreground color (backward compatibility alias).
RGB color representation for extracting color components from Style values.
std::uint8_t b
Blue channel (0-255)
std::uint8_t r
Red channel (0-255)
std::uint8_t g
Green channel (0-255)