Porytiles
Loading...
Searching...
No Matches
text_formatter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <sstream>
6#include <string>
7#include <type_traits>
8#include <utility>
9#include <vector>
10
11namespace porytiles2 {
12
25enum class Style : std::uint32_t {
26 none = 0,
27 bold = 1 << 0,
28 italic = 1 << 1,
29 red = 1 << 2,
30 green = 1 << 3,
31 blue = 1 << 4,
32 yellow = 1 << 5,
33 cyan = 1 << 6,
34 magenta = 1 << 7
35};
36
48[[nodiscard]] constexpr Style operator|(Style lhs, Style rhs)
49{
50 return static_cast<Style>(static_cast<std::uint32_t>(lhs) | static_cast<std::uint32_t>(rhs));
51}
52
64[[nodiscard]] constexpr Style operator&(Style lhs, Style rhs)
65{
66 return static_cast<Style>(static_cast<std::uint32_t>(lhs) & static_cast<std::uint32_t>(rhs));
67}
68
79constexpr Style &operator|=(Style &lhs, Style rhs)
80{
81 lhs = lhs | rhs;
82 return lhs;
83}
84
95constexpr Style &operator&=(Style &lhs, Style rhs)
96{
97 lhs = lhs & rhs;
98 return lhs;
99}
100
112[[nodiscard]] constexpr bool has_style(Style styles, Style flag)
113{
114 return (styles & flag) != Style::none;
115}
116
133 public:
142 FormatParam(std::string text) : text_{std::move(text)}, styles_{Style::none} {}
143
153 explicit FormatParam(std::string text, Style styles) : text_{std::move(text)}, styles_{styles} {}
154
166 template <typename T>
167 requires(
168 !std::is_same_v<std::decay_t<T>, std::string> && !std::is_same_v<std::decay_t<T>, FormatParam> &&
169 requires(std::ostringstream os, T val) { os << val; })
170 FormatParam(T &&value) : FormatParam(to_string_impl(std::forward<T>(value)))
171 {
172 }
173
185 template <typename T>
186 requires(
187 !std::is_same_v<std::decay_t<T>, std::string> && !std::is_same_v<std::decay_t<T>, FormatParam> &&
188 requires(std::ostringstream os, T val) { os << val; })
189 explicit FormatParam(T &&value, Style styles) : FormatParam(to_string_impl(std::forward<T>(value)), styles)
190 {
191 }
192
193 [[nodiscard]] const std::string &text() const
194 {
195 return text_;
196 }
197
198 [[nodiscard]] Style styles() const
199 {
200 return styles_;
201 }
202
203 private:
204 std::string text_;
205 Style styles_;
206
207 template <typename T>
208 static std::string to_string_impl(T &&value)
209 {
210 std::ostringstream oss;
211 oss << std::forward<T>(value);
212 return oss.str();
213 }
214};
215
236 public:
237 virtual ~TextFormatter() = default;
238
251 [[nodiscard]] virtual std::string style(const std::string &text, Style styles) const = 0;
252
270 [[nodiscard]] virtual std::string
271 format(const std::string &format_str, const std::vector<FormatParam> &params) const;
272
296 template <typename FirstParam, typename... RestParams>
297 requires(
298 !std::is_same_v<std::decay_t<FirstParam>, std::vector<FormatParam>> &&
299 std::is_constructible_v<FormatParam, FirstParam> &&
300 (std::is_constructible_v<FormatParam, RestParams> && ...))
301 [[nodiscard]] std::string format(const std::string &format_str, FirstParam &&first, RestParams &&...rest) const
302 {
303 std::vector<FormatParam> param_vector;
304 param_vector.reserve(1 + sizeof...(RestParams));
305 param_vector.emplace_back(std::forward<FirstParam>(first));
306 (param_vector.emplace_back(std::forward<RestParams>(rest)), ...);
307 return this->format(format_str, param_vector);
308 }
309};
310
329using FormattedMessageBuilder = std::function<std::vector<std::string>(const TextFormatter &)>;
330
331} // namespace porytiles2
A text parameter with associated styling for formatted output.
FormatParam(T &&value)
Constructs a FormatParam by converting a value to string.
FormatParam(std::string text, Style styles)
Constructs a FormatParam with styled text.
const std::string & text() const
FormatParam(std::string text)
Constructs a FormatParam with unstyled text.
FormatParam(T &&value, Style styles)
Constructs a FormatParam by converting a value to styled string.
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 std::string format(const std::string &format_str, const std::vector< FormatParam > &params) 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.
virtual ~TextFormatter()=default
Style
Bitmask flags for text styling options.
@ italic
Italic text formatting.
@ none
No styling applied.
@ blue
Blue text color.
@ magenta
Magenta text color.
@ cyan
Cyan text color.
@ bold
Bold text formatting.
@ green
Green text color.
@ red
Red text color.
@ yellow
Yellow text color.
constexpr Style & operator|=(Style &lhs, Style rhs)
Adds Style flags to an existing Style value using bitwise OR.
constexpr Style operator|(Style lhs, Style rhs)
Combines two Style flags using bitwise OR.
constexpr bool has_style(Style styles, Style flag)
Checks if a specific style flag is set in a Style value.
std::function< std::vector< std::string >(const TextFormatter &)> FormattedMessageBuilder
Function type for building formatted messages with TextFormatter access.
constexpr Style operator&(Style lhs, Style rhs)
Masks Style flags using bitwise AND.
constexpr Style & operator&=(Style &lhs, Style rhs)
Masks an existing Style value using bitwise AND.