Porytiles
Loading...
Searching...
No Matches
porytiles::TextFormatter Class Referenceabstract

Abstract base class for applying text styling with context-aware formatting. More...

#include <text_formatter.hpp>

Inheritance diagram for porytiles::TextFormatter:
[legend]

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 > &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

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:

  • style(): Apply Style flags to individual text strings
  • format(): Substitute styled FormatParams into format strings using fmtlib syntax

Implementations:

This class integrates with the error reporting system through FormattableError and UserDiagnostics to provide adaptive styling for diagnostic messages.

Definition at line 529 of file text_formatter.hpp.

Constructor & Destructor Documentation

◆ ~TextFormatter()

virtual porytiles::TextFormatter::~TextFormatter ( )
virtualdefault

Member Function Documentation

◆ format() [1/2]

std::string porytiles::TextFormatter::format ( const std::string &  format_str,
const std::vector< FormatParam > &  params 
) const
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:

++
formatter.format("Error in file '{}'", {FormatParam{filename, Style::bold}})
A text parameter with associated styling for formatted output.
static const Style bold
Bold text formatting.
Parameters
format_strThe format string with {} placeholders
paramsVector of FormatParams to substitute into placeholders
Returns
The formatted string with styled parameters substituted

Definition at line 49 of file text_formatter.cpp.

◆ format() [2/2]

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 porytiles::TextFormatter::format ( const std::string &  format_str,
FirstParam &&  first,
RestParams &&...  rest 
) const
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:

++
formatter.format("Error in file '{}'", FormatParam{filename, Style::bold | Style::red});
formatter.format("Expected {} but got {}", expected, actual);
static const Style red
Red foreground color.

This template is disabled when called with a std::vector<FormatParam> to avoid ambiguity with the base implementation.

Template Parameters
FirstParamType of the first parameter
RestParamsTypes of remaining parameters
Parameters
format_strThe format string with {} placeholders
firstFirst FormatParam argument
restRemaining FormatParam arguments to substitute into placeholders
Returns
The formatted string with styled parameters substituted

Definition at line 595 of file text_formatter.hpp.

◆ style()

virtual std::string porytiles::TextFormatter::style ( const std::string &  text,
Style  styles 
) const
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).

Parameters
textThe text to style
stylesThe Style flags to apply
Returns
The styled text string (may be unchanged in PlainTextFormatter)

Implemented in porytiles::PlainTextFormatter, and porytiles::AnsiStyledTextFormatter.


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