BadScript 2
Loading...
Searching...
No Matches
CommandLine.Text.TextWrapper Class Reference

A utility class to word-wrap and indent blocks of text. More...

Public Member Functions

 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 Public Member Functions

static string WrapAndIndentText (string input, int indentLevel, int columnWidth)
 Convenience method to wraps and indent a string in a single operation.
 

Private Member Functions

string[] WordWrapLine (string line, int columnWidth)
 

Static Private Member Functions

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.
 

Private Attributes

string[] lines
 

Detailed Description

A utility class to word-wrap and indent blocks of text.

Definition at line 13 of file TextWrapper.cs.

Constructor & Destructor Documentation

◆ TextWrapper()

CommandLine.Text.TextWrapper.TextWrapper ( string  input)

Definition at line 17 of file TextWrapper.cs.

18 {
19 //start by splitting at newlines and then reinserting the newline as a separate word
20 //Note that on the input side, we can't assume the line-break style at run time so we have to
21 //be able to handle both. We can't use Environment.NewLine because that changes at
22 //_runtime_ and may not match the line-break style that was compiled in
23 lines = input
24 .Replace("\r", "")
25 .Split(new[] { '\n' },
26 StringSplitOptions.None
27 );
28 }

Member Function Documentation

◆ 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
linesA list of StringBuilders containing results so far
wordThe individual word to append
columnWidthThe 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 //The current indentation level is based on the previous line but we need to be careful
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 //The usual case is we just append the 'word' and a space to the current line
149 //Note that trailing spaces will get removed later when we turn the line list
150 //into a single string
151 lines.Last()
152 .Append(word + ' ');
153 }
154 else
155 {
156 //The 'while' here is to take account of the possibility of someone providing a word
157 //which just can't fit in the current column. In that case we just split it at the
158 //column end.
159 //That's a rare case though - most of the time we'll succeed in a single pass without
160 //having to split
161 //Note that we always do at least one pass even if the 'word' is empty in order to
162 //honour sub-indentation and extra spaces within strings
163 do
164 {
165 int availableCharacters = Math.Min(columnWidth, word.Length);
166 string segmentToAdd = LeftString(word, availableCharacters) + ' ';
167 lines.Add(new StringBuilder(segmentToAdd));
168 word = RightString(word, availableCharacters);
169 }
170 while (word.Length > 0);
171 }
172
173 return lines;
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
numberOfSpacesThe number of spaces to indent by
Returns
this

Definition at line 63 of file TextWrapper.cs.

64 {
65 lines = lines
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 //return the whole thing as a single string
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
columnWidthThe 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 //ensure we always use at least 1 column even if the client has told us there's no space available
49 columnWidth = Math.Max(1, columnWidth);
50
51 lines = lines
52 .SelectMany(line => WordWrapLine(line, columnWidth))
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 //create a list of individual lines generated from the supplied line
105
106 //When handling sub-indentation we must always reserve at least one column for text!
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) =>
114 AddWordToLastLineOrCreateNewLineIfNecessary(lineList, word, columnWidth)
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
inputThe string to operate on
indentLevelThe number of spaces to indent by
columnWidthThe 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 {
95 return new TextWrapper(input)
96 .WordWrap(columnWidth)
97 .Indent(indentLevel)
98 .ToText();
99 }

Member Data Documentation

◆ lines

string [] CommandLine.Text.TextWrapper.lines
private

Definition at line 15 of file TextWrapper.cs.


The documentation for this class was generated from the following file: