33 bool in_escape =
false;
39 }
else if (c ==
'\033') {
61 std::string spaces(
static_cast<std::size_t
>(gap),
' ');
76 using Row = std::vector<std::string>;
83 inline std::string
format_row(std::vector<Column>
const& columns,
Row const& cells, std::string
const& sep =
" ") {
86 for (std::size_t i = 0; i < columns.size() && i < cells.size(); ++i) {
90 line +=
pad(cells[i], columns[i].width, columns[i].align);
99 inline std::string
format_separator(std::vector<Column>
const& columns, std::string
const& sep =
" ") {
102 for (std::size_t i = 0; i < columns.size(); ++i) {
104 total +=
static_cast<int>(sep.size());
106 total += columns[i].width;
109 return std::string(
static_cast<std::size_t
>(total),
'-');
117 labels.reserve(columns.size());
119 for (
auto const& col : columns) {
120 labels.push_back(col.label);
130 std::string line =
"|";
132 for (
auto const& cell : cells) {
133 line +=
" " + cell +
" |";
143 std::string line =
"|";
145 for (
auto const& col : columns) {
146 line += (col.align ==
Align::right) ?
"---:|" :
":---|";
Column-driven table formatting with ANSI-safe alignment.
Definition Table.hpp:23
std::string format_separator(std::vector< Column > const &columns, std::string const &sep=" ")
Generate a separator line spanning the full table width.
Definition Table.hpp:99
Align
Definition Table.hpp:24
@ right
Definition Table.hpp:24
@ left
Definition Table.hpp:24
std::string pad(std::string const &s, int width, Align align=Align::right)
Pad a string to a target visual width, handling ANSI codes.
Definition Table.hpp:55
int visual_width(std::string const &s)
Compute the visual width of a string, ignoring ANSI escape codes.
Definition Table.hpp:31
std::string format_row(std::vector< Column > const &columns, Row const &cells, std::string const &sep=" ")
Format a row of cells according to column definitions.
Definition Table.hpp:83
std::vector< std::string > Row
A row of pre-formatted cell strings.
Definition Table.hpp:76
Row header_labels(std::vector< Column > const &columns)
Extract header labels from column definitions as a Row.
Definition Table.hpp:115
std::string format_md_row(Row const &cells)
Format a row as a markdown table row.
Definition Table.hpp:129
std::string format_md_separator(std::vector< Column > const &columns)
Generate a markdown alignment row.
Definition Table.hpp:142
Column definition for table formatting.
Definition Table.hpp:69
Align align
Definition Table.hpp:72
int width
Definition Table.hpp:71
std::string label
Definition Table.hpp:70