Porytiles
Loading...
Searching...
No Matches
porytiles2::AnsiStyledTextFormatter Class Referencefinal

TextFormatter implementation that applies ANSI escape codes for terminal styling. More...

#include <ansi_styled_text_formatter.hpp>

Inheritance diagram for porytiles2::AnsiStyledTextFormatter:
[legend]
Collaboration diagram for porytiles2::AnsiStyledTextFormatter:
[legend]

Public Member Functions

std::string style (const std::string &text, Style styles) const override
 Applies ANSI escape codes to style text according to the specified Style flags.
 
- Public Member Functions inherited from porytiles2::TextFormatter
virtual ~TextFormatter ()=default
 
virtual std::string format (const std::string &format_str, const std::vector< FormatParam > &params) const
 Formats a string with styled parameters using fmtlib syntax.
 
template<typename FirstParam , typename... RestParams>
requires ( !std::is_same_v<std::decay_t<FirstParam>, std::vector<FormatParam>> && std::is_constructible_v<FormatParam, FirstParam> && (std::is_constructible_v<FormatParam, RestParams> && ...))
std::string format (const std::string &format_str, FirstParam &&first, RestParams &&...rest) const
 Formats a string with styled parameters using variadic template syntax.
 

Detailed Description

TextFormatter implementation that applies ANSI escape codes for terminal styling.

AnsiStyledTextFormatter is a concrete implementation of TextFormatter that applies ANSI escape codes to text based on the provided Style flags. This formatter is designed for TTY output where terminal emulators can interpret ANSI codes to display colored and styled text.

Multiple style flags can be combined (e.g., Style::bold | Style::red) to produce text that has both attributes. The styled text is automatically terminated with an ANSI reset code (\033[0m) to prevent styling from bleeding into subsequent output.

Usage context:

  • Interactive terminal sessions (TTY)
  • Terminal-based user interfaces
  • Console diagnostic output
  • Any scenario where ANSI color codes are supported and desired

Definition at line 25 of file ansi_styled_text_formatter.hpp.

Member Function Documentation

◆ style()

std::string porytiles2::AnsiStyledTextFormatter::style ( const std::string &  text,
Style  styles 
) const
overridevirtual

Applies ANSI escape codes to style text according to the specified Style flags.

Wraps the input text with appropriate ANSI escape codes based on the provided Style flags. If Style::none is provided, the text is returned unchanged. Otherwise, the text is prefixed with ANSI codes for each set flag and suffixed with an ANSI reset code.

The ANSI codes are applied in a specific order (bold first, then colors) to ensure consistent rendering across terminal emulators.

Parameters
textThe text to style
stylesThe Style flags specifying which attributes to apply
Returns
The text wrapped with appropriate ANSI escape codes, or unchanged if styles is Style::none

Implements porytiles2::TextFormatter.

Definition at line 28 of file ansi_styled_text_formatter.cpp.


The documentation for this class was generated from the following files: