Creates fancy formatted text tables for terminal output.
More...
#include <Manager/TablePrinter.h>
|
| | TablePrinter (std::vector< ColumnSpec > columns) |
| | Construct a table printer with the specified columns. More...
|
| |
| template<typename... Args> |
| void | AddRow (Args &&... args) |
| | Add a row to the table. More...
|
| |
| std::vector< std::string > | Render () const |
| | Render the complete table, with widths etc. defined by constructor. More...
|
| |
|
| std::vector< std::size_t > | CalculateWidths () const |
| |
| std::vector< std::string > | MakeHeader () 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 |
| |
| std::string | MakeRow (const std::vector< std::size_t > &widths, const std::vector< std::string > &row) const |
| |
|
| template<typename T > |
| static std::string | toString (T &&value) |
| | Convert a value to a string using stream formatting. More...
|
| |
| 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 SuperLong... More...
|
| |
| 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. More...
|
| |
Creates fancy formatted text tables for terminal output.
- Author
- Kamil Skwarczynski
Definition at line 50 of file TablePrinter.h.
◆ TablePrinter()
| M3::Utils::TablePrinter::TablePrinter |
( |
std::vector< ColumnSpec > |
columns | ) |
|
|
inlineexplicit |
Construct a table printer with the specified columns.
Definition at line 53 of file TablePrinter.h.
std::vector< ColumnSpec > _columns
◆ AddRow()
template<typename... Args>
| void M3::Utils::TablePrinter::AddRow |
( |
Args &&... |
args | ) |
|
|
inline |
Add a row to the table.
Definition at line 57 of file TablePrinter.h.
static std::string toString(T &&value)
Convert a value to a string using stream formatting.
std::vector< std::vector< std::string > > _rows
◆ Align()
| static std::string M3::Utils::TablePrinter::Align |
( |
const std::string_view |
value, |
|
|
const std::size_t |
width, |
|
|
const Alignment |
alignment |
|
) |
| |
|
inlinestaticprivate |
Alling to values to table deepening on values.
Definition at line 114 of file TablePrinter.h.
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);
@ Center
Center text within the column.
@ Right
Align text to the right.
@ Left
Align text to the left.
◆ CalculateWidths()
| std::vector<std::size_t> M3::Utils::TablePrinter::CalculateWidths |
( |
| ) |
const |
|
inlineprivate |
Definition at line 141 of file TablePrinter.h.
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);
◆ MakeBorder()
| std::string M3::Utils::TablePrinter::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 |
|
inlineprivate |
Definition at line 175 of file TablePrinter.h.
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;
◆ MakeHeader()
| std::vector<std::string> M3::Utils::TablePrinter::MakeHeader |
( |
| ) |
const |
|
inlineprivate |
◆ MakeRow()
| std::string M3::Utils::TablePrinter::MakeRow |
( |
const std::vector< std::size_t > & |
widths, |
|
|
const std::vector< std::string > & |
row |
|
) |
| const |
|
inlineprivate |
Definition at line 192 of file TablePrinter.h.
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]);
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.
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...
◆ Render()
| std::vector<std::string> M3::Utils::TablePrinter::Render |
( |
| ) |
const |
|
inline |
Render the complete table, with widths etc. defined by constructor.
Definition at line 63 of file TablePrinter.h.
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,
"└",
"┴",
"┘",
"─"));
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
std::string MakeRow(const std::vector< std::size_t > &widths, const std::vector< std::string > &row) const
std::vector< std::string > MakeHeader() const
◆ toString()
template<typename T >
| static std::string M3::Utils::TablePrinter::toString |
( |
T && |
value | ) |
|
|
inlinestaticprivate |
Convert a value to a string using stream formatting.
Definition at line 96 of file TablePrinter.h.
98 std::ostringstream os;
99 os << std::forward<T>(value);
◆ Truncate()
| static std::string M3::Utils::TablePrinter::Truncate |
( |
const std::string_view |
value, |
|
|
const std::size_t |
width |
|
) |
| |
|
inlinestaticprivate |
If name is longer than desired name we truncate for example instead of SuperLongBlarb we have SuperLong...
Definition at line 103 of file TablePrinter.h.
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)) +
"…";
◆ _columns
| std::vector<ColumnSpec> M3::Utils::TablePrinter::_columns |
|
private |
◆ _rows
| std::vector<std::vector<std::string> > M3::Utils::TablePrinter::_rows |
|
private |
The documentation for this class was generated from the following file: