Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
table_column.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/text_format.hpp>
6
7#include <portals/virtual_table.hpp>
8
9#include <array>
10#include <concepts>
11#include <functional>
12#include <type_traits>
13#include <utility>
14
15namespace portals {
22
28template <class T> struct TableColumnSpec {
29 std::string header;
30 std::int32_t width{1};
31 std::function<GraphemeText(const T &)> format;
32 std::function<std::function<GraphemeText(const T &)>(WidthPolicy)> prepareFormat;
34};
35
38 std::string header;
39 std::int32_t width{1};
40};
41
47template <class T, class Projection>
49 std::is_object_v<Projection> && std::copy_constructible<Projection> && std::invocable<Projection &, const T &>;
50
57template <class T, class Projection, class Formatter>
59 ColumnProjection<T, Projection> && std::is_object_v<Formatter> && std::copy_constructible<Formatter> &&
60 requires(Projection &projection, Formatter &formatter, const T &row) {
61 { std::invoke(formatter, std::invoke(projection, row)) } -> std::same_as<GraphemeText>;
62 };
63
82template <class T, class Projection, class Formatter>
84TableColumn<T> tableColumn(TableColumnOptions options, Projection projection, Formatter formatter) {
85 return {options.id, std::move(options.header), options.width,
86 [projection = std::move(projection), formatter = std::move(formatter)](const T &row) mutable -> GraphemeText {
87 return std::invoke(formatter, std::invoke(projection, row));
88 }};
89}
90
100template <class T, class Member, class Formatter>
101 requires ColumnFormatter<T, Member T::*, Formatter>
102TableColumn<T> tableColumn(TableColumnOptions options, Member T::*member, Formatter formatter) {
103 return tableColumn<T, Member T::*, Formatter>(std::move(options), member, std::move(formatter));
104}
105
114inline std::expected<TableColumnOptions, TextError> columnOptions(ColumnId id, std::string_view header,
115 std::int32_t width = 1, WidthPolicy policy = {}) {
116 auto text = GraphemeText::from(header, policy);
117
118 if (!text) {
119 return std::unexpected(text.error());
120 }
121
122 return TableColumnOptions{id, std::move(*text), width};
123}
124
138template <class T, class Member, class Formatter>
140std::expected<TableColumn<T>, TextError> tableColumn(ColumnId id, std::string_view header, Member T::*member,
141 Formatter formatter, std::int32_t width = 1,
142 WidthPolicy policy = {}) {
143 auto options = columnOptions(id, header, width, policy);
144
145 if (!options) {
146 return std::unexpected(options.error());
147 }
148
149 return tableColumn<T, Member T::*, Formatter>(std::move(*options), member, std::move(formatter));
150}
151
162template <class T, class Projection, class Formatter>
164TableColumnSpec<T> tableColumn(std::string_view header, Projection projection, Formatter formatter,
165 std::int32_t width = 1) {
166 return {std::string(header), width,
167 [projection = std::move(projection), formatter = std::move(formatter)](const T &row) mutable -> GraphemeText {
168 return std::invoke(formatter, std::invoke(projection, row));
169 }};
170}
171
182template <class T, class Member, class Formatter>
183 requires ColumnFormatter<T, Member T::*, Formatter>
184TableColumnSpec<T> tableColumn(std::string_view header, Member T::*member, Formatter formatter,
185 std::int32_t width = 1) {
186 return tableColumn<T, Member T::*, Formatter>(header, member, std::move(formatter), width);
187}
188
195template <class T, class Projection>
196 requires ColumnProjection<T, Projection> &&
197 std::formattable<std::remove_cvref_t<std::invoke_result_t<Projection &, const T &>>, char>
198TableColumnSpec<T> tableColumn(std::string_view header, Projection projection, std::int32_t width = 1) {
199 using Field = std::remove_cvref_t<std::invoke_result_t<Projection &, const T &>>;
200 return {std::string(header),
201 width,
202 {},
203 [projection = std::move(projection)](WidthPolicy policy) -> std::function<GraphemeText(const T &)> {
204 return [projection, formatter = cellFormat<Field>("{}", policy)](const T &row) mutable {
205 return formatter(std::invoke(projection, row));
206 };
207 }};
208}
209
214template <class T, class Member>
215 requires ColumnProjection<T, Member T::*> &&
216 std::formattable<std::remove_cvref_t<std::invoke_result_t<Member T::*&, const T &>>, char>
217TableColumnSpec<T> tableColumn(std::string_view header, Member T::*member, std::int32_t width = 1) {
218 return tableColumn<T, Member T::*>(header, member, width);
219}
220
221namespace detail {
222template <class T, std::size_t N, std::size_t... I>
223std::expected<std::array<TableColumn<T>, N>, TextError>
224assembleColumns(WidthPolicy policy, std::array<TableColumnSpec<T>, N> specs, std::index_sequence<I...>) {
225 constexpr std::uint64_t INITIAL_COLUMN_GENERATION = 1;
226
227 auto built = std::array{[&](std::size_t index) -> std::expected<TableColumn<T>, TextError> {
228 auto header = GraphemeText::from(specs[index].header, policy);
229
230 if (!header) {
231 return std::unexpected(header.error());
232 }
233
234 auto format = specs[index].prepareFormat ? specs[index].prepareFormat(policy) : std::move(specs[index].format);
235 return TableColumn<T>{
236 {index + 1, INITIAL_COLUMN_GENERATION}, std::move(*header), specs[index].width, std::move(format)};
237 }(I)...};
238
239 for (const auto &column : built) {
240 if (!column) {
241 return std::unexpected(column.error());
242 }
243 }
244
245 return std::array<TableColumn<T>, N>{std::move(*built[I])...};
246}
247} // namespace detail
248
262template <class T, class... Rest>
263 requires(std::same_as<std::remove_cvref_t<Rest>, TableColumnSpec<T>> && ...)
264std::expected<std::array<TableColumn<T>, 1 + sizeof...(Rest)>, TextError>
265tableColumns(WidthPolicy policy, TableColumnSpec<T> first, Rest &&...rest) {
266 constexpr std::size_t COLUMN_COUNT = 1 + sizeof...(Rest);
267
268 return detail::assembleColumns(
269 policy, std::array<TableColumnSpec<T>, COLUMN_COUNT>{std::move(first), std::forward<Rest>(rest)...},
270 std::make_index_sequence<COLUMN_COUNT>{});
271}
272
281template <class T, class... Rest>
282 requires(std::same_as<std::remove_cvref_t<Rest>, TableColumnSpec<T>> && ...)
283std::expected<std::array<TableColumn<T>, 1 + sizeof...(Rest)>, TextError> tableColumns(TableColumnSpec<T> first,
284 Rest &&...rest) {
285 return tableColumns(WidthPolicy{}, std::move(first), std::forward<Rest>(rest)...);
286}
287} // namespace portals
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.
GraphemeText(const GraphemeText &other)=default
Copy bytes and metadata into independent storage; may allocate.
A copyable formatter returning exactly owning GraphemeText for a projection result.
Definition table_column.hpp:58
An owning copyable callable that projects a borrowed immutable row.
Definition table_column.hpp:48
consteval auto id(StyleState state=StyleState::ANY, bool readOnly=false)
Definition style_static.hpp:865
Public Portals API.
Definition action.hpp:8
ui::ValueText text(std::format_string< const ui::detail::TextArgumentValue< Args > &... > pattern, Args &&...args)
Declare live formatted text over explicit scalar roots, fields or value properties.
Definition ui_value_text.hpp:567
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< TableColumnOptions, TextError > columnOptions(ColumnId id, std::string_view header, std::int32_t width=1, WidthPolicy policy={})
Construct typed metadata from UTF-8 without unchecked header extraction.
Definition table_column.hpp:114
std::expected< std::array< TableColumn< T >, 1+sizeof...(Rest)>, TextError > tableColumns(WidthPolicy policy, TableColumnSpec< T > first, Rest &&...rest)
Assemble a fixed schema and allocate deterministic table-local identities.
Definition table_column.hpp:265
TableColumn< T > tableColumn(TableColumnOptions options, Projection projection, Formatter formatter)
Compose an owning typed column from explicit projection and text formatting.
Definition table_column.hpp:84
Owning fixed-schema caption and sizing, without identity bookkeeping.
Definition table_column.hpp:37
std::int32_t width
Positive width in terminal cells, validated by table preparation.
Definition table_column.hpp:39
std::string header
UTF-8 copied into the schema; captions may change with locale.
Definition table_column.hpp:38
Provider-assigned column identity; typed row accessors define column values.
Definition model.hpp:24
Owning metadata for a typed column; validation remains part of table preparation.
Definition table_column.hpp:17
ColumnId id
Nonzero unique value/generation pair within the table configuration.
Definition table_column.hpp:18
std::int32_t width
Positive width in terminal cells; columns remain in configured order.
Definition table_column.hpp:20
GraphemeText header
Owning header text using the table's width policy.
Definition table_column.hpp:19
Owning column recipe whose identity is assigned by tableColumns.
Definition table_column.hpp:28
std::string header
Owning UTF-8 caption, validated when the schema is assembled.
Definition table_column.hpp:29
std::int32_t width
Width in terminal cells, validated by table preparation.
Definition table_column.hpp:30
std::function< std::function< GraphemeText(const T &)>(WidthPolicy)> prepareFormat
Optional cold default formatter factory; each assembly owns its independent width policy.
Definition table_column.hpp:32
std::function< GraphemeText(const T &)> format
Owning callback; reference captures remain borrowed.
Definition table_column.hpp:31
A typed owning column definition; identity is independent of visual order.
Definition virtual_table.hpp:25
Owning error value, independent of the failed input's lifetime.
Definition text.hpp:87
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42