-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextTable.h
155 lines (131 loc) · 3.46 KB
/
TextTable.h
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
#pragma once
#include <iostream>
#include <map>
#include <iomanip>
#include <vector>
#include <string>
class TextTable {
public:
enum class Alignment { LEFT, RIGHT };
typedef std::vector< std::string > Row;
TextTable(char horizontal = '-', char vertical = '|', char corner = '+') :
_horizontal(horizontal),
_vertical(vertical),
_corner(corner)
{}
void setAlignment(unsigned i, Alignment alignment)
{
_alignment[i] = alignment;
}
Alignment alignment(unsigned i) const
{
return _alignment[i];
}
char vertical() const
{
return _vertical;
}
char horizontal() const
{
return _horizontal;
}
void add(std::string const& content)
{
_current.push_back(content);
}
void endOfRow()
{
_rows.push_back(_current);
_current.assign(0, "");
}
template <typename Iterator>
void addRow(Iterator begin, Iterator end)
{
for (auto i = begin; i != end; ++i) {
add(*i);
}
endOfRow();
}
template <typename Container>
void addRow(Container const& container)
{
addRow(container.begin(), container.end());
}
std::vector< Row > const& rows() const
{
return _rows;
}
void setup() const
{
determineWidths();
setupAlignment();
}
std::string ruler() const
{
std::string result;
result += _corner;
for (auto width = _width.begin(); width != _width.end(); ++width) {
result += repeat(*width, _horizontal);
result += _corner;
}
return result;
}
int width(unsigned i) const
{
return _width[i];
}
private:
char _horizontal;
char _vertical;
char _corner;
Row _current;
std::vector< Row > _rows;
std::vector< unsigned > mutable _width;
std::map< unsigned, Alignment > mutable _alignment;
static std::string repeat(unsigned times, char c)
{
std::string result;
for (; times > 0; --times)
result += c;
return result;
}
unsigned columns() const
{
return _rows[0].size();
}
void determineWidths() const
{
_width.assign(columns(), 0);
for (auto rowIterator = _rows.begin(); rowIterator != _rows.end(); ++rowIterator) {
Row const& row = *rowIterator;
for (unsigned i = 0; i < row.size(); ++i) {
_width[i] = _width[i] > row[i].size() ? _width[i] : row[i].size();
}
}
}
void setupAlignment() const
{
for (unsigned i = 0; i < columns(); ++i) {
if (_alignment.find(i) == _alignment.end()) {
_alignment[i] = Alignment::LEFT;
}
}
}
};
inline std::ostream& operator<<(std::ostream& stream, TextTable const& table)
{
table.setup();
stream << table.ruler() << "\n";
for (auto rowIterator = table.rows().begin(); rowIterator != table.rows().end(); ++rowIterator) {
TextTable::Row const& row = *rowIterator;
stream << table.vertical();
for (unsigned i = 0; i < row.size(); ++i) {
auto alignment = table.alignment(i) == TextTable::Alignment::LEFT ? std::left : std::right;
stream << std::setw(table.width(i)) << alignment << row[i];
stream << table.vertical();
}
stream << "\n";
stream << table.ruler() << "\n";
}
return stream;
}