1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // http://www.opensource.org/licenses/mit-license.php
25 ////////////////////////////////////////////////////////////////////////////////
34 ////////////////////////////////////////////////////////////////////////////////
35 void Table::add (const std::string& col, bool alignLeft, bool wrap)
37 _columns.push_back (col);
38 _align.push_back (alignLeft);
39 _wrap.push_back (wrap);
42 ////////////////////////////////////////////////////////////////////////////////
45 _data.push_back (std::vector <std::string> (_columns.size (), ""));
46 _color.push_back (std::vector <Color> (_columns.size (), Color::nocolor));
47 _oddness.push_back (_data.size () % 2 ? true : false);
48 return _data.size () - 1;
51 ////////////////////////////////////////////////////////////////////////////////
52 int Table::addRowOdd ()
54 _data.push_back (std::vector <std::string> (_columns.size (), ""));
55 _color.push_back (std::vector <Color> (_columns.size (), Color::nocolor));
56 _oddness.push_back (true);
57 return _data.size () - 1;
60 ////////////////////////////////////////////////////////////////////////////////
61 int Table::addRowEven ()
63 _data.push_back (std::vector <std::string> (_columns.size (), ""));
64 _color.push_back (std::vector <Color> (_columns.size (), Color::nocolor));
65 _oddness.push_back (false);
66 return _data.size () - 1;
69 ////////////////////////////////////////////////////////////////////////////////
70 void Table::set (int row, int col, const std::string& value, const Color color)
72 _data[row][col] = value;
74 if (color.nontrivial ())
75 _color[row][col] = color;
78 ////////////////////////////////////////////////////////////////////////////////
79 void Table::set (int row, int col, int value, const Color color)
81 std::string string_value = format (value);
82 _data[row][col] = string_value;
84 if (color.nontrivial ())
85 _color[row][col] = color;
88 ////////////////////////////////////////////////////////////////////////////////
89 void Table::set (int row, int col, const Color color)
91 if (color.nontrivial ())
92 _color[row][col] = color;
95 ////////////////////////////////////////////////////////////////////////////////
96 std::string Table::render ()
98 // Piped output disables color, unless overridden.
100 ! isatty (STDOUT_FILENO))
102 _header = Color ("");
105 _intra_odd = Color ("");
106 _intra_even = Color ("");
107 _extra_odd = Color ("");
108 _extra_even = Color ("");
110 for (auto& row : _color)
111 for (auto& col : row)
114 _underline_headers = true;
117 // Determine minimal, ideal column widths.
118 std::vector <int> minimal;
119 std::vector <int> ideal;
120 for (unsigned int col = 0; col < _columns.size (); ++col)
122 // Headers factor in to width calculations.
123 unsigned int global_min = utf8_text_width (_columns[col]);
124 unsigned int global_ideal = global_min;
126 for (unsigned int row = 0; row < _data.size (); ++row)
128 // Determine minimum and ideal width for this column.
129 unsigned int min = 0;
130 unsigned int ideal = 0;
131 measureCell (_data[row][col], min, ideal);
133 if (min > global_min) global_min = min;
134 if (ideal > global_ideal) global_ideal = ideal;
137 minimal.push_back (global_min);
138 ideal.push_back (global_ideal);
141 // Sum the minimal widths.
143 for (const auto& c : minimal)
146 // Sum the ideal widths.
148 for (const auto& c : ideal)
151 // Calculate final column widths.
154 - (2 * _extra_padding)
155 - ((_columns.size () - 1) * _intra_padding);
157 std::vector <int> widths;
158 if (sum_ideal <= overage)
160 else if (sum_minimal > overage || overage < 0)
162 else if (overage > 0)
165 overage -= sum_minimal;
167 // Spread 'overage' among columns where width[i] < ideal[i]
170 for (unsigned int i = 0; i < _columns.size () && overage; ++i)
172 if (widths[i] < ideal[i])
181 // Compose column headers.
182 unsigned int max_lines = 0;
183 std::vector <std::vector <std::string>> headers;
184 for (unsigned int c = 0; c < _columns.size (); ++c)
186 headers.push_back ({});
187 renderCell (headers[c], _columns[c], widths[c], _align[c], _wrap[c], _header);
189 if (headers[c].size () > max_lines)
190 max_lines = headers[c].size ();
197 // Render column headers.
198 std::string left_margin = std::string (_left_margin, ' ');
199 std::string extra = std::string (_extra_padding, ' ');
200 std::string intra = std::string (_intra_padding, ' ');
202 std::string extra_odd = _extra_odd.colorize (extra);
203 std::string extra_even = _extra_even.colorize (extra);
204 std::string intra_odd = _intra_odd.colorize (intra);
205 std::string intra_even = _intra_even.colorize (intra);
207 for (unsigned int i = 0; i < max_lines; ++i)
209 out += left_margin + extra;
211 for (unsigned int c = 0; c < _columns.size (); ++c)
216 if (headers[c].size () < max_lines - i)
217 out += _header.colorize (std::string (widths[c], ' '));
219 out += headers[c][i];
225 out.erase (out.find_last_not_of (" ") + 1);
228 // Stop if the line limit is exceeded.
229 if (++_lines >= _truncate_lines && _truncate_lines != 0)
233 // Underline headers with ------ if necessary.
234 if (_underline_headers)
236 out += left_margin + extra;
237 for (unsigned int c = 0; c < _columns.size (); ++c)
242 out += _header.colorize (std::string (widths[c], '-'));
248 // Compose, render columns, in sequence.
250 std::vector <std::vector <std::string>> cells;
251 for (unsigned int row = 0; row < _data.size (); ++row)
255 // Alternate rows based on |s % 2|
256 auto oddness = _oddness[row];
257 Color row_color = oddness ? _odd : _even;
259 // TODO row_color.blend (provided color);
260 // TODO Problem: colors for columns are specified, not rows,
261 // therefore there are only cell colors, not intra colors.
264 for (unsigned int col = 0; col < _columns.size (); ++col)
266 cell_color = row_color;
267 cell_color.blend (_color[row][col]);
269 cells.push_back (std::vector <std::string> ());
270 renderCell (cells[col], _data[row][col], widths[col], _align[col], _wrap[col], cell_color);
272 if (cells[col].size () > max_lines)
273 max_lines = cells[col].size ();
276 for (auto& value : cells[col])
277 value = obfuscateText (value);
280 for (unsigned int i = 0; i < max_lines; ++i)
282 out += left_margin + (oddness ? extra_odd : extra_even);
284 for (unsigned int col = 0; col < _columns.size (); ++col)
288 if (row_color.nontrivial ())
289 out += row_color.colorize (intra);
291 out += (oddness ? intra_odd : intra_even);
294 if (i < cells[col].size ())
295 out += cells[col][i];
298 cell_color = row_color;
299 cell_color.blend (_color[row][col]);
300 out += cell_color.colorize (std::string (widths[col], ' '));
304 out += (oddness ? extra_odd : extra_even);
307 out.erase (out.find_last_not_of (" ") + 1);
310 // Stop if the line limit is exceeded.
311 if (++_lines >= _truncate_lines && _truncate_lines != 0)
317 // Stop if the row limit is exceeded.
318 if (++_rows >= _truncate_rows && _truncate_rows != 0)
325 ////////////////////////////////////////////////////////////////////////////////
326 void Table::measureCell (
327 const std::string& data,
328 unsigned int& minimum,
329 unsigned int& maximum) const
331 std::string stripped = Color::strip (data);
332 maximum = longestLine (stripped);
333 minimum = longestWord (stripped);
336 ////////////////////////////////////////////////////////////////////////////////
337 void Table::renderCell (
338 std::vector <std::string>& lines,
339 const std::string& value,
343 const Color& color) const
347 std::vector <std::string> raw;
348 wrapText (raw, value, width, false);
350 for (const auto& line : raw)
354 leftJustify (line, width)));
358 rightJustify (line, width)));
365 leftJustify (value, width)));
369 rightJustify (value, width)));
373 ////////////////////////////////////////////////////////////////////////////////