UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Table.hxx
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2021 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi@unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
20//
21// eof: Table.hxx
22//
23
24/*\
25
26 Based on terminal-table:
27
28 https://github.com/Bornageek/terminal-table
29
30 Copyright 2015 Andreas Wilhelm
31
32 Licensed under the Apache License, Version 2.0 (the "License");
33 you may not use this file except in compliance with the License.
34 You may obtain a copy of the License at
35
36 http://www.apache.org/licenses/LICENSE-2.0
37
38 Unless required by applicable law or agreed to in writing, software
39 distributed under the License is distributed on an "AS IS" BASIS,
40 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
41 See the License for the specific language governing permissions and
42 limitations under the License.
43
44\*/
45
46namespace Utils {
47
48 using std::string;
49 using std::string_view;
50 using std::vector;
51
52 namespace Table {
53
54 class Table;
55
56 using integer = int;
57
64 using Alignment = enum class Table_align : integer { LEFT, RIGHT, CENTER };
65
72 class Style {
73 private:
74
75 char m_border_top = '-';
76 char m_border_top_mid = '+';
77 char m_border_top_left = '+';
78 char m_border_top_right = '+';
79
80 char m_border_bottom = '-';
81 char m_border_bottom_mid = '+';
82 char m_border_bottom_left = '+';
83 char m_border_bottom_right = '+';
84
85 char m_border_left = '|';
86 char m_border_left_mid = '+';
87
88 char m_border_mid = '-';
89 char m_border_mid_mid = '+';
90
91 char m_border_right = '|';
92 char m_border_right_mid = '+';
93
94 char m_border_middle = '|';
95
96 integer m_padding_left = 1;
97 integer m_padding_right = 1;
98
99 Alignment m_Align = Alignment::LEFT;
100
101 integer m_Width = 0;
102
103 public:
104
108 Style() = default;
109
110 char border_top() const { return m_border_top; }
111 void border_top( char borderStyle ) { m_border_top = borderStyle; }
112
113 char border_top_mid() const { return m_border_top_mid; }
114 void border_top_mid( char borderStyle ) { m_border_top_mid = borderStyle; }
115
116 char border_top_left() const { return m_border_top_left; }
117 void border_top_left( char borderStyle ) { m_border_top_left = borderStyle; }
118
119 char border_top_right() const { return m_border_top_right; }
120 void border_top_right( char borderStyle ) { m_border_top_right = borderStyle; }
121
122 char border_bottom() const { return m_border_bottom; }
123 void border_bottom( char borderStyle ) { m_border_bottom = borderStyle; }
124
125 char border_bottom_mid() const { return m_border_bottom_mid; }
126 void border_bottom_mid( char borderStyle ) { m_border_bottom_mid = borderStyle; }
127
128 char border_bottom_left() const { return m_border_bottom_left; }
129 void border_bottom_left( char borderStyle ) { m_border_bottom_left = borderStyle; }
130
131 char border_bottom_right() const { return m_border_bottom_right; }
132 void border_bottom_right( char borderStyle) { m_border_bottom_right = borderStyle; }
133
134 char border_left() const { return m_border_left; }
135 void border_left( char borderStyle ) { m_border_left = borderStyle; }
136
137 char border_left_mid() const { return m_border_left_mid; }
138 void border_left_mid( char borderStyle ) { m_border_left_mid = borderStyle; }
139
140 char border_mid() const { return m_border_mid; }
141 void border_mid( char borderStyle ) { m_border_mid = borderStyle; }
142
143 char border_mid_mid() const { return m_border_mid_mid; }
144 void border_mid_mid( char borderStyle ) { m_border_mid_mid = borderStyle; }
145
146 char border_right() const { return m_border_right; }
147 void border_right( char borderStyle ) { m_border_right = borderStyle; }
148
149 char border_right_mid() const { return m_border_right_mid; }
150 void border_right_mid( char borderStyle ) { m_border_right_mid = borderStyle; }
151
152 char border_middle() const { return m_border_middle; }
153 void border_middle( char borderStyle ) { m_border_middle = borderStyle; }
154
155 integer padding_left() const { return m_padding_left; }
156 void padding_left( integer padding ) { m_padding_left = padding; }
157
158 integer padding_right() const { return m_padding_right; }
159 void padding_right( integer padding ) { m_padding_right = padding; }
160
161 Alignment alignment() const { return m_Align; }
162 void alignment( Alignment align ) { m_Align = align; }
163
164 integer width() const { return m_Width; }
165 void width( integer width ) { m_Width = width; }
166 };
167
174 class Cell {
175 private:
176 Table * m_Table = nullptr;
177 string m_Value = "";
178 Alignment m_Align = Alignment::LEFT;
179 integer m_col_span = 1;
180 integer m_Width = 0;
181
182 public:
183
187 Cell() = default;
188
196 explicit
198 Table* table,
199 string_view val = "",
200 integer col_span = 1
201 );
202
203 string_view value() const { return m_Value; }
204 void value( string_view val ) { m_Value = val; }
205
206 Alignment alignment() const { return m_Align; }
207 void alignment( Alignment const & align ) { m_Align = align; }
208
209 integer col_span() const { return m_col_span; }
210 void col_span( integer col_span ) { m_col_span = col_span; }
211
212 integer width( integer col ) const;
214
216
217 string line( integer idx ) const;
218 static void trim_line( std::string & line ) ;
219
220 string render( integer line, integer col ) const;
221 };
222
229 class Row {
230 protected:
231 using vecCell = vector<Cell>;
232 using vecstr = vector<string>;
233
234 Table * m_Table = nullptr;
236
237 public:
238
242 Row() = default;
243
250 explicit
252 Table * table,
253 vecstr const & cells = vecstr()
254 );
255
256 Table const * table() const { return m_Table; }
257
258 //vecCell & cells() { return m_Cells; }
259 void cells( vecstr const & cells );
260
261 integer num_cells() const { return integer(m_Cells.size()); }
263 void cell_col_span( integer idx, integer span );
264
265 void cell( string_view value );
266 //Cell& cell( integer idx ) { return m_Cells[idx]; }
267
268 Cell const & operator [] ( integer idx ) const { return m_Cells[idx]; }
269 Cell & operator [] ( integer idx ) { return m_Cells[idx]; }
270
272
273 string render() const;
274 };
275
283 class Table {
284 public:
285 using vecRow = std::vector<Row>;
286 using vecCell = std::vector<Cell>;
287 using vecstr = std::vector<string>;
288 using vecvecstr = std::vector<vecstr>;
289 using integer = int;
290
291 private:
292 Style m_Style;
293 string m_Title;
294 Row m_Headings;
295 vecRow m_Rows;
296
297 public:
301 Table() = default;
302
309 explicit
311 Style const & style,
312 vecvecstr const & rows = vecvecstr()
313 )
314 : m_Style(style) {
315 this->rows(rows);
316 }
317
318 void
320 Style const & style,
321 vecvecstr const & rows = vecvecstr()
322 ) {
323 m_Style = style;
324 this->rows(rows);
325 }
326
328 void add_row( vecstr const & row );
329
332
336
337 Style const & style() const { return m_Style; }
338
339 void style( Style const & style ) { m_Style = style; }
340
341 string_view title() const { return m_Title; }
342
343 void title( string_view title ) { m_Title = title; }
344
345 Row const & headings() const { return m_Headings; }
346
347 void headings( vecstr const & headings );
348
350 Row const & row( integer n ) const;
351
352 Row & operator [] ( integer n ) { return this->row(n); }
353 Row const & operator [] ( integer n ) const { return this->row(n); }
354
355 Cell & operator () ( integer i, integer j ) { return (*this)[i][j]; }
356 Cell const & operator () ( integer i, integer j ) const { return (*this)[i][j]; }
357
358 vecRow const & rows() const { return m_Rows; }
359 void rows( vecvecstr const & rows );
360
361 std::string
363 char left,
364 char mid,
365 char right,
366 char sep
367 ) const;
368
369 std::string render() const;
370 };
371 }
372}
373
377inline
379operator << ( Utils::ostream_type& stream, Utils::Table::Row const & row ) {
380 return stream << row.render();
381}
382
386inline
388operator << ( Utils::ostream_type& stream, Utils::Table::Table const & table ) {
389 return stream << table.render();
390}
391
392//
393// eof: Table.hxx
394//
Utils::ostream_type & operator<<(Utils::ostream_type &stream, Utils::Table::Row const &row)
Stream insertion operator for rendering a table row to an output stream.
Definition Table.hxx:379
Represents a cell in a table with alignment, content, and optional column span.
Definition Table.hxx:174
string line(integer idx) const
Alignment alignment() const
Definition Table.hxx:206
string render(integer line, integer col) const
integer maximum_line_width() const
Cell()=default
Default constructor for an empty cell.
string_view value() const
Definition Table.hxx:203
integer width(integer col) const
void col_span(integer col_span)
Definition Table.hxx:210
void alignment(Alignment const &align)
Definition Table.hxx:207
static void trim_line(std::string &line)
Cell(Table *table, string_view val="", integer col_span=1)
Constructs a cell with a value and optional column span.
integer height() const
void value(string_view val)
Definition Table.hxx:204
integer col_span() const
Definition Table.hxx:209
Represents a row in a table consisting of multiple cells.
Definition Table.hxx:229
Row()=default
Default constructor for an empty row.
void cells(vecstr const &cells)
void cell_col_span(integer idx, integer span)
Row(Table *table, vecstr const &cells=vecstr())
Constructs a row with a set of initial cell values.
string render() const
Table const * table() const
Definition Table.hxx:256
vector< Cell > vecCell
Definition Table.hxx:231
Cell const & operator[](integer idx) const
Definition Table.hxx:268
integer num_cells() const
Definition Table.hxx:261
Table * m_Table
Definition Table.hxx:234
vecCell m_Cells
Definition Table.hxx:235
integer cell_width(integer idx) const
integer height() const
void cell(string_view value)
vector< string > vecstr
Definition Table.hxx:232
Defines the style and structure of table borders, padding, and alignment.
Definition Table.hxx:72
char border_top() const
Definition Table.hxx:110
void border_bottom_right(char borderStyle)
Definition Table.hxx:132
char border_left() const
Definition Table.hxx:134
void border_top_left(char borderStyle)
Definition Table.hxx:117
char border_left_mid() const
Definition Table.hxx:137
void border_mid(char borderStyle)
Definition Table.hxx:141
Style()=default
Default constructor initializing the table style with default borders.
char border_right() const
Definition Table.hxx:146
char border_bottom() const
Definition Table.hxx:122
integer padding_right() const
Definition Table.hxx:158
char border_mid_mid() const
Definition Table.hxx:143
char border_middle() const
Definition Table.hxx:152
char border_top_left() const
Definition Table.hxx:116
void border_right_mid(char borderStyle)
Definition Table.hxx:150
void border_left_mid(char borderStyle)
Definition Table.hxx:138
Alignment alignment() const
Definition Table.hxx:161
void border_mid_mid(char borderStyle)
Definition Table.hxx:144
char border_top_mid() const
Definition Table.hxx:113
void border_middle(char borderStyle)
Definition Table.hxx:153
void alignment(Alignment align)
Definition Table.hxx:162
char border_mid() const
Definition Table.hxx:140
void border_top_right(char borderStyle)
Definition Table.hxx:120
void border_bottom_mid(char borderStyle)
Definition Table.hxx:126
integer width() const
Definition Table.hxx:164
void padding_left(integer padding)
Definition Table.hxx:156
char border_bottom_mid() const
Definition Table.hxx:125
void border_right(char borderStyle)
Definition Table.hxx:147
void padding_right(integer padding)
Definition Table.hxx:159
integer padding_left() const
Definition Table.hxx:155
void width(integer width)
Definition Table.hxx:165
void border_bottom_left(char borderStyle)
Definition Table.hxx:129
char border_bottom_left() const
Definition Table.hxx:128
void border_bottom(char borderStyle)
Definition Table.hxx:123
void border_top_mid(char borderStyle)
Definition Table.hxx:114
char border_right_mid() const
Definition Table.hxx:149
char border_top_right() const
Definition Table.hxx:119
void border_top(char borderStyle)
Definition Table.hxx:111
void border_left(char borderStyle)
Definition Table.hxx:135
char border_bottom_right() const
Definition Table.hxx:131
The main class for creating and managing a table.
Definition Table.hxx:283
void rows(vecvecstr const &rows)
Row const & row(integer n) const
std::vector< vecstr > vecvecstr
Definition Table.hxx:288
integer cell_padding() const
vecRow const & rows() const
Definition Table.hxx:358
void add_row(vecstr const &row)
int integer
Definition Table.hxx:289
std::string render_separator(char left, char mid, char right, char sep) const
Cell & operator()(integer i, integer j)
Definition Table.hxx:355
string_view title() const
Definition Table.hxx:341
integer column_width(integer n) const
void style(Style const &style)
Definition Table.hxx:339
Row & operator[](integer n)
Definition Table.hxx:352
void headings(vecstr const &headings)
Row const & headings() const
Definition Table.hxx:345
Table(Style const &style, vecvecstr const &rows=vecvecstr())
Constructs a table with a given style and initial rows.
Definition Table.hxx:310
void align_column(integer n, Alignment align)
Table()=default
Default constructor for an empty table.
std::vector< Row > vecRow
Definition Table.hxx:285
integer num_columns() const
Row & row(integer n)
vecCell column(integer n) const
std::string render() const
void setup(Style const &style, vecvecstr const &rows=vecvecstr())
Definition Table.hxx:319
void title(string_view title)
Definition Table.hxx:343
integer cell_spacing() const
std::vector< Cell > vecCell
Definition Table.hxx:286
Style const & style() const
Definition Table.hxx:337
std::vector< string > vecstr
Definition Table.hxx:287
int integer
Definition Table.hxx:56
enum class Table_align :integer { LEFT, RIGHT, CENTER } Alignment
Enum class defining alignment types for table cells.
Definition Table.hxx:64
Definition SystemUtils.cc:39
std::basic_ostream< char > ostream_type
Type for output stream.
Definition Console.hxx:28