2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \libinternal \file
37 * Declares common string formatting routines for online help.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_onlinehelp
43 #ifndef GMX_ONLINEHELP_HELPFORMAT_H
44 #define GMX_ONLINEHELP_HELPFORMAT_H
48 #include "../utility/common.h"
55 /*! \libinternal \brief
56 * Formats rows of a table for text output.
58 * This utility class formats tabular data, mainly for console output.
59 * Each row in the table can take multiple lines, and automatic text wrapping
60 * is supported. If text overflows the allocated width, the remaining columns
61 * on that line become shifted. To avoid this, it is possible to start the
62 * output for different columns from different lines (it is the caller's
63 * responsibility to check that overflows are avoided or are acceptable).
65 * Column definitions are first set with addColumn().
66 * To format a row, first call clear(). Then call addColumnLine() to add text
67 * to each column (can be called multiple times on a single column to add
68 * multiple lines), and possibly setColumnFirstLineOffset() to adjust the line
69 * from which the column output should start. Finally, call formatRow() to
70 * obtain the formatted row.
72 * A header (column titles and a horizontal line) is printed before the first
77 gmx::TextTableFormatter formatter;
78 formatter.addColumn("Name", 10, false);
79 formatter.addColumn("Type", 10, false);
80 formatter.addColumn("Description", 50, true);
83 formatter.addColumnLine(0, "name");
84 formatter.addColumnLine(1, "type");
85 formatter.addColumnLine(2, "Description for name");
86 printf("%s", formatter.formatRow().c_str());
89 formatter.addColumnLine(0, "averylongname");
90 formatter.addColumnLine(1, "type");
91 formatter.setColumnFirstLineOffset(1, 1);
92 formatter.addColumnLine(2, "Description for name");
93 printf("%s", formatter.formatRow().c_str());
95 // format other rows by repeating the above code
98 * Methods in this class may throw std::bad_alloc if out of memory.
99 * Other exceptions are not thrown.
102 * \ingroup module_onlinehelp
104 class TextTableFormatter
107 //! Constructs an empty formatter.
108 TextTableFormatter();
109 ~TextTableFormatter();
112 * Adds a column to the table.
114 * \param[in] title Title string for the column (used for header).
115 * \param[in] width Width of the column (must be > 0).
116 * \param[in] bWrap Whether text that exceeds \p width is
117 * automatically wrapped.
119 * The length of \p title must not exceed \p width.
121 void addColumn(const char *title, int width, bool bWrap);
123 * Sets amount on indentation before the first column.
125 * \param[in] indent Number of spaces to use for indenting.
129 void setFirstColumnIndent(int indent);
132 * Whether formatRow() has been successfully called.
134 * This method can be used to determine after-the-fact whether anything
135 * was written in the table.
139 bool didOutput() const;
142 * Removes all text from all columns and resets the line offsets.
144 * Removes all text added using addColumnLine() and resets line offsets
145 * set with setColumnFirstLineOffset() to zero.
146 * Should be called before starting to add data for a row.
152 * Adds text to be printed in a column.
154 * \param[in] index Zero-based column index.
155 * \param[in] text Text to add.
157 * Can be called multiple times. Additional calls append \p text as
158 * additional lines. Any calls with \p text empty have no effect.
159 * To add an empty line, use "\n" as \p text.
161 * If \p text contains newlines, the text is automatically splitted to
162 * multiple lines. The same happens if automatic wrapping is on for
163 * the column and the text contains lines that are longer than what
166 void addColumnLine(int index, const std::string &text);
168 * Sets the first line to which text is printed for a column.
170 * \param[in] index Zero-based column index.
171 * \param[in] firstLine Zero-based line index from which to start the
174 * Can be called if there is no text for column \p index.
175 * Does not affect the output in this case.
179 void setColumnFirstLineOffset(int index, int firstLine);
181 * Formats the lines for the current row.
183 * \returns Current row formatted as a single string
184 * (contains newlines).
186 * Formats the data as set after the previous clear()/formatRow() using
187 * addColumnLine() and setColumnFirstLineOffset().
189 * If this is the first line to be formatted, a header is also added to
190 * the beginning of the returned string if any column has a title.
192 * The return value always terminates with a newline.
194 * Calls clear() on successful return.
196 std::string formatRow();
199 * Returns the last line on which column \p index has text.
201 * \param[in] index Zero-based column index.
202 * \returns Last line index (zero-based) on which \p index has text.
204 * The return value is the sum of the number of lines added with
205 * addColumnLine() (taking into account possible wrapping) and the line
206 * offset set with setColumnFirstLineOffset().
210 int lastColumnLine(int index) const;
215 PrivateImplPointer<Impl> impl_;