118 result.format_flags_ = format_flags_ | other.format_flags_;
122 result.fg_predefined_ = other.fg_predefined_;
123 result.fg_rgb_ = other.fg_rgb_;
124 result.has_fg_rgb_ = other.has_fg_rgb_;
127 result.fg_predefined_ = fg_predefined_;
128 result.fg_rgb_ = fg_rgb_;
129 result.has_fg_rgb_ = has_fg_rgb_;
134 result.bg_predefined_ = other.bg_predefined_;
135 result.bg_rgb_ = other.bg_rgb_;
136 result.has_bg_rgb_ = other.has_bg_rgb_;
139 result.bg_predefined_ = bg_predefined_;
140 result.bg_rgb_ = bg_rgb_;
141 result.has_bg_rgb_ = has_bg_rgb_;
154 return (format_flags_ & bold_flag) != 0;
164 return (format_flags_ & faint_flag) != 0;
174 return (format_flags_ & italic_flag) != 0;
184 return (format_flags_ & underline_flag) != 0;
194 return (format_flags_ & blink_flag) != 0;
267 return fg_predefined_;
278 return bg_predefined_;
282 static constexpr std::uint8_t bold_flag = 1 << 0;
283 static constexpr std::uint8_t faint_flag = 1 << 1;
284 static constexpr std::uint8_t italic_flag = 1 << 2;
285 static constexpr std::uint8_t underline_flag = 1 << 3;
286 static constexpr std::uint8_t blink_flag = 1 << 4;
288 std::uint8_t format_flags_{0};
291 RgbColor fg_rgb_{0, 0, 0};
292 RgbColor bg_rgb_{0, 0, 0};
293 bool has_fg_rgb_{
false};
294 bool has_bg_rgb_{
false};
298 enum class FgColorTag { predefined, rgb };
299 enum class BgColorTag { predefined, rgb };
301 constexpr explicit Style(FormatFlagTag tag)
304 case FormatFlagTag::bold:
305 format_flags_ = bold_flag;
307 case FormatFlagTag::faint:
308 format_flags_ = faint_flag;
310 case FormatFlagTag::italic:
311 format_flags_ = italic_flag;
313 case FormatFlagTag::underline:
314 format_flags_ = underline_flag;
316 case FormatFlagTag::blink:
317 format_flags_ = blink_flag;
320 panic(
"unhandled FormatFlagTag value");
328 constexpr explicit Style(FgColorTag, std::uint8_t r, std::uint8_t g, std::uint8_t b)
329 : fg_rgb_{r, g, b}, has_fg_rgb_{true}
333 constexpr explicit Style(BgColorTag, std::uint8_t r, std::uint8_t g, std::uint8_t b)
334 : bg_rgb_{r, g, b}, has_bg_rgb_{true}
339 friend constexpr Style rgb_fg_style(std::uint8_t r, std::uint8_t g, std::uint8_t b);
340 friend constexpr Style rgb_bg_style(std::uint8_t r, std::uint8_t g, std::uint8_t b);
361 return Style{Style::FgColorTag::rgb, r, g, b};
382 return Style{Style::BgColorTag::rgb, r, g, b};
397[[nodiscard]]
constexpr Style rgb_style(std::uint8_t r, std::uint8_t g, std::uint8_t b)
451 template <
typename T>
453 !std::is_same_v<std::decay_t<T>, std::string> && !std::is_same_v<std::decay_t<T>,
FormatParam> &&
454 std::formattable<std::decay_t<T>,
char>)
470 template <
typename T>
472 !std::is_same_v<std::decay_t<T>, std::string> && !std::is_same_v<std::decay_t<T>,
FormatParam> &&
473 std::formattable<std::decay_t<T>,
char>)
478 [[nodiscard]]
const std::string &
text()
const
503 template <
typename T>
504 static std::string resolve_string(T &&value)
506 return std::format(
"{}", std::forward<T>(value));
545 [[nodiscard]]
virtual std::string
style(
const std::string &text,
Style styles)
const = 0;
564 [[nodiscard]]
virtual std::string
565 format(
const std::string &format_str,
const std::vector<FormatParam> ¶ms)
const;
590 template <
typename FirstParam,
typename... RestParams>
592 !std::is_same_v<std::decay_t<FirstParam>, std::vector<FormatParam>> &&
593 std::is_constructible_v<FormatParam, FirstParam> &&
594 (std::is_constructible_v<FormatParam, RestParams> && ...))
595 [[nodiscard]] std::string
format(
const std::string &format_str, FirstParam &&first, RestParams &&...rest)
const
597 std::vector<FormatParam> param_vector;
598 param_vector.reserve(1 +
sizeof...(RestParams));
599 param_vector.emplace_back(std::forward<FirstParam>(first));
600 (param_vector.emplace_back(std::forward<RestParams>(rest)), ...);
601 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)