ZeroErr
载入中...
搜索中...
未找到
table.h
浏览该文件的文档.
1#pragma once
2
3#include <zeroerr/dbg.h>
5#include <zeroerr/print.h>
6
7#include <ostream>
8#include "zeroerr/string.h"
9#include <vector>
10
11namespace zeroerr {
12
16struct Card {
17 Card() : title(), width(0), height(0) {}
19 Card(unsigned width, unsigned height) : title(), width(width), height(height) {}
23 unsigned width, height;
24
25 void show(std::ostream& os, zeroerr::String str);
26};
27
31class Table : public Card {
32public:
33 Table() : Card() {}
35 Table(unsigned width, unsigned height) : Card(width, height) {}
37 ~Table() {}
38
42 struct Style {
43 Style() {}
44 Style(std::initializer_list<zeroerr::String> args) : m_args(args) {}
45 std::vector<zeroerr::String> m_args;
46
47 operator bool() const { return !m_args.empty(); }
48 };
49
50 static void registerStyle(zeroerr::String name, Style style);
51
75 static Style getStyle(zeroerr::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),
91 show_col_split(true),
92 show_row_split(true),
93 show_footer_split(true) {}
94 };
95
101 zeroerr::String str(Config config = Config(), Style style = Table::getStyle("square_double_head"));
102
103 void set_header(std::vector<zeroerr::String> _header) { header = _header; }
104 void add_row(std::initializer_list<zeroerr::String> _row) { cells.push_back(_row); }
105
106 template <typename T, typename... Args>
107 void push_back(std::vector<zeroerr::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<zeroerr::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<zeroerr::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
136protected:
138 _push_back(rank<0>, std::vector<zeroerr::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<zeroerr::String>& row, T t) {
149 row.push_back(std::forward<zeroerr::String>(t));
150 }
151
153 _push_back(rank<1>, std::vector<zeroerr::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<zeroerr::String> header, footer;
166 std::vector<std::vector<zeroerr::String>> cells;
167};
168
169
170} // namespace zeroerr
Definition string.h:41
Table is used to generate a table with configurable style.
Definition table.h:31
std::vector< zeroerr::String > footer
Definition table.h:165
static Style getStyle(zeroerr::String name)
getStyle can load predefined style from the StyleManager.
Definition table.cpp:247
std::vector< std::vector< zeroerr::String > > cells
Definition table.h:166
void add_rows(T &&t, Args &&... args)
Definition table.h:126
void push_back(std::vector< zeroerr::String > &row, T &&t)
Definition table.h:113
void add_row(std::initializer_list< zeroerr::String > _row)
Definition table.h:104
void set_header(std::vector< zeroerr::String > _header)
Definition table.h:103
std::vector< zeroerr::String > header
Definition table.h:165
void add_rows(T &&t)
Definition table.h:132
Table(unsigned width, unsigned height)
Definition table.h:35
Table(zeroerr::String title, unsigned width, unsigned height)
Definition table.h:36
Table()
Definition table.h:33
~Table()
Definition table.h:37
void push_back(std::vector< zeroerr::String > &row, T &&t, Args &&... args)
Definition table.h:107
Table(zeroerr::String title)
Definition table.h:34
static void registerStyle(zeroerr::String name, Style style)
Definition table.cpp:244
std::vector< unsigned > col_width
Definition table.h:164
_push_back(rank< 0 >, std::vector< zeroerr::String > &row, T &&t)
Definition table.h:138
zeroerr::String str(Config config=Config(), Style style=Table::getStyle("square_double_head"))
str is used to generate the table string.
Definition table.cpp:276
void add_row(Args &&... args)
Definition table.h:119
STL namespace
Definition assert_text.cpp:5
Card defines a display range in the console.
Definition table.h:16
zeroerr::String title
Definition table.h:22
Card()
Definition table.h:17
unsigned height
Definition table.h:23
Card(zeroerr::String title, unsigned width, unsigned height)
Definition table.h:20
Card(zeroerr::String title)
Definition table.h:18
unsigned width
Definition table.h:23
Card(unsigned width, unsigned height)
Definition table.h:19
void show(std::ostream &os, zeroerr::String str)
A functor class Printer for printing a value of any type.
Definition print.h:74
bool isQuoted
Definition print.h:118
bool isCompact
Definition print.h:117
const char * line_break
Definition print.h:120
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< zeroerr::String > args)
Definition table.h:44
std::vector< zeroerr::String > m_args
Definition table.h:45
rank is a helper class for Printer to define the priority of overloaded functions.
Definition typetraits.h:43
#define ZEROERR_IS_CONTAINER
Definition typetraits.h:340
#define ZEROERR_IS_STRING
Definition typetraits.h:341
#define ZEROERR_ENABLE_IF(x)
Definition typetraits.h:334