1*b0d29bc4SBrooks Davis // Copyright 2012 The Kyua Authors.
2*b0d29bc4SBrooks Davis // All rights reserved.
3*b0d29bc4SBrooks Davis //
4*b0d29bc4SBrooks Davis // Redistribution and use in source and binary forms, with or without
5*b0d29bc4SBrooks Davis // modification, are permitted provided that the following conditions are
6*b0d29bc4SBrooks Davis // met:
7*b0d29bc4SBrooks Davis //
8*b0d29bc4SBrooks Davis // * Redistributions of source code must retain the above copyright
9*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer.
10*b0d29bc4SBrooks Davis // * Redistributions in binary form must reproduce the above copyright
11*b0d29bc4SBrooks Davis // notice, this list of conditions and the following disclaimer in the
12*b0d29bc4SBrooks Davis // documentation and/or other materials provided with the distribution.
13*b0d29bc4SBrooks Davis // * Neither the name of Google Inc. nor the names of its contributors
14*b0d29bc4SBrooks Davis // may be used to endorse or promote products derived from this software
15*b0d29bc4SBrooks Davis // without specific prior written permission.
16*b0d29bc4SBrooks Davis //
17*b0d29bc4SBrooks Davis // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*b0d29bc4SBrooks Davis // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*b0d29bc4SBrooks Davis // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*b0d29bc4SBrooks Davis // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*b0d29bc4SBrooks Davis // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*b0d29bc4SBrooks Davis // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*b0d29bc4SBrooks Davis // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*b0d29bc4SBrooks Davis // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*b0d29bc4SBrooks Davis // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*b0d29bc4SBrooks Davis // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*b0d29bc4SBrooks Davis // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*b0d29bc4SBrooks Davis
29*b0d29bc4SBrooks Davis #include "utils/text/table.hpp"
30*b0d29bc4SBrooks Davis
31*b0d29bc4SBrooks Davis #include <algorithm>
32*b0d29bc4SBrooks Davis #include <iterator>
33*b0d29bc4SBrooks Davis #include <limits>
34*b0d29bc4SBrooks Davis #include <sstream>
35*b0d29bc4SBrooks Davis
36*b0d29bc4SBrooks Davis #include "utils/sanity.hpp"
37*b0d29bc4SBrooks Davis #include "utils/text/operations.ipp"
38*b0d29bc4SBrooks Davis
39*b0d29bc4SBrooks Davis namespace text = utils::text;
40*b0d29bc4SBrooks Davis
41*b0d29bc4SBrooks Davis
42*b0d29bc4SBrooks Davis namespace {
43*b0d29bc4SBrooks Davis
44*b0d29bc4SBrooks Davis
45*b0d29bc4SBrooks Davis /// Applies user overrides to the column widths of a table.
46*b0d29bc4SBrooks Davis ///
47*b0d29bc4SBrooks Davis /// \param table The table from which to calculate the column widths.
48*b0d29bc4SBrooks Davis /// \param user_widths The column widths provided by the user. This vector must
49*b0d29bc4SBrooks Davis /// have less or the same number of elements as the columns of the table.
50*b0d29bc4SBrooks Davis /// Values of width_auto are ignored; any other explicit values are copied
51*b0d29bc4SBrooks Davis /// to the output widths vector, including width_refill.
52*b0d29bc4SBrooks Davis ///
53*b0d29bc4SBrooks Davis /// \return A vector with the widths of the columns of the input table with any
54*b0d29bc4SBrooks Davis /// user overrides applied.
55*b0d29bc4SBrooks Davis static text::widths_vector
override_column_widths(const text::table & table,const text::widths_vector & user_widths)56*b0d29bc4SBrooks Davis override_column_widths(const text::table& table,
57*b0d29bc4SBrooks Davis const text::widths_vector& user_widths)
58*b0d29bc4SBrooks Davis {
59*b0d29bc4SBrooks Davis PRE(user_widths.size() <= table.ncolumns());
60*b0d29bc4SBrooks Davis text::widths_vector widths = table.column_widths();
61*b0d29bc4SBrooks Davis
62*b0d29bc4SBrooks Davis // Override the actual width of the columns based on user-specified widths.
63*b0d29bc4SBrooks Davis for (text::widths_vector::size_type i = 0; i < user_widths.size(); ++i) {
64*b0d29bc4SBrooks Davis const text::widths_vector::value_type& user_width = user_widths[i];
65*b0d29bc4SBrooks Davis if (user_width != text::table_formatter::width_auto) {
66*b0d29bc4SBrooks Davis PRE_MSG(user_width == text::table_formatter::width_refill ||
67*b0d29bc4SBrooks Davis user_width >= widths[i],
68*b0d29bc4SBrooks Davis "User-provided column widths must be larger than the "
69*b0d29bc4SBrooks Davis "column contents (except for the width_refill column)");
70*b0d29bc4SBrooks Davis widths[i] = user_width;
71*b0d29bc4SBrooks Davis }
72*b0d29bc4SBrooks Davis }
73*b0d29bc4SBrooks Davis
74*b0d29bc4SBrooks Davis return widths;
75*b0d29bc4SBrooks Davis }
76*b0d29bc4SBrooks Davis
77*b0d29bc4SBrooks Davis
78*b0d29bc4SBrooks Davis /// Locates the refill column, if any.
79*b0d29bc4SBrooks Davis ///
80*b0d29bc4SBrooks Davis /// \param widths The widths of the columns as returned by
81*b0d29bc4SBrooks Davis /// override_column_widths(). Note that one of the columns may or may not
82*b0d29bc4SBrooks Davis /// be width_refill, which is the column we are looking for.
83*b0d29bc4SBrooks Davis ///
84*b0d29bc4SBrooks Davis /// \return The index of the refill column with a width_refill width if any, or
85*b0d29bc4SBrooks Davis /// otherwise the index of the last column (which is the default refill column).
86*b0d29bc4SBrooks Davis static text::widths_vector::size_type
find_refill_column(const text::widths_vector & widths)87*b0d29bc4SBrooks Davis find_refill_column(const text::widths_vector& widths)
88*b0d29bc4SBrooks Davis {
89*b0d29bc4SBrooks Davis text::widths_vector::size_type i = 0;
90*b0d29bc4SBrooks Davis for (; i < widths.size(); ++i) {
91*b0d29bc4SBrooks Davis if (widths[i] == text::table_formatter::width_refill)
92*b0d29bc4SBrooks Davis return i;
93*b0d29bc4SBrooks Davis }
94*b0d29bc4SBrooks Davis return i - 1;
95*b0d29bc4SBrooks Davis }
96*b0d29bc4SBrooks Davis
97*b0d29bc4SBrooks Davis
98*b0d29bc4SBrooks Davis /// Pads the widths of the table to fit within a maximum width.
99*b0d29bc4SBrooks Davis ///
100*b0d29bc4SBrooks Davis /// On output, a column of the widths vector is truncated to a shorter length
101*b0d29bc4SBrooks Davis /// than its current value, if the total width of the table would exceed the
102*b0d29bc4SBrooks Davis /// maximum table width.
103*b0d29bc4SBrooks Davis ///
104*b0d29bc4SBrooks Davis /// \param [in,out] widths The widths of the columns as returned by
105*b0d29bc4SBrooks Davis /// override_column_widths(). One of these columns should have a value of
106*b0d29bc4SBrooks Davis /// width_refill; if not, a default column is refilled.
107*b0d29bc4SBrooks Davis /// \param user_max_width The target width of the table; must not be zero.
108*b0d29bc4SBrooks Davis /// \param column_padding The padding between the cells, if any. The target
109*b0d29bc4SBrooks Davis /// width should be larger than the padding times the number of columns; if
110*b0d29bc4SBrooks Davis /// that is not the case, we attempt a readjustment here.
111*b0d29bc4SBrooks Davis static void
refill_widths(text::widths_vector & widths,const text::widths_vector::value_type user_max_width,const std::size_t column_padding)112*b0d29bc4SBrooks Davis refill_widths(text::widths_vector& widths,
113*b0d29bc4SBrooks Davis const text::widths_vector::value_type user_max_width,
114*b0d29bc4SBrooks Davis const std::size_t column_padding)
115*b0d29bc4SBrooks Davis {
116*b0d29bc4SBrooks Davis PRE(user_max_width != 0);
117*b0d29bc4SBrooks Davis
118*b0d29bc4SBrooks Davis // widths.size() is a proxy for the number of columns of the table.
119*b0d29bc4SBrooks Davis const std::size_t total_padding = column_padding * (widths.size() - 1);
120*b0d29bc4SBrooks Davis const text::widths_vector::value_type max_width = std::max(
121*b0d29bc4SBrooks Davis user_max_width, total_padding) - total_padding;
122*b0d29bc4SBrooks Davis
123*b0d29bc4SBrooks Davis const text::widths_vector::size_type refill_column =
124*b0d29bc4SBrooks Davis find_refill_column(widths);
125*b0d29bc4SBrooks Davis INV(refill_column < widths.size());
126*b0d29bc4SBrooks Davis
127*b0d29bc4SBrooks Davis text::widths_vector::value_type width = 0;
128*b0d29bc4SBrooks Davis for (text::widths_vector::size_type i = 0; i < widths.size(); ++i) {
129*b0d29bc4SBrooks Davis if (i != refill_column)
130*b0d29bc4SBrooks Davis width += widths[i];
131*b0d29bc4SBrooks Davis }
132*b0d29bc4SBrooks Davis widths[refill_column] = max_width - width;
133*b0d29bc4SBrooks Davis }
134*b0d29bc4SBrooks Davis
135*b0d29bc4SBrooks Davis
136*b0d29bc4SBrooks Davis /// Pads an input text to a specified width with spaces.
137*b0d29bc4SBrooks Davis ///
138*b0d29bc4SBrooks Davis /// \param input The text to add padding to (may be empty).
139*b0d29bc4SBrooks Davis /// \param length The desired length of the output.
140*b0d29bc4SBrooks Davis /// \param is_last Whether the text being processed belongs to the last column
141*b0d29bc4SBrooks Davis /// of a row or not. Values in the last column should not be padded to
142*b0d29bc4SBrooks Davis /// prevent trailing whitespace on the screen (which affects copy/pasting
143*b0d29bc4SBrooks Davis /// for example).
144*b0d29bc4SBrooks Davis ///
145*b0d29bc4SBrooks Davis /// \return The padded cell. If the input string is longer than the desired
146*b0d29bc4SBrooks Davis /// length, the input string is returned verbatim. The padded table won't be
147*b0d29bc4SBrooks Davis /// correct, but we don't expect this to be a common case to worry about.
148*b0d29bc4SBrooks Davis static std::string
pad_cell(const std::string & input,const std::size_t length,const bool is_last)149*b0d29bc4SBrooks Davis pad_cell(const std::string& input, const std::size_t length, const bool is_last)
150*b0d29bc4SBrooks Davis {
151*b0d29bc4SBrooks Davis if (is_last)
152*b0d29bc4SBrooks Davis return input;
153*b0d29bc4SBrooks Davis else {
154*b0d29bc4SBrooks Davis if (input.length() < length)
155*b0d29bc4SBrooks Davis return input + std::string(length - input.length(), ' ');
156*b0d29bc4SBrooks Davis else
157*b0d29bc4SBrooks Davis return input;
158*b0d29bc4SBrooks Davis }
159*b0d29bc4SBrooks Davis }
160*b0d29bc4SBrooks Davis
161*b0d29bc4SBrooks Davis
162*b0d29bc4SBrooks Davis /// Refills a cell and adds it to the output lines.
163*b0d29bc4SBrooks Davis ///
164*b0d29bc4SBrooks Davis /// \param row The row containing the cell to be refilled.
165*b0d29bc4SBrooks Davis /// \param widths The widths of the row.
166*b0d29bc4SBrooks Davis /// \param column The column being refilled.
167*b0d29bc4SBrooks Davis /// \param [in,out] textual_rows The output lines as processed so far. This is
168*b0d29bc4SBrooks Davis /// updated to accomodate for the contents of the refilled cell, extending
169*b0d29bc4SBrooks Davis /// the rows as necessary.
170*b0d29bc4SBrooks Davis static void
refill_cell(const text::table_row & row,const text::widths_vector & widths,const text::table_row::size_type column,std::vector<text::table_row> & textual_rows)171*b0d29bc4SBrooks Davis refill_cell(const text::table_row& row, const text::widths_vector& widths,
172*b0d29bc4SBrooks Davis const text::table_row::size_type column,
173*b0d29bc4SBrooks Davis std::vector< text::table_row >& textual_rows)
174*b0d29bc4SBrooks Davis {
175*b0d29bc4SBrooks Davis const std::vector< std::string > rows = text::refill(row[column],
176*b0d29bc4SBrooks Davis widths[column]);
177*b0d29bc4SBrooks Davis
178*b0d29bc4SBrooks Davis if (textual_rows.size() < rows.size())
179*b0d29bc4SBrooks Davis textual_rows.resize(rows.size(), text::table_row(row.size()));
180*b0d29bc4SBrooks Davis
181*b0d29bc4SBrooks Davis for (std::vector< std::string >::size_type i = 0; i < rows.size(); ++i) {
182*b0d29bc4SBrooks Davis for (text::table_row::size_type j = 0; j < row.size(); ++j) {
183*b0d29bc4SBrooks Davis const bool is_last = j == row.size() - 1;
184*b0d29bc4SBrooks Davis if (j == column)
185*b0d29bc4SBrooks Davis textual_rows[i][j] = pad_cell(rows[i], widths[j], is_last);
186*b0d29bc4SBrooks Davis else {
187*b0d29bc4SBrooks Davis if (textual_rows[i][j].empty())
188*b0d29bc4SBrooks Davis textual_rows[i][j] = pad_cell("", widths[j], is_last);
189*b0d29bc4SBrooks Davis }
190*b0d29bc4SBrooks Davis }
191*b0d29bc4SBrooks Davis }
192*b0d29bc4SBrooks Davis }
193*b0d29bc4SBrooks Davis
194*b0d29bc4SBrooks Davis
195*b0d29bc4SBrooks Davis /// Formats a single table row.
196*b0d29bc4SBrooks Davis ///
197*b0d29bc4SBrooks Davis /// \param row The row to format.
198*b0d29bc4SBrooks Davis /// \param widths The widths of the columns to apply during formatting. Cells
199*b0d29bc4SBrooks Davis /// wider than the specified width are refilled to attempt to fit in the
200*b0d29bc4SBrooks Davis /// cell. Cells narrower than the width are right-padded with spaces.
201*b0d29bc4SBrooks Davis /// \param separator The column separator to use.
202*b0d29bc4SBrooks Davis ///
203*b0d29bc4SBrooks Davis /// \return The textual lines that contain the formatted row.
204*b0d29bc4SBrooks Davis static std::vector< std::string >
format_row(const text::table_row & row,const text::widths_vector & widths,const std::string & separator)205*b0d29bc4SBrooks Davis format_row(const text::table_row& row, const text::widths_vector& widths,
206*b0d29bc4SBrooks Davis const std::string& separator)
207*b0d29bc4SBrooks Davis {
208*b0d29bc4SBrooks Davis PRE(row.size() == widths.size());
209*b0d29bc4SBrooks Davis
210*b0d29bc4SBrooks Davis std::vector< text::table_row > textual_rows(1, text::table_row(row.size()));
211*b0d29bc4SBrooks Davis
212*b0d29bc4SBrooks Davis for (text::table_row::size_type column = 0; column < row.size(); ++column) {
213*b0d29bc4SBrooks Davis if (widths[column] > row[column].length())
214*b0d29bc4SBrooks Davis textual_rows[0][column] = pad_cell(row[column], widths[column],
215*b0d29bc4SBrooks Davis column == row.size() - 1);
216*b0d29bc4SBrooks Davis else
217*b0d29bc4SBrooks Davis refill_cell(row, widths, column, textual_rows);
218*b0d29bc4SBrooks Davis }
219*b0d29bc4SBrooks Davis
220*b0d29bc4SBrooks Davis std::vector< std::string > lines;
221*b0d29bc4SBrooks Davis for (std::vector< text::table_row >::const_iterator
222*b0d29bc4SBrooks Davis iter = textual_rows.begin(); iter != textual_rows.end(); ++iter) {
223*b0d29bc4SBrooks Davis lines.push_back(text::join(*iter, separator));
224*b0d29bc4SBrooks Davis }
225*b0d29bc4SBrooks Davis return lines;
226*b0d29bc4SBrooks Davis }
227*b0d29bc4SBrooks Davis
228*b0d29bc4SBrooks Davis
229*b0d29bc4SBrooks Davis } // anonymous namespace
230*b0d29bc4SBrooks Davis
231*b0d29bc4SBrooks Davis
232*b0d29bc4SBrooks Davis /// Constructs a new table.
233*b0d29bc4SBrooks Davis ///
234*b0d29bc4SBrooks Davis /// \param ncolumns_ The number of columns that the table will have.
table(const table_row::size_type ncolumns_)235*b0d29bc4SBrooks Davis text::table::table(const table_row::size_type ncolumns_)
236*b0d29bc4SBrooks Davis {
237*b0d29bc4SBrooks Davis _column_widths.resize(ncolumns_, 0);
238*b0d29bc4SBrooks Davis }
239*b0d29bc4SBrooks Davis
240*b0d29bc4SBrooks Davis
241*b0d29bc4SBrooks Davis /// Gets the number of columns in the table.
242*b0d29bc4SBrooks Davis ///
243*b0d29bc4SBrooks Davis /// \return The number of columns in the table. This value remains constant
244*b0d29bc4SBrooks Davis /// during the existence of the table.
245*b0d29bc4SBrooks Davis text::widths_vector::size_type
ncolumns(void) const246*b0d29bc4SBrooks Davis text::table::ncolumns(void) const
247*b0d29bc4SBrooks Davis {
248*b0d29bc4SBrooks Davis return _column_widths.size();
249*b0d29bc4SBrooks Davis }
250*b0d29bc4SBrooks Davis
251*b0d29bc4SBrooks Davis
252*b0d29bc4SBrooks Davis /// Gets the width of a column.
253*b0d29bc4SBrooks Davis ///
254*b0d29bc4SBrooks Davis /// The returned value is not valid if add_row() is called again, as the column
255*b0d29bc4SBrooks Davis /// may have grown in width.
256*b0d29bc4SBrooks Davis ///
257*b0d29bc4SBrooks Davis /// \param column The index of the column of which to get the width. Must be
258*b0d29bc4SBrooks Davis /// less than the total number of columns.
259*b0d29bc4SBrooks Davis ///
260*b0d29bc4SBrooks Davis /// \return The width of a column.
261*b0d29bc4SBrooks Davis text::widths_vector::value_type
column_width(const widths_vector::size_type column) const262*b0d29bc4SBrooks Davis text::table::column_width(const widths_vector::size_type column) const
263*b0d29bc4SBrooks Davis {
264*b0d29bc4SBrooks Davis PRE(column < _column_widths.size());
265*b0d29bc4SBrooks Davis return _column_widths[column];
266*b0d29bc4SBrooks Davis }
267*b0d29bc4SBrooks Davis
268*b0d29bc4SBrooks Davis
269*b0d29bc4SBrooks Davis /// Gets the widths of all columns.
270*b0d29bc4SBrooks Davis ///
271*b0d29bc4SBrooks Davis /// The returned value is not valid if add_row() is called again, as the columns
272*b0d29bc4SBrooks Davis /// may have grown in width.
273*b0d29bc4SBrooks Davis ///
274*b0d29bc4SBrooks Davis /// \return A vector with the width of all columns.
275*b0d29bc4SBrooks Davis const text::widths_vector&
column_widths(void) const276*b0d29bc4SBrooks Davis text::table::column_widths(void) const
277*b0d29bc4SBrooks Davis {
278*b0d29bc4SBrooks Davis return _column_widths;
279*b0d29bc4SBrooks Davis }
280*b0d29bc4SBrooks Davis
281*b0d29bc4SBrooks Davis
282*b0d29bc4SBrooks Davis /// Checks whether the table is empty or not.
283*b0d29bc4SBrooks Davis ///
284*b0d29bc4SBrooks Davis /// \return True if the table is empty; false otherwise.
285*b0d29bc4SBrooks Davis bool
empty(void) const286*b0d29bc4SBrooks Davis text::table::empty(void) const
287*b0d29bc4SBrooks Davis {
288*b0d29bc4SBrooks Davis return _rows.empty();
289*b0d29bc4SBrooks Davis }
290*b0d29bc4SBrooks Davis
291*b0d29bc4SBrooks Davis
292*b0d29bc4SBrooks Davis /// Adds a row to the table.
293*b0d29bc4SBrooks Davis ///
294*b0d29bc4SBrooks Davis /// \param row The row to be added. This row must have the same amount of
295*b0d29bc4SBrooks Davis /// columns as defined during the construction of the table.
296*b0d29bc4SBrooks Davis void
add_row(const table_row & row)297*b0d29bc4SBrooks Davis text::table::add_row(const table_row& row)
298*b0d29bc4SBrooks Davis {
299*b0d29bc4SBrooks Davis PRE(row.size() == _column_widths.size());
300*b0d29bc4SBrooks Davis _rows.push_back(row);
301*b0d29bc4SBrooks Davis
302*b0d29bc4SBrooks Davis for (table_row::size_type i = 0; i < row.size(); ++i)
303*b0d29bc4SBrooks Davis if (_column_widths[i] < row[i].length())
304*b0d29bc4SBrooks Davis _column_widths[i] = row[i].length();
305*b0d29bc4SBrooks Davis }
306*b0d29bc4SBrooks Davis
307*b0d29bc4SBrooks Davis
308*b0d29bc4SBrooks Davis /// Gets an iterator pointing to the beginning of the rows of the table.
309*b0d29bc4SBrooks Davis ///
310*b0d29bc4SBrooks Davis /// \return An iterator on the rows.
311*b0d29bc4SBrooks Davis text::table::const_iterator
begin(void) const312*b0d29bc4SBrooks Davis text::table::begin(void) const
313*b0d29bc4SBrooks Davis {
314*b0d29bc4SBrooks Davis return _rows.begin();
315*b0d29bc4SBrooks Davis }
316*b0d29bc4SBrooks Davis
317*b0d29bc4SBrooks Davis
318*b0d29bc4SBrooks Davis /// Gets an iterator pointing to the end of the rows of the table.
319*b0d29bc4SBrooks Davis ///
320*b0d29bc4SBrooks Davis /// \return An iterator on the rows.
321*b0d29bc4SBrooks Davis text::table::const_iterator
end(void) const322*b0d29bc4SBrooks Davis text::table::end(void) const
323*b0d29bc4SBrooks Davis {
324*b0d29bc4SBrooks Davis return _rows.end();
325*b0d29bc4SBrooks Davis }
326*b0d29bc4SBrooks Davis
327*b0d29bc4SBrooks Davis
328*b0d29bc4SBrooks Davis /// Column width to denote that the column has to fit all of its cells.
329*b0d29bc4SBrooks Davis const std::size_t text::table_formatter::width_auto = 0;
330*b0d29bc4SBrooks Davis
331*b0d29bc4SBrooks Davis
332*b0d29bc4SBrooks Davis /// Column width to denote that the column can be refilled to fit the table.
333*b0d29bc4SBrooks Davis const std::size_t text::table_formatter::width_refill =
334*b0d29bc4SBrooks Davis std::numeric_limits< std::size_t >::max();
335*b0d29bc4SBrooks Davis
336*b0d29bc4SBrooks Davis
337*b0d29bc4SBrooks Davis /// Constructs a new table formatter.
table_formatter(void)338*b0d29bc4SBrooks Davis text::table_formatter::table_formatter(void) :
339*b0d29bc4SBrooks Davis _separator(""),
340*b0d29bc4SBrooks Davis _table_width(0)
341*b0d29bc4SBrooks Davis {
342*b0d29bc4SBrooks Davis }
343*b0d29bc4SBrooks Davis
344*b0d29bc4SBrooks Davis
345*b0d29bc4SBrooks Davis /// Sets the width of a column.
346*b0d29bc4SBrooks Davis ///
347*b0d29bc4SBrooks Davis /// All columns except one must have a width that is, at least, as wide as the
348*b0d29bc4SBrooks Davis /// widest cell in the column. One of the columns can have a width of
349*b0d29bc4SBrooks Davis /// width_refill, which indicates that the column will be refilled if the table
350*b0d29bc4SBrooks Davis /// does not fit in its maximum width.
351*b0d29bc4SBrooks Davis ///
352*b0d29bc4SBrooks Davis /// \param column The index of the column to set the width for.
353*b0d29bc4SBrooks Davis /// \param width The width to set the column to.
354*b0d29bc4SBrooks Davis ///
355*b0d29bc4SBrooks Davis /// \return A reference to this formatter to allow using the builder pattern.
356*b0d29bc4SBrooks Davis text::table_formatter&
set_column_width(const table_row::size_type column,const std::size_t width)357*b0d29bc4SBrooks Davis text::table_formatter::set_column_width(const table_row::size_type column,
358*b0d29bc4SBrooks Davis const std::size_t width)
359*b0d29bc4SBrooks Davis {
360*b0d29bc4SBrooks Davis #if !defined(NDEBUG)
361*b0d29bc4SBrooks Davis if (width == width_refill) {
362*b0d29bc4SBrooks Davis for (widths_vector::size_type i = 0; i < _column_widths.size(); i++) {
363*b0d29bc4SBrooks Davis if (i != column)
364*b0d29bc4SBrooks Davis PRE_MSG(_column_widths[i] != width_refill,
365*b0d29bc4SBrooks Davis "Only one column width can be set to width_refill");
366*b0d29bc4SBrooks Davis }
367*b0d29bc4SBrooks Davis }
368*b0d29bc4SBrooks Davis #endif
369*b0d29bc4SBrooks Davis
370*b0d29bc4SBrooks Davis if (_column_widths.size() < column + 1)
371*b0d29bc4SBrooks Davis _column_widths.resize(column + 1, width_auto);
372*b0d29bc4SBrooks Davis _column_widths[column] = width;
373*b0d29bc4SBrooks Davis return *this;
374*b0d29bc4SBrooks Davis }
375*b0d29bc4SBrooks Davis
376*b0d29bc4SBrooks Davis
377*b0d29bc4SBrooks Davis /// Sets the separator to use between the cells.
378*b0d29bc4SBrooks Davis ///
379*b0d29bc4SBrooks Davis /// \param separator The separator to use.
380*b0d29bc4SBrooks Davis ///
381*b0d29bc4SBrooks Davis /// \return A reference to this formatter to allow using the builder pattern.
382*b0d29bc4SBrooks Davis text::table_formatter&
set_separator(const char * separator)383*b0d29bc4SBrooks Davis text::table_formatter::set_separator(const char* separator)
384*b0d29bc4SBrooks Davis {
385*b0d29bc4SBrooks Davis _separator = separator;
386*b0d29bc4SBrooks Davis return *this;
387*b0d29bc4SBrooks Davis }
388*b0d29bc4SBrooks Davis
389*b0d29bc4SBrooks Davis
390*b0d29bc4SBrooks Davis /// Sets the maximum width of the table.
391*b0d29bc4SBrooks Davis ///
392*b0d29bc4SBrooks Davis /// \param table_width The maximum width of the table; cannot be zero.
393*b0d29bc4SBrooks Davis ///
394*b0d29bc4SBrooks Davis /// \return A reference to this formatter to allow using the builder pattern.
395*b0d29bc4SBrooks Davis text::table_formatter&
set_table_width(const std::size_t table_width)396*b0d29bc4SBrooks Davis text::table_formatter::set_table_width(const std::size_t table_width)
397*b0d29bc4SBrooks Davis {
398*b0d29bc4SBrooks Davis PRE(table_width > 0);
399*b0d29bc4SBrooks Davis _table_width = table_width;
400*b0d29bc4SBrooks Davis return *this;
401*b0d29bc4SBrooks Davis }
402*b0d29bc4SBrooks Davis
403*b0d29bc4SBrooks Davis
404*b0d29bc4SBrooks Davis /// Formats a table into a collection of textual lines.
405*b0d29bc4SBrooks Davis ///
406*b0d29bc4SBrooks Davis /// \param t Table to format.
407*b0d29bc4SBrooks Davis ///
408*b0d29bc4SBrooks Davis /// \return A collection of textual lines.
409*b0d29bc4SBrooks Davis std::vector< std::string >
format(const table & t) const410*b0d29bc4SBrooks Davis text::table_formatter::format(const table& t) const
411*b0d29bc4SBrooks Davis {
412*b0d29bc4SBrooks Davis std::vector< std::string > lines;
413*b0d29bc4SBrooks Davis
414*b0d29bc4SBrooks Davis if (!t.empty()) {
415*b0d29bc4SBrooks Davis widths_vector widths = override_column_widths(t, _column_widths);
416*b0d29bc4SBrooks Davis if (_table_width != 0)
417*b0d29bc4SBrooks Davis refill_widths(widths, _table_width, _separator.length());
418*b0d29bc4SBrooks Davis
419*b0d29bc4SBrooks Davis for (table::const_iterator iter = t.begin(); iter != t.end(); ++iter) {
420*b0d29bc4SBrooks Davis const std::vector< std::string > sublines =
421*b0d29bc4SBrooks Davis format_row(*iter, widths, _separator);
422*b0d29bc4SBrooks Davis std::copy(sublines.begin(), sublines.end(),
423*b0d29bc4SBrooks Davis std::back_inserter(lines));
424*b0d29bc4SBrooks Davis }
425*b0d29bc4SBrooks Davis }
426*b0d29bc4SBrooks Davis
427*b0d29bc4SBrooks Davis return lines;
428*b0d29bc4SBrooks Davis }
429