Porytiles
Loading...
Searching...
No Matches
text_formatter.cpp
Go to the documentation of this file.
2
3#include <string>
4#include <vector>
5
6#include "fmt/args.h"
7#include "fmt/format.h"
8
9namespace porytiles {
10
11// Style static constants - Formatting
12const Style Style::none{};
13const Style Style::bold{Style::FormatFlagTag::bold};
14const Style Style::faint{Style::FormatFlagTag::faint};
15const Style Style::italic{Style::FormatFlagTag::italic};
16const Style Style::underline{Style::FormatFlagTag::underline};
17const Style Style::blink{Style::FormatFlagTag::blink};
18
19// Style static constants - Foreground colors
20const Style Style::black{Style::FgColorTag::predefined, PredefinedColor::black};
21const Style Style::red{Style::FgColorTag::predefined, PredefinedColor::red};
22const Style Style::green{Style::FgColorTag::predefined, PredefinedColor::green};
23const Style Style::yellow{Style::FgColorTag::predefined, PredefinedColor::yellow};
24const Style Style::blue{Style::FgColorTag::predefined, PredefinedColor::blue};
25const Style Style::magenta{Style::FgColorTag::predefined, PredefinedColor::magenta};
26const Style Style::cyan{Style::FgColorTag::predefined, PredefinedColor::cyan};
27const Style Style::white{Style::FgColorTag::predefined, PredefinedColor::white};
28
29// Style static constants - Background colors
30const Style Style::bg_black{Style::BgColorTag::predefined, PredefinedColor::black};
31const Style Style::bg_red{Style::BgColorTag::predefined, PredefinedColor::red};
32const Style Style::bg_green{Style::BgColorTag::predefined, PredefinedColor::green};
33const Style Style::bg_yellow{Style::BgColorTag::predefined, PredefinedColor::yellow};
34const Style Style::bg_blue{Style::BgColorTag::predefined, PredefinedColor::blue};
35const Style Style::bg_magenta{Style::BgColorTag::predefined, PredefinedColor::magenta};
36const Style Style::bg_cyan{Style::BgColorTag::predefined, PredefinedColor::cyan};
37const Style Style::bg_white{Style::BgColorTag::predefined, PredefinedColor::white};
38
39// NOTE: This function uses fmt::dynamic_format_arg_store and fmt::vformat because
40// C++23 std::format does not support runtime-variable argument counts.
41// std::make_format_args requires compile-time known argument counts.
42//
43// Future alternatives:
44// 1. C++26 may add better runtime format argument support
45// 2. Custom placeholder replacement (limited to simple {} placeholders only)
46// 3. Refactor callers to use compile-time known argument counts
47//
48// For now, fmt remains the only dependency that cannot be fully migrated to std::format.
49std::string TextFormatter::format(const std::string &format_str, const std::vector<FormatParam> &params) const
50{
51 fmt::dynamic_format_arg_store<fmt::format_context> store;
52 for (const auto &param : params) {
53 store.push_back(style(param.text(), param.styles()));
54 }
55 return fmt::vformat(format_str, store);
56}
57
58} // namespace porytiles
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.
static const Style black
Black foreground color.
static const Style bg_black
Black background color.
static const Style yellow
Yellow foreground color.
static const Style bg_white
White background color.
static const Style bg_magenta
Magenta background color.
static const Style magenta
Magenta foreground color.
static const Style bg_cyan
Cyan background color.
static const Style red
Red foreground color.
static const Style cyan
Cyan foreground color.
static const Style blue
Blue foreground color.
static const Style bold
Bold text formatting.
static const Style bg_red
Red background color.
static const Style green
Green foreground color.
static const Style blink
Blink text formatting.
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.