MaCh3  2.8.0
Reference Guide
Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
M3::Utils::TablePrinter Class Reference

Creates fancy formatted text tables for terminal output. More...

#include <Manager/TablePrinter.h>

Collaboration diagram for M3::Utils::TablePrinter:
[legend]

Public Member Functions

 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...
 

Private Member Functions

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
 

Static Private Member Functions

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...
 

Private Attributes

std::vector< ColumnSpec > _columns
 
std::vector< std::vector< std::string > > _rows
 

Detailed Description

Creates fancy formatted text tables for terminal output.

Author
Kamil Skwarczynski

Definition at line 50 of file TablePrinter.h.

Constructor & Destructor Documentation

◆ 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.

53 : _columns(std::move(columns)) {}
std::vector< ColumnSpec > _columns
Definition: TablePrinter.h:92

Member Function Documentation

◆ 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.

57  {
58  _rows.push_back({toString(std::forward<Args>(args))...});
59  }
static std::string toString(T &&value)
Convert a value to a string using stream formatting.
Definition: TablePrinter.h:96
std::vector< std::vector< std::string > > _rows
Definition: TablePrinter.h:93

◆ 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.

118  {
119  if (value.size() >= width){
120  return std::string(value);
121  }
122  const std::size_t padding = width - value.size();
123 
124  switch (alignment) {
125  case Alignment::Left:
126  return std::string(value) + std::string(padding, ' ');
127 
128  case Alignment::Right:
129  return std::string(padding, ' ') + std::string(value);
130 
131  case Alignment::Center: {
132  const std::size_t left = padding / 2;
133  const std::size_t right = padding - left;
134 
135  return std::string(left, ' ') + std::string(value) + std::string(right, ' ');
136  }
137  }
138  return std::string(value);
139  }
@ 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.

142  {
143  std::vector<std::size_t> widths;
144  widths.reserve(_columns.size());
145 
146  for (std::size_t col = 0; col < _columns.size(); ++col) {
147  auto width = _columns[col].name.size();
148 
149  for (const auto& row : _rows) {
150  if (col < row.size()) {
151  width = std::max(width, row[col].size());
152  }
153  }
154 
155  width = std::max(width, _columns[col].minWidth);
156 
157  if (_columns[col].maxWidth != 0){
158  width = std::min(width, _columns[col].maxWidth);
159  }
160  widths.push_back(width);
161  }
162 
163  return widths;
164  }

◆ 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.

177  {
178  std::string result;
179  result += left;
180 
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;
184  }
185  if (i + 1 < widths.size()) result += middle;
186  }
187 
188  result += right;
189  return result;
190  }

◆ MakeHeader()

std::vector<std::string> M3::Utils::TablePrinter::MakeHeader ( ) const
inlineprivate

Definition at line 166 of file TablePrinter.h.

167  {
168  std::vector<std::string> header(_columns.size());
169  for (std::size_t i = 0; i < _columns.size(); i++) {
170  header[i] = _columns[i].name;
171  }
172  return header;
173  }

◆ 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.

193  {
194  std::string result = "│";
195 
196  for (std::size_t col = 0; col < widths.size(); ++col) {
197  std::string value;
198 
199  if (col < row.size()){
200  value = Truncate(row[col], widths[col]);
201  }
202  value = Align(value, widths[col], _columns[col].alignment);
203 
204  result += " ";
205  result += value;
206  result += " │";
207  }
208  return result;
209  }
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.
Definition: TablePrinter.h:114
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...
Definition: TablePrinter.h:103

◆ 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.

64  {
65  if (_columns.empty()) return {};
66 
67  const auto widths = CalculateWidths();
68 
69  std::vector<std::string> result;
70  result.reserve(_rows.size() + 4);
71 
72  // ┌──────┬────────┐
73  result.push_back(MakeBorder(widths, "┌", "┬", "┐", "─"));
74 
75  // │ Name │ Value │
76  result.push_back(MakeRow(widths, MakeHeader()));
77 
78  // ├──────┼────────┤
79  result.push_back(MakeBorder(widths, "├", "┼", "┤", "─"));
80 
81  // data rows
82  for (const auto& row : _rows)
83  result.push_back(MakeRow(widths, row));
84 
85  // └──────┴────────┘
86  result.push_back(MakeBorder(widths, "└", "┴", "┘", "─"));
87 
88  return result;
89  }
std::vector< std::size_t > CalculateWidths() const
Definition: TablePrinter.h:141
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
Definition: TablePrinter.h:175
std::string MakeRow(const std::vector< std::size_t > &widths, const std::vector< std::string > &row) const
Definition: TablePrinter.h:192
std::vector< std::string > MakeHeader() const
Definition: TablePrinter.h:166

◆ 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.

97  {
98  std::ostringstream os;
99  os << std::forward<T>(value);
100  return os.str();
101  }

◆ 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.

104  {
105  if (value.size() <= width) return std::string(value);
106 
107  if (width == 0) return {};
108 
109  if (width == 1) return "…";
110 
111  return std::string(value.substr(0, width - 1)) + "…";
112  }

Member Data Documentation

◆ _columns

std::vector<ColumnSpec> M3::Utils::TablePrinter::_columns
private

Definition at line 92 of file TablePrinter.h.

◆ _rows

std::vector<std::vector<std::string> > M3::Utils::TablePrinter::_rows
private

Definition at line 93 of file TablePrinter.h.


The documentation for this class was generated from the following file: