forked from Tracktion/choc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
choc_TextTable.h
213 lines (168 loc) · 6.59 KB
/
choc_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// ██████ ██ ██ ██████ ██████
// ██ ██ ██ ██ ██ ██ ** Classy Header-Only Classes **
// ██ ███████ ██ ██ ██
// ██ ██ ██ ██ ██ ██ https://github.com/Tracktion/choc
// ██████ ██ ██ ██████ ██████
//
// CHOC is (C)2022 Tracktion Corporation, and is offered under the terms of the ISC license:
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
// without fee is hereby granted, provided that the above copyright notice and this permission
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifndef CHOC_TEXT_TABLE_HEADER_INCLUDED
#define CHOC_TEXT_TABLE_HEADER_INCLUDED
#include "choc_StringUtilities.h"
namespace choc::text
{
/**
A class to build and format basic text tables with columns that are padded to
make them vertically-aligned.
Just create a TextTable object, then
- add items to the current row with the << operator
- start a new row with newRow()
- when done, use the toString and getRows() methods to get the result.
Rows can have a different number of columns, and the table's size is based on the
maximum number of columns.
*/
struct TextTable
{
TextTable() = default;
~TextTable() = default;
/// Appends a column to the current row.
TextTable& operator<< (std::string_view text);
/// Clears and resets the table
void clear();
/// Ends the current row, so that subsequent new columns will be added to a new row
void newRow();
/// Returns the current number of rows
size_t getNumRows() const;
/// Finds the number of columns (by looking for the row with the most columns)
size_t getNumColumns() const;
/// Returns a vector of strings for each each row, which will have been
/// padded and formatted to align vertically. Each row will have the prefix
/// and suffix strings attached, and the columnSeparator string added between
/// each column.
std::vector<std::string> getRows (std::string_view rowPrefix,
std::string_view columnSeparator,
std::string_view rowSuffix) const;
/// Returns a string containing all the rows in the table.
/// See getRows() for details about the format strings. This method simply
/// concatenates all the strings for each row.
std::string toString (std::string_view rowPrefix,
std::string_view columnSeparator,
std::string_view rowSuffix) const;
private:
//==============================================================================
struct Row
{
std::vector<std::string> columns;
void addColumn (std::string_view text);
std::string toString (std::string_view columnSeparator, const std::vector<size_t> widths) const;
};
std::vector<Row> rows;
bool startNewRow = true;
std::vector<size_t> getColumnWidths() const;
};
//==============================================================================
// _ _ _ _
// __| | ___ | |_ __ _ (_)| | ___
// / _` | / _ \| __| / _` || || |/ __|
// | (_| || __/| |_ | (_| || || |\__ \ _ _ _
// \__,_| \___| \__| \__,_||_||_||___/(_)(_)(_)
//
// Code beyond this point is implementation detail...
//
//==============================================================================
inline TextTable& TextTable::operator<< (std::string_view text)
{
if (startNewRow)
{
startNewRow = false;
rows.push_back ({});
}
rows.back().addColumn (text);
return *this;
}
inline void TextTable::newRow()
{
if (startNewRow)
rows.push_back ({});
else
startNewRow = true;
}
inline void TextTable::clear()
{
rows.clear();
startNewRow = true;
}
inline size_t TextTable::getNumRows() const
{
return rows.size();
}
inline size_t TextTable::getNumColumns() const
{
size_t maxColummns = 0;
for (auto& row : rows)
maxColummns = std::max (maxColummns, row.columns.size());
return maxColummns;
}
inline std::vector<std::string> TextTable::getRows (std::string_view rowPrefix,
std::string_view columnSeparator,
std::string_view rowSuffix) const
{
std::vector<std::string> result;
result.reserve (rows.size());
auto widths = getColumnWidths();
for (auto& row : rows)
result.push_back (std::string (rowPrefix)
+ row.toString (columnSeparator, widths)
+ std::string (rowSuffix));
return result;
}
inline std::string TextTable::toString (std::string_view rowPrefix,
std::string_view columnSeparator,
std::string_view rowSuffix) const
{
std::string result;
for (auto& row : getRows (rowPrefix, columnSeparator, rowSuffix))
result += row;
return result;
}
inline std::vector<size_t> TextTable::getColumnWidths() const
{
std::vector<size_t> widths;
widths.resize (getNumColumns());
for (auto& row : rows)
for (size_t i = 0; i < row.columns.size(); ++i)
widths[i] = std::max (widths[i], row.columns[i].length());
return widths;
}
inline void TextTable::Row::addColumn (std::string_view text)
{
columns.emplace_back (text);
}
inline std::string TextTable::Row::toString (std::string_view columnSeparator, const std::vector<size_t> widths) const
{
std::string result;
size_t index = 0;
for (auto& width : widths)
{
if (index != 0)
result += columnSeparator;
std::string padded;
if (index < columns.size())
padded = columns[index];
padded.resize (width, ' ');
result += padded;
++index;
}
return result;
}
} // namespace choc::text
#endif