35 template <
typename Container>
36 std::string
join(
const Container& values, std::string_view separator =
" ") {
37 return fmt::format(
"{}",
fmt::join(values, separator));
44 static std::string
Cell(T&& value, std::string_view format) {
45 return fmt::format(fmt::runtime(format), std::forward<T>(value));
56 template <
typename... Args>
63 std::vector<std::string>
Render()
const
69 std::vector<std::string> result;
70 result.reserve(
_rows.size() + 4);
73 result.push_back(
MakeBorder(widths,
"┌",
"┬",
"┐",
"─"));
79 result.push_back(
MakeBorder(widths,
"├",
"┼",
"┤",
"─"));
82 for (
const auto& row :
_rows)
83 result.push_back(
MakeRow(widths, row));
86 result.push_back(
MakeBorder(widths,
"└",
"┴",
"┘",
"─"));
93 std::vector<std::vector<std::string>>
_rows;
98 std::ostringstream os;
99 os << std::forward<T>(value);
103 static std::string
Truncate(
const std::string_view value,
const std::size_t width)
105 if (value.size() <= width)
return std::string(value);
107 if (width == 0)
return {};
109 if (width == 1)
return "…";
111 return std::string(value.substr(0, width - 1)) +
"…";
115 const std::string_view value,
116 const std::size_t width,
119 if (value.size() >= width){
120 return std::string(value);
122 const std::size_t padding = width - value.size();
126 return std::string(value) + std::string(padding,
' ');
129 return std::string(padding,
' ') + std::string(value);
132 const std::size_t left = padding / 2;
133 const std::size_t right = padding - left;
135 return std::string(left,
' ') + std::string(value) + std::string(right,
' ');
138 return std::string(value);
143 std::vector<std::size_t> widths;
146 for (std::size_t col = 0; col <
_columns.size(); ++col) {
147 auto width =
_columns[col].name.size();
149 for (
const auto& row :
_rows) {
150 if (col < row.size()) {
151 width = std::max(width, row[col].size());
155 width = std::max(width,
_columns[col].minWidth);
158 width = std::min(width,
_columns[col].maxWidth);
160 widths.push_back(width);
168 std::vector<std::string> header(
_columns.size());
169 for (std::size_t i = 0; i <
_columns.size(); i++) {
175 std::string
MakeBorder(
const std::vector<std::size_t>& widths,
176 const std::string_view left,
const std::string_view middle,
177 const std::string_view right,
const std::string_view horizontal)
const {
181 for (std::size_t i = 0; i < widths.size(); ++i) {
182 for (std::size_t j = 0; j < widths[i] + 2; ++j) {
183 result += horizontal;
185 if (i + 1 < widths.size()) result += middle;
192 std::string
MakeRow(
const std::vector<std::size_t>& widths,
193 const std::vector<std::string>& row)
const {
194 std::string result =
"│";
196 for (std::size_t col = 0; col < widths.size(); ++col) {
199 if (col < row.size()){
200 value =
Truncate(row[col], widths[col]);
MaCh3 Logging utilities built on top of SPDLOG.
Creates fancy formatted text tables for terminal output.
static std::string Align(const std::string_view value, const std::size_t width, const Alignment alignment)
Alling to values to table deepening on values.
TablePrinter(std::vector< ColumnSpec > columns)
Construct a table printer with the specified columns.
std::vector< ColumnSpec > _columns
static std::string toString(T &&value)
Convert a value to a string using stream formatting.
std::vector< std::size_t > CalculateWidths() const
std::string MakeBorder(const std::vector< std::size_t > &widths, const std::string_view left, const std::string_view middle, const std::string_view right, const std::string_view horizontal) const
void AddRow(Args &&... args)
Add a row to the table.
static std::string Truncate(const std::string_view value, const std::size_t width)
If name is longer than desired name we truncate for example instead of SuperLongBlarb we have SuperLo...
std::vector< std::vector< std::string > > _rows
std::string MakeRow(const std::vector< std::size_t > &widths, const std::vector< std::string > &row) const
std::vector< std::string > Render() const
Render the complete table, with widths etc. defined by constructor.
std::vector< std::string > MakeHeader() const
Utility helpers used across MaCh3.
static std::string Cell(T &&value, std::string_view format)
Format a value using a runtime fmt format string.
std::string join(const Container &values, std::string_view separator=" ")
Join all values in a container into a single string.
Alignment
Column alignment.
@ Center
Center text within the column.
@ Right
Align text to the right.
@ Left
Align text to the left.
Column specification used by TablePrinter.
std::size_t maxWidth
Maximum column width. Use 0 for no maximum.
std::string name
Column header displayed in the table.
std::size_t minWidth
Minimum column width.
Alignment alignment
Alignment of values within the column.