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