|
Porytiles
|
Abstract base class for applying text styling with context-aware formatting. More...
#include <text_formatter.hpp>
Public Member Functions | |
| virtual | ~TextFormatter ()=default |
| virtual std::string | style (const std::string &text, Style styles) const =0 |
| Applies styling to a text string. | |
| virtual std::string | format (const std::string &format_str, const std::vector< FormatParam > ¶ms) 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. | |
Abstract base class for applying text styling with context-aware formatting.
TextFormatter provides a polymorphic interface for applying text styles in a way that adapts to the output context. Concrete implementations can choose whether to apply styling based on factors like TTY detection, allowing the same code to produce styled terminal output or plain text for file output.
The class provides two key capabilities:
Implementations:
This class integrates with the error reporting system through FormattableError and UserDiagnostics to provide adaptive styling for diagnostic messages.
Definition at line 235 of file text_formatter.hpp.
|
virtualdefault |
|
virtual |
Formats a string with styled parameters using fmtlib syntax.
Substitutes styled FormatParams into a format string using fmtlib's formatting system. Each FormatParam's text is first styled using the style() method, then substituted into the corresponding {} placeholder in the format string.
Example:
| format_str | The format string with {} placeholders |
| params | Vector of FormatParams to substitute into placeholders |
Definition at line 11 of file text_formatter.cpp.
|
inline |
Formats a string with styled parameters using variadic template syntax.
Convenience template that allows passing FormatParams directly as arguments instead of wrapping them in a std::vector. This provides more natural syntax for formatting calls with a known number of parameters.
Example:
This template is disabled when called with a std::vector<FormatParam> to avoid ambiguity with the base implementation.
| FirstParam | Type of the first parameter |
| RestParams | Types of remaining parameters |
| format_str | The format string with {} placeholders |
| first | First FormatParam argument |
| rest | Remaining FormatParam arguments to substitute into placeholders |
Definition at line 301 of file text_formatter.hpp.
|
pure virtual |
Applies styling to a text string.
Pure virtual method that concrete formatters must implement to apply the specified Style flags to the given text. The implementation determines whether to actually apply styling (ANSI codes) or return the text unchanged (plain text).
| text | The text to style |
| styles | The Style flags to apply |
Implemented in porytiles2::PlainTextFormatter, and porytiles2::AnsiStyledTextFormatter.