ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Table.hpp
Go to the documentation of this file.
1
10#pragma once
11
12#include <string>
13#include <vector>
14
24 enum class Align { left, right };
25
31 inline int visual_width(std::string const& s) {
32 int width = 0;
33 bool in_escape = false;
34
35 for (char c : s) {
36 if (in_escape) {
37 if (c == 'm')
38 in_escape = false;
39 } else if (c == '\033') {
40 in_escape = true;
41 } else {
42 ++width;
43 }
44 }
45
46 return width;
47 }
48
55 inline std::string pad(std::string const& s, int width, Align align = Align::right) {
56 int gap = width - visual_width(s);
57
58 if (gap <= 0)
59 return s;
60
61 std::string spaces(static_cast<std::size_t>(gap), ' ');
62
63 return align == Align::right ? spaces + s : s + spaces;
64 }
65
69 struct Column {
70 std::string label;
71 int width;
73 };
74
76 using Row = std::vector<std::string>;
77
83 inline std::string format_row(std::vector<Column> const& columns, Row const& cells, std::string const& sep = " ") {
84 std::string line;
85
86 for (std::size_t i = 0; i < columns.size() && i < cells.size(); ++i) {
87 if (i > 0)
88 line += sep;
89
90 line += pad(cells[i], columns[i].width, columns[i].align);
91 }
92
93 return line;
94 }
95
99 inline std::string format_separator(std::vector<Column> const& columns, std::string const& sep = " ") {
100 int total = 0;
101
102 for (std::size_t i = 0; i < columns.size(); ++i) {
103 if (i > 0)
104 total += static_cast<int>(sep.size());
105
106 total += columns[i].width;
107 }
108
109 return std::string(static_cast<std::size_t>(total), '-');
110 }
111
115 inline Row header_labels(std::vector<Column> const& columns) {
116 Row labels;
117 labels.reserve(columns.size());
118
119 for (auto const& col : columns) {
120 labels.push_back(col.label);
121 }
122
123 return labels;
124 }
125
129 inline std::string format_md_row(Row const& cells) {
130 std::string line = "|";
131
132 for (auto const& cell : cells) {
133 line += " " + cell + " |";
134 }
135
136 return line;
137 }
138
142 inline std::string format_md_separator(std::vector<Column> const& columns) {
143 std::string line = "|";
144
145 for (auto const& col : columns) {
146 line += (col.align == Align::right) ? "---:|" : ":---|";
147 }
148
149 return line;
150 }
151}
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