A utility class to word-wrap and indent blocks of text.
More...
|
| TextWrapper (string input) |
|
TextWrapper | WordWrap (int columnWidth) |
| Splits a string into a words and performs wrapping while also preserving line-breaks and sub-indentation.
|
|
TextWrapper | Indent (int numberOfSpaces) |
| Indent all lines in the TextWrapper by the desired number of spaces.
|
|
string | ToText () |
| Returns the current state of the TextWrapper as a string.
|
|
|
static string | WrapAndIndentText (string input, int indentLevel, int columnWidth) |
| Convenience method to wraps and indent a string in a single operation.
|
|
|
static List< StringBuilder > | AddWordToLastLineOrCreateNewLineIfNecessary (List< StringBuilder > lines, string word, int columnWidth) |
| When presented with a word, either append to the last line in the list or start a new line.
|
|
static string | RightString (string str, int n) |
| Return the right part of a string in a way that compensates for Substring's deficiencies.
|
|
static string | LeftString (string str, int n) |
| Return the left part of a string in a way that compensates for Substring's deficiencies.
|
|
A utility class to word-wrap and indent blocks of text.
Definition at line 13 of file TextWrapper.cs.
◆ TextWrapper()
CommandLine.Text.TextWrapper.TextWrapper |
( |
string |
input | ) |
|
Definition at line 17 of file TextWrapper.cs.
18 {
19
20
21
22
24 .Replace("\r", "")
25 .Split(new[] { '\n' },
26 StringSplitOptions.None
27 );
28 }
◆ AddWordToLastLineOrCreateNewLineIfNecessary()
static List< StringBuilder > CommandLine.Text.TextWrapper.AddWordToLastLineOrCreateNewLineIfNecessary |
( |
List< StringBuilder > |
lines, |
|
|
string |
word, |
|
|
int |
columnWidth |
|
) |
| |
|
staticprivate |
When presented with a word, either append to the last line in the list or start a new line.
- Parameters
-
lines | A list of StringBuilders containing results so far |
word | The individual word to append |
columnWidth | The usable text space |
The 'word' can actually be an empty string. It's important to keep these - empty strings allow us to preserve indentation and extra spaces within a line.
- Returns
- The same list as is passed in
Definition at line 134 of file TextWrapper.cs.
138 {
139
140 string previousLine =
lines.LastOrDefault()
141 ?.ToString() ??
142 string.Empty;
143
144 bool wouldWrap = !
lines.Any() || (word.Length > 0 && previousLine.Length + word.Length > columnWidth);
145
146 if (!wouldWrap)
147 {
148
149
150
152 .Append(word + ' ');
153 }
154 else
155 {
156
157
158
159
160
161
162
163 do
164 {
165 int availableCharacters = Math.Min(columnWidth, word.Length);
166 string segmentToAdd =
LeftString(word, availableCharacters) +
' ';
167 lines.Add(
new StringBuilder(segmentToAdd));
169 }
170 while (word.Length > 0);
171 }
172
174 }
static string RightString(string str, int n)
Return the right part of a string in a way that compensates for Substring's deficiencies.
static string LeftString(string str, int n)
Return the left part of a string in a way that compensates for Substring's deficiencies.
◆ Indent()
TextWrapper CommandLine.Text.TextWrapper.Indent |
( |
int |
numberOfSpaces | ) |
|
Indent all lines in the TextWrapper by the desired number of spaces.
- Parameters
-
numberOfSpaces | The number of spaces to indent by |
- Returns
- this
Definition at line 63 of file TextWrapper.cs.
64 {
66 .Select(line => numberOfSpaces.Spaces() + line)
67 .ToArray();
68
69 return this;
70 }
◆ LeftString()
static string CommandLine.Text.TextWrapper.LeftString |
( |
string |
str, |
|
|
int |
n |
|
) |
| |
|
staticprivate |
Return the left part of a string in a way that compensates for Substring's deficiencies.
Definition at line 190 of file TextWrapper.cs.
191 {
192 return n >= str.Length || str.Length == 0
193 ? str
194 : str.Substring(0, n);
195 }
◆ RightString()
static string CommandLine.Text.TextWrapper.RightString |
( |
string |
str, |
|
|
int |
n |
|
) |
| |
|
staticprivate |
Return the right part of a string in a way that compensates for Substring's deficiencies.
Definition at line 180 of file TextWrapper.cs.
181 {
182 return n >= str.Length || str.Length == 0
183 ? string.Empty
184 : str.Substring(n);
185 }
◆ ToText()
string CommandLine.Text.TextWrapper.ToText |
( |
| ) |
|
Returns the current state of the TextWrapper as a string.
- Returns
Definition at line 76 of file TextWrapper.cs.
77 {
78
79 return string.Join(Environment.NewLine,
lines);
80 }
◆ WordWrap()
TextWrapper CommandLine.Text.TextWrapper.WordWrap |
( |
int |
columnWidth | ) |
|
Splits a string into a words and performs wrapping while also preserving line-breaks and sub-indentation.
- Parameters
-
columnWidth | The number of characters we can use for text |
This method attempts to wrap text without breaking words For example, if columnWidth is 10 , the input "a string for wrapping 01234567890123" would return "a string
"for "wrapping
"0123456789 "0123"
- Returns
- this
Definition at line 46 of file TextWrapper.cs.
47 {
48
49 columnWidth = Math.Max(1, columnWidth);
50
53 .ToArray();
54
55 return this;
56 }
string[] WordWrapLine(string line, int columnWidth)
◆ WordWrapLine()
string[] CommandLine.Text.TextWrapper.WordWrapLine |
( |
string |
line, |
|
|
int |
columnWidth |
|
) |
| |
|
private |
Definition at line 102 of file TextWrapper.cs.
103 {
104
105
106
107 string unindentedLine = line.TrimStart();
108 int currentIndentLevel = Math.Min(line.Length - unindentedLine.Length, columnWidth - 1);
109 columnWidth -= currentIndentLevel;
110
111 return unindentedLine.Split(' ')
112 .Aggregate(new List<StringBuilder>(),
113 (lineList, word) =>
115 )
116 .Select(builder => currentIndentLevel.Spaces() +
117 builder.ToString()
118 .TrimEnd()
119 )
120 .ToArray();
121 }
static List< StringBuilder > AddWordToLastLineOrCreateNewLineIfNecessary(List< StringBuilder > lines, string word, int columnWidth)
When presented with a word, either append to the last line in the list or start a new line.
◆ WrapAndIndentText()
static string CommandLine.Text.TextWrapper.WrapAndIndentText |
( |
string |
input, |
|
|
int |
indentLevel, |
|
|
int |
columnWidth |
|
) |
| |
|
static |
Convenience method to wraps and indent a string in a single operation.
- Parameters
-
input | The string to operate on |
indentLevel | The number of spaces to indent by |
columnWidth | The width of the column used for wrapping |
The string is wrapped then indented so the columnWidth is the width of the usable text block, and does NOT include the indentLevel.
- Returns
- the processed string
Definition at line 93 of file TextWrapper.cs.
94 {
96 .WordWrap(columnWidth)
97 .Indent(indentLevel)
98 .ToText();
99 }
TextWrapper(string input)
◆ lines
string [] CommandLine.Text.TextWrapper.lines |
|
private |
The documentation for this class was generated from the following file: