-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
160 lines (140 loc) · 4.73 KB
/
demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Author: Ozan Irsoy
#include <iostream>
#include <tblr.h>
int main() {
using namespace tblr;
{ // Create a table and stream cell by cell. Use endr to end a row.
Table t;
// Disabling clang-format because of this bug:
// https://bugs.llvm.org/show_bug.cgi?id=45018
// clang-format off
t << "animal" << "does what" << "when" << endr
<< "dog" << "barks" << "angry" << endr
<< "rooster" << "crows" << "in the morning" << endr
<< "computer" << "crashes" << "at night" << endr;
// clang-format on
std::cout << t << std::endl;
}
{ // Stream multiply into the same cell by using Cell()
Table t;
// clang-format off
t << "animal" << "does what" << "when" << endr;
// clang-format on
for (int i = 0; i < 3; i++) {
t << (Cell() << "dog " << i) << (Cell() << "barks " << i << " times")
<< "angry" << endr;
}
std::cout << t << std::endl;
}
{ // Specify widths and alignments (justifications)
Table t;
t.aligns({Left, Center, Right}).widths({10, 10, 0}); // 0 is automatic
// clang-format off
t << "animal" << "does what" << "when" << endr
<< "dog" << "barks" << "angry" << endr
<< "rooster" << "crows" << "in the morning" << endr
<< "computer" << "crashes" << "at night" << endr;
// clang-format on
std::cout << t << std::endl;
}
{ // Specify what to do when content does not fit in width
Table t;
t.widths({10, 10, 7}).multiline(Space);
// clang-format off
t << "animal" << "does what" << "when" << endr
<< "dog" << "barks" << "angry" << endr
<< "rooster" << "crows" << "in the morning" << endr
<< "computer" << "crashes" << "at night" << endr;
// clang-format on
std::cout << t << std::endl;
t.multiline(Naive);
std::cout << t << std::endl;
t.multiline(SingleLine);
std::cout << t << std::endl;
}
{ // Specify layouts
Table t;
t.layout(markdown())
.aligns({Left, Center, Right})
.widths({10, 10, 0}); // 0 is automatic
// clang-format off
t << "animal" << "does what" << "when" << endr
<< "dog" << "barks" << "angry" << endr
<< "rooster" << "crows" << "in the morning" << endr
<< "computer" << "crashes" << "at night" << endr;
// clang-format on
std::cout << t << std::endl;
t.layout(latex());
std::cout << t << std::endl;
t.layout(ascii_box());
std::cout << t << std::endl;
t.layout(unicode_box_light());
std::cout << t << std::endl;
}
{ // Customize layouts
auto layout = simple_border(/*left */ "|| ",
/*mid */ " | ",
/*right */ " ||",
/*top */ "^",
/*header mid*/ "-.",
/*mid */ "-",
/*bottom */ "vV");
Table t;
t.layout(layout)
.aligns({Left, Center, Right})
.widths({10, 10, 0}); // 0 is automatic
// clang-format off
t << "animal" << "does what" << "when" << endr
<< "dog" << "barks" << "angry" << endr
<< "rooster" << "crows" << "in the morning" << endr
<< "computer" << "crashes" << "at night" << endr;
// clang-format on
std::cout << t << std::endl;
}
{ // UTF8
Table t;
t.layout(markdown());
// clang-format off
t << "Op" << "Time" << endr
<< "f(x)" << "3ms" << endr
<< "∂f(x)/∂x" << "4μs" << endr;
// clang-format on
std::cout << t << std::endl;
}
{ // Nesting of tables
Table t, mini;
mini.layout(markdown()).aligns({Right, Left}).widths({2, 2});
// clang-format off
mini << "T" << "B" << endr
<< "L" << "R" << endr;
// clang-format on
t.layout(extra_space());
t << mini << mini << endr << mini << mini << endr;
std::cout << t << std::endl;
}
{ // ANSI color code aware line breaks & width computation
const auto green = "\033[1;32m";
const auto red = "\033[1;31m";
const auto blue = "\033[1;34m";
const auto underline = "\033[1;4m";
const auto clear = "\033[0m";
Table t;
t.layout(unicode_box_light())
.widths({10, 10, 7})
.aligns({Left, Left, Right})
.multiline(Space);
// clang-format off
t << "animal" << "does what" << "when" << endr
<< "dog" << "barks" << "angry" << endr
<< "rooster" << "crows"
<< (Cell() << green << "in the cool breezy morning" << clear)
<< endr
<< "computer" << "crashes"
<< (Cell() << underline << red << "at warm co"
<< blue << "zy night" << clear)
<< endr;
// clang-format on
std::cout << t << std::endl;
}
return 0;
}