Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
text_format.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/text.hpp>
6
7#include <format>
8#include <utility>
9
10namespace portals {
11
21template <class... Args>
22std::expected<GraphemeText, TextError> formatText(WidthPolicy policy, std::format_string<Args...> pattern,
23 Args &&...args) {
24 return GraphemeText::from(std::format(pattern, std::forward<Args>(args)...), policy);
25}
26
35template <class... Args>
36std::expected<GraphemeText, TextError> formatText(std::format_string<Args...> pattern, Args &&...args) {
37 return formatText(WidthPolicy{}, pattern, std::forward<Args>(args)...);
38}
39
46template <class T> class CellFormat {
47public:
51 explicit CellFormat(std::format_string<const T &> pattern, WidthPolicy policy = {})
52 : pattern_(pattern.get()), policy_(policy) {
53 }
54
61 GraphemeText operator()(const T &value) const {
62 return GraphemeText::from(std::vformat(pattern_, std::make_format_args(value)), policy_).value();
63 }
64
65private:
66 std::string pattern_;
67 WidthPolicy policy_;
68};
69
76template <class T> CellFormat<T> cellFormat(std::format_string<const T &> pattern, WidthPolicy policy = {}) {
77 return CellFormat<T>(pattern, policy);
78}
79
80} // namespace portals
Owning single-value formatter for the existing table callback contract.
Definition text_format.hpp:46
CellFormat(std::format_string< const T & > pattern, WidthPolicy policy={})
Definition text_format.hpp:51
GraphemeText operator()(const T &value) const
Definition text_format.hpp:61
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
static std::expected< GraphemeText, TextError > from(std::string_view bytes, WidthPolicy policy={}, TextLimits limits={})
Validate, segment and measure a single logical UTF-8 buffer.
Public Portals API.
Definition action.hpp:8
CellFormat< T > cellFormat(std::format_string< const T & > pattern, WidthPolicy policy={})
Build a reusable column formatter without a formatting lambda.
Definition text_format.hpp:76
std::expected< GraphemeText, TextError > formatText(WidthPolicy policy, std::format_string< Args... > pattern, Args &&...args)
Format and validate an owning text value with an explicit width policy.
Definition text_format.hpp:22
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42