6891adfb0845bdd56c32cb4822110f2b4d673146
[alexxy/gromacs.git] / src / gromacs / onlinehelp / helpformat.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \libinternal \file
36  * \brief
37  * Declares common string formatting routines for online help.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_onlinehelp
42  */
43 #ifndef GMX_ONLINEHELP_HELPFORMAT_H
44 #define GMX_ONLINEHELP_HELPFORMAT_H
45
46 #include <string>
47
48 #include "../utility/common.h"
49
50 namespace gmx
51 {
52
53 class File;
54 class HelpWriterContext;
55
56 /*! \libinternal \brief
57  * Formats rows of a table for text output.
58  *
59  * This utility class formats tabular data, mainly for console output.
60  * Each row in the table can take multiple lines, and automatic text wrapping
61  * is supported.  If text overflows the allocated width, the remaining columns
62  * on that line become shifted.  To avoid this, it is possible to start the
63  * output for different columns from different lines (it is the caller's
64  * responsibility to check that overflows are avoided or are acceptable).
65  *
66  * Column definitions are first set with addColumn().
67  * To format a row, first call clear().  Then call addColumnLine() to add text
68  * to each column (can be called multiple times on a single column to add
69  * multiple lines), and possibly setColumnFirstLineOffset() to adjust the line
70  * from which the column output should start.  Finally, call formatRow() to
71  * obtain the formatted row.
72  *
73  * A header (column titles and a horizontal line) is printed before the first
74  * line.
75  *
76  * Typical usage:
77  * \code
78    gmx::TextTableFormatter formatter;
79    formatter.addColumn("Name", 10, false);
80    formatter.addColumn("Type", 10, false);
81    formatter.addColumn("Description", 50, true);
82
83    formatter.clear();
84    formatter.addColumnLine(0, "name");
85    formatter.addColumnLine(1, "type");
86    formatter.addColumnLine(2, "Description for name");
87    printf("%s", formatter.formatRow().c_str());
88
89    formatter.clear();
90    formatter.addColumnLine(0, "averylongname");
91    formatter.addColumnLine(1, "type");
92    formatter.setColumnFirstLineOffset(1, 1);
93    formatter.addColumnLine(2, "Description for name");
94    printf("%s", formatter.formatRow().c_str());
95
96    // format other rows by repeating the above code
97  * \endcode
98  *
99  * Methods in this class may throw std::bad_alloc if out of memory.
100  * Other exceptions are not thrown.
101  *
102  * \inlibraryapi
103  * \ingroup module_onlinehelp
104  */
105 class TextTableFormatter
106 {
107     public:
108         //! Constructs an empty formatter.
109         TextTableFormatter();
110         ~TextTableFormatter();
111
112         /*! \brief
113          * Adds a column to the table.
114          *
115          * \param[in]  title  Title string for the column (used for header).
116          * \param[in]  width  Width of the column (must be > 0).
117          * \param[in]  bWrap  Whether text that exceeds \p width is
118          *      automatically wrapped.
119          *
120          * The length of \p title must not exceed \p width.
121          */
122         void addColumn(const char *title, int width, bool bWrap);
123         /*! \brief
124          * Sets amount on indentation before the first column.
125          *
126          * \param[in]  indent  Number of spaces to use for indenting.
127          *
128          * Does not throw.
129          */
130         void setFirstColumnIndent(int indent);
131         /*! \brief
132          * Enables folding the last column to separate lines if it overflows.
133          *
134          * \param[in]  indent  Number of spaces to use for indenting the lines.
135          *
136          * If called with `indent >= 0`, the last column for each row is
137          * treated specially: if it contains more lines than the other columns,
138          * and if the text would fit more compactly as separate lines after the
139          * row, then the whole last column is written after the row with the
140          * given \p indent.  The column text then spans the whole space
141          * reserved for the table, making long text fit into a smaller amount
142          * of vertical space.
143          * If not called, the last column is not treates specially.
144          *
145          * Does not throw.
146          */
147         void setFoldLastColumnToNextLine(int indent);
148
149         /*! \brief
150          * Whether formatRow() has been successfully called.
151          *
152          * This method can be used to determine after-the-fact whether anything
153          * was written in the table.
154          *
155          * Does not throw.
156          */
157         bool didOutput() const;
158
159         /*! \brief
160          * Removes all text from all columns and resets the line offsets.
161          *
162          * Removes all text added using addColumnLine() and resets line offsets
163          * set with setColumnFirstLineOffset() to zero.
164          * Should be called before starting to add data for a row.
165          *
166          * Does not throw.
167          */
168         void clear();
169         /*! \brief
170          * Adds text to be printed in a column.
171          *
172          * \param[in]  index     Zero-based column index.
173          * \param[in]  text      Text to add.
174          *
175          * Can be called multiple times.  Additional calls append \p text as
176          * additional lines.  Any calls with \p text empty have no effect.
177          * To add an empty line, use "\n" as \p text.
178          *
179          * If \p text contains newlines, the text is automatically splitted to
180          * multiple lines.  The same happens if automatic wrapping is on for
181          * the column and the text contains lines that are longer than what
182          * fits the column.
183          */
184         void addColumnLine(int index, const std::string &text);
185         /*! \brief
186          * Adds text containing help markup to be printed in a column.
187          *
188          * \param[in]  index     Zero-based column index.
189          * \param[in]  context   Context to use for markup processing.
190          * \param[in]  text      Text to add.
191          *
192          * Works as addColumnLine(), except that it uses
193          * HelpWriterContext::substituteMarkupAndWrapToVector() to process
194          * markup in the input text instead of just wrapping it as plain text.
195          */
196         void addColumnHelpTextBlock(int index, const HelpWriterContext &context,
197                                     const std::string &text);
198         /*! \brief
199          * Sets the first line to which text is printed for a column.
200          *
201          * \param[in]  index     Zero-based column index.
202          * \param[in]  firstLine Zero-based line index from which to start the
203          *      output.
204          *
205          * Can be called if there is no text for column \p index.
206          * Does not affect the output in this case.
207          *
208          * Does not throw.
209          */
210         void setColumnFirstLineOffset(int index, int firstLine);
211         /*! \brief
212          * Formats the lines for the current row.
213          *
214          * \returns  Current row formatted as a single string
215          *      (contains newlines).
216          *
217          * Formats the data as set after the previous clear()/formatRow() using
218          * addColumnLine() and setColumnFirstLineOffset().
219          *
220          * If this is the first line to be formatted, a header is also added to
221          * the beginning of the returned string if any column has a title.
222          *
223          * The return value always terminates with a newline.
224          *
225          * Calls clear() on successful return.
226          */
227         std::string formatRow();
228
229     private:
230         class Impl;
231
232         PrivateImplPointer<Impl> impl_;
233 };
234
235 } // namespace gmx
236
237 #endif