ZeroErr
table.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <zeroerr/dbg.h>
5 #include <zeroerr/print.h>
6 
7 #include <ostream>
8 #include <string>
9 #include <vector>
10 
11 namespace zeroerr {
12 
16 struct Card {
17  Card() : title(), width(0), height(0) {}
18  Card(std::string title) : title(title), width(0), height(0) {}
19  Card(unsigned width, unsigned height) : title(), width(width), height(height) {}
20  Card(std::string title, unsigned width, unsigned height)
22  std::string title;
23  unsigned width, height;
24 
25  void show(std::ostream& os, std::string str);
26 };
27 
31 class Table : public Card {
32 public:
33  Table() : Card() {}
34  Table(std::string title) : Card(title) {}
35  Table(unsigned width, unsigned height) : Card(width, height) {}
36  Table(std::string title, unsigned width, unsigned height) : Card(title, width, height) {}
37  ~Table() {}
38 
42  struct Style {
43  Style() {}
44  Style(std::initializer_list<std::string> args) : m_args(args) {}
45  std::vector<std::string> m_args;
46 
47  operator bool() const { return !m_args.empty(); }
48  };
49 
50  static void registerStyle(std::string name, Style style);
51 
75  static Style getStyle(std::string name);
76 
80  struct Config {
81  bool show_tb_border; // show top and bottom border
82  bool show_lr_border; // show left and right border
83  bool show_header_split; // show header split
84  bool show_col_split; // show column split
85  bool show_row_split; // show row split
86  bool show_footer_split; // show footer split
88  : show_tb_border(true),
89  show_lr_border(true),
90  show_header_split(true),
91  show_col_split(true),
92  show_row_split(true),
93  show_footer_split(true) {}
94  };
95 
101  std::string str(Config config = Config(), Style style = Table::getStyle("square_double_head"));
102 
103  void set_header(std::vector<std::string> _header) { header = _header; }
104  void add_row(std::initializer_list<std::string> _row) { cells.push_back(_row); }
105 
106  template <typename T, typename... Args>
107  void push_back(std::vector<std::string>& row, T&& t, Args&&... args) {
108  _push_back(rank<2>{}, row, std::forward<T>(t));
109  push_back(row, std::forward<Args>(args)...);
110  }
111 
112  template <typename T>
113  void push_back(std::vector<std::string>& row, T&& t) {
114  _push_back(rank<2>{}, row, std::forward<T>(t));
115  }
116 
117 
118  template <typename... Args>
119  void add_row(Args&&... args) {
120  std::vector<std::string> row;
121  push_back(row, std::forward<Args>(args)...);
122  cells.push_back(row);
123  }
124 
125  template <typename T, typename... Args>
126  void add_rows(T&& t, Args&&... args) {
127  add_row(std::forward<T>(t));
128  add_rows(std::forward<Args>(args)...);
129  }
130 
131  template <typename T>
132  void add_rows(T&& t) {
133  add_row(std::forward<T>(t));
134  }
135 
136 protected:
138  _push_back(rank<0>, std::vector<std::string>& row, T&& t) {
139  Printer print;
140  print.isCompact = true;
141  print.isQuoted = false;
142  print.line_break = "";
143  print(std::forward<T>(t));
144  row.push_back(print.str());
145  }
146 
148  _push_back(rank<2>, std::vector<std::string>& row, T t) {
149  row.push_back(std::forward<std::string>(t));
150  }
151 
153  _push_back(rank<1>, std::vector<std::string>& row, const T& t) {
154  for (auto& ele : t) {
155  Printer print;
156  print.isCompact = true;
157  print.isQuoted = false;
158  print.line_break = "";
159  print(ele);
160  row.push_back(print.str());
161  }
162  }
163 
164  std::vector<unsigned> col_width;
165  std::vector<std::string> header, footer;
166  std::vector<std::vector<std::string>> cells;
167 };
168 
169 
170 } // namespace zeroerr
Table is used to generate a table with configurable style.
Definition: table.h:31
void push_back(std::vector< std::string > &row, T &&t, Args &&... args)
Definition: table.h:107
void add_rows(T &&t, Args &&... args)
Definition: table.h:126
void add_row(std::initializer_list< std::string > _row)
Definition: table.h:104
Table(std::string title)
Definition: table.h:34
_push_back(rank< 0 >, std::vector< std::string > &row, T &&t)
Definition: table.h:138
std::vector< std::string > header
Definition: table.h:165
void push_back(std::vector< std::string > &row, T &&t)
Definition: table.h:113
Table(std::string title, unsigned width, unsigned height)
Definition: table.h:36
void add_rows(T &&t)
Definition: table.h:132
Table(unsigned width, unsigned height)
Definition: table.h:35
Table()
Definition: table.h:33
std::vector< std::vector< std::string > > cells
Definition: table.h:166
~Table()
Definition: table.h:37
std::string str(Config config=Config(), Style style=Table::getStyle("square_double_head"))
str is used to generate the table string.
Definition: table.cpp:276
std::vector< std::string > footer
Definition: table.h:165
static void registerStyle(std::string name, Style style)
Definition: table.cpp:244
void set_header(std::vector< std::string > _header)
Definition: table.h:103
std::vector< unsigned > col_width
Definition: table.h:164
static Style getStyle(std::string name)
getStyle can load predefined style from the StyleManager.
Definition: table.cpp:247
void add_row(Args &&... args)
Definition: table.h:119
Definition: benchmark.cpp:17
Card defines a display range in the console.
Definition: table.h:16
void show(std::ostream &os, std::string str)
std::string title
Definition: table.h:22
Card()
Definition: table.h:17
Card(std::string title, unsigned width, unsigned height)
Definition: table.h:20
Card(std::string title)
Definition: table.h:18
unsigned height
Definition: table.h:23
unsigned width
Definition: table.h:23
Card(unsigned width, unsigned height)
Definition: table.h:19
A functor class Printer for printing a value of any type.
Definition: print.h:80
bool isQuoted
Definition: print.h:123
std::string str() const
Definition: print.h:270
bool isCompact
Definition: print.h:122
const char * line_break
Definition: print.h:125
Config is used to configure the table style how it is displayed.
Definition: table.h:80
bool show_row_split
Definition: table.h:85
bool show_col_split
Definition: table.h:84
Config()
Definition: table.h:87
bool show_header_split
Definition: table.h:83
bool show_tb_border
Definition: table.h:81
bool show_lr_border
Definition: table.h:82
bool show_footer_split
Definition: table.h:86
Style is used to define the border style of the table.
Definition: table.h:42
Style()
Definition: table.h:43
Style(std::initializer_list< std::string > args)
Definition: table.h:44
std::vector< std::string > m_args
Definition: table.h:45
rank is a helper class for Printer to define the priority of overloaded functions.
Definition: print.h:44
#define ZEROERR_IS_CONTAINER
Definition: typetraits.h:244
#define ZEROERR_IS_STRING
Definition: typetraits.h:245
#define ZEROERR_ENABLE_IF(x)
Definition: typetraits.h:239