libyui-ncurses  2.55.0
NCWordWrapper.h
1 /*
2  Copyright (C) 2020 SUSE LLC
3 
4  This library is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) version 3.0 of the License. This library
8  is distributed in the hope that it will be useful, but WITHOUT ANY
9  WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11  License for more details. You should have received a copy of the GNU
12  Lesser General Public License along with this library; if not, write
13  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
14  Floor, Boston, MA 02110-1301 USA
15 */
16 
17 
18 /*-/
19 
20  File: NCWordWrapper.h
21 
22  Author: Stefan Hundhammer <shundhammer@suse.de>
23 
24 /-*/
25 
26 
27 #ifndef NCWordWrapper_h
28 #define NCWordWrapper_h
29 
30 #include <string>
31 
32 /**
33  * Helper class to word-wrap text into a specified maximum line width.
34  * Whitespace is normalized in the process, i.e. any sequence of whitespace
35  * (blanks, newlines, tabs, ...) is replaced by a single blank. All lines end
36  * with a single newline character except the last one which has no newline.
37  **/
39 {
40 public:
41 
42  /**
43  * Constructor.
44  **/
45  NCWordWrapper();
46 
47  /**
48  * Set the original text to wrap.
49  **/
50  void setText( const std::wstring & origText );
51 
52  /**
53  * Set the maximum line width to wrap into.
54  **/
55  void setLineWidth( int width );
56 
57  /**
58  * Return the number of lines after wrapping the original text.
59  **/
60  int lines();
61 
62  /**
63  * Wrap the original text and return the wrapped text.
64  **/
65  const std::wstring & wrappedText();
66 
67  /**
68  * Return the original unwrapped text.
69  **/
70  const std::wstring & origText() const { return _origText; }
71 
72  /**
73  * Return the last used maximum line width.
74  **/
75  int lineWidth() const { return _lineWidth; }
76 
77  /**
78  * Return a string where any sequence of whitespace in the original text is
79  * replaced with a single blank and without leading or trailing whitespace.
80  **/
81  static std::wstring normalizeWhitespace( const std::wstring & orig );
82 
83  /**
84  * Do the wrapping.
85  *
86  * This normally doesn't need to be called manually; it is done
87  * automatically when retrieving the wrapped text or the number of wrapped
88  * lines (and when the internal 'dirty' flag is set).
89  *
90  * But it can be useful to call it manually for debugging and testing.
91  **/
92  void wrap();
93 
94  /**
95  * Clear the old content.
96  **/
97  void clear();
98 
99 
100 protected:
101 
102  /**
103  * Do the wrapping if necessary.
104  **/
105  void ensureWrapped();
106 
107  /**
108  * Return the next line that fits into the line width and removed it from
109  * 'unwrapped'.
110  **/
111  std::wstring nextLine( std::wstring & unwrapped );
112 
113  //
114  // Data members
115  //
116 
117  std::wstring _origText;
118  std::wstring _wrappedText;
119  int _lineWidth;
120  int _lines;
121  bool _dirty;
122 };
123 
124 #endif // NCWordWrapper_h
NCWordWrapper::setLineWidth
void setLineWidth(int width)
Set the maximum line width to wrap into.
Definition: NCWordWrapper.cc:58
NCWordWrapper::normalizeWhitespace
static std::wstring normalizeWhitespace(const std::wstring &orig)
Return a string where any sequence of whitespace in the original text is replaced with a single blank...
Definition: NCWordWrapper.cc:103
NCWordWrapper::wrap
void wrap()
Do the wrapping.
Definition: NCWordWrapper.cc:145
NCWordWrapper::ensureWrapped
void ensureWrapped()
Do the wrapping if necessary.
Definition: NCWordWrapper.cc:94
NCWordWrapper::nextLine
std::wstring nextLine(std::wstring &unwrapped)
Return the next line that fits into the line width and removed it from 'unwrapped'.
Definition: NCWordWrapper.cc:172
NCWordWrapper::setText
void setText(const std::wstring &origText)
Set the original text to wrap.
Definition: NCWordWrapper.cc:48
NCWordWrapper
Helper class to word-wrap text into a specified maximum line width.
Definition: NCWordWrapper.h:39
NCWordWrapper::wrappedText
const std::wstring & wrappedText()
Wrap the original text and return the wrapped text.
Definition: NCWordWrapper.cc:86
NCWordWrapper::clear
void clear()
Clear the old content.
Definition: NCWordWrapper.cc:68
NCWordWrapper::origText
const std::wstring & origText() const
Return the original unwrapped text.
Definition: NCWordWrapper.h:70
NCWordWrapper::lineWidth
int lineWidth() const
Return the last used maximum line width.
Definition: NCWordWrapper.h:75
NCWordWrapper::lines
int lines()
Return the number of lines after wrapping the original text.
Definition: NCWordWrapper.cc:78
NCWordWrapper::NCWordWrapper
NCWordWrapper()
Constructor.
Definition: NCWordWrapper.cc:39