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