LineWrappingTextWriter Class

Implements a TextWriter that writes text to another TextWriter, white-space wrapping lines at the specified maximum line length, and supporting hanging indentation.

Definition

Namespace: Ookii.CommandLine
Assembly: Ookii.CommandLine (in Ookii.CommandLine.dll) Version: 4.2.0+a4d3631e4dcc0970081ed435288efdebc1325f83
C#
public class LineWrappingTextWriter : TextWriter
Inheritance
Object    MarshalByRefObject    TextWriter    LineWrappingTextWriter

Remarks

If the MaximumLineLength property is not zero, the LineWrappingTextWriter will buffer the data written to it until an explicit new line is present in the text, or until the length of the buffered data exceeds the value of the MaximumLineLength property.

If the length of the buffered data exceeds the value of the MaximumLineLength property, the LineWrappingTextWriter will attempt to find a white-space character to break the line at. If such a white-space character is found, everything before that character is output to the BaseWriter, followed by a line ending, and everything after that character is kept in the buffer. The white-space character itself is not written to the output.

If no suitable place to break the line could be found, the line is broken at the maximum line length. This may occur in the middle of a word. If the Wrapping property is set to EnabledNoForce, lines without a suitable white-space character will not be wrapped and can be longer than the value of the MaximumLineLength property.

After a line break (either one that was caused by wrapping or one that was part of the text), the next line is indented by the number of characters specified by the Indent property, unless the previous line was blank. The length of the indentation counts towards the maximum line length.

When the Flush or FlushAsync method is called, the current contents of the buffer are written to the BaseWriter, followed by a new line, unless the buffer is empty. If the buffer contains only indentation, it is considered empty and no new line is written. Calling Flush has the same effect as writing a new line to the LineWrappingTextWriter if the buffer is not empty. The LineWrappingTextWriter is flushed when the Dispose(Boolean) or DisposeAsync method is called.

The ResetIndent or ResetIndentAsync(CancellationToken) method can be used to move the output position back to the beginning of the line. If the buffer is not empty, is first flushed and indentation is reset to zero on the next line. After the next line break, indentation will again be set to the value of the Indent property.

If there is no maximum line length, output is written directly to the BaseWriter and buffering does not occur. Indentation is still inserted as appropriate.

The Flush, FlushAsync, Dispose(Boolean) and DisposeAsync methods will not write an additional new line if the MaximumLineLength property is zero.

Constructors

LineWrappingTextWriter Initializes a new instance of the LineWrappingTextWriter class.

Properties

BaseWriter Gets the TextWriter that this LineWrappingTextWriter is writing to.
Encoding Gets the character encoding in which the output is written.
(Overrides TextWriterEncoding)
FormatProviderGets an object that controls formatting.
(Inherited from TextWriter)
Indent Gets or sets the amount of characters to indent all but the first line.
IndentAfterEmptyLine Gets or sets a value which indicates whether a line after an empty line should have indentation.
MaximumLineLength Gets the maximum length of a line in the output.
NewLineGets or sets the line terminator string used by the current TextWriter.
(Overrides TextWriterNewLine)
Wrapping Gets or sets a value which indicates how to wrap lines at the maximum line length.

Methods

CloseCloses the current writer and releases any system resources associated with the writer.
(Inherited from TextWriter)
DisposeReleases all resources used by the TextWriter object.
(Inherited from TextWriter)
Dispose(Boolean)Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources.
(Overrides TextWriterDispose(Boolean))
DisposeAsyncAsynchronously releases all resources used by the TextWriter object.
(Overrides TextWriterDisposeAsync)
EqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object)
FlushClears all buffers for the current writer and causes any buffered data to be written to the underlying device.
(Overrides TextWriterFlush)
Flush(Boolean) Clears all buffers for this TextWriter and causes any buffered data to be written to the underlying writer, optionally inserting an additional new line.
FlushAsyncAsynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
(Overrides TextWriterFlushAsync)
FlushAsync(CancellationToken)Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
(Inherited from TextWriter)
FlushAsync(Boolean, CancellationToken) Clears all buffers for this TextWriter and causes any buffered data to be written to the underlying writer, optionally inserting an additional new line.
ForConsoleError Gets a LineWrappingTextWriter that writes to the standard error stream, using ConsoleWindowWidth as the maximum line length.
ForConsoleOut Gets a LineWrappingTextWriter that writes to the standard output stream, using ConsoleWindowWidth as the maximum line length.
ForStandardStream Gets a LineWrappingTextWriter that writes to the specified standard stream.
ForStringWriter Gets a LineWrappingTextWriter that writes to a StringWriter.
GetHashCodeServes as the default hash function.
(Inherited from Object)
GetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Obsolete.
GetTypeGets the Type of the current instance.
(Inherited from Object)
InitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Obsolete.
MemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
MemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject)
ResetIndent Restarts writing on the beginning of the line, without indenting that line.
ResetIndentAsync Restarts writing on the beginning of the line, without indenting that line.
ToString Returns a string representation of the current LineWrappingTextWriter instance.
(Overrides ObjectToString)
Write(Boolean)Writes the text representation of a Boolean value to the text stream.
(Inherited from TextWriter)
Write(Char)Writes a character to the text stream.
(Overrides TextWriterWrite(Char))
Write(Char)Writes a character array to the text stream.
(Inherited from TextWriter)
Write(Decimal)Writes the text representation of a decimal value to the text stream.
(Inherited from TextWriter)
Write(Double)Writes the text representation of an 8-byte floating-point value to the text stream.
(Inherited from TextWriter)
Write(Int32)Writes the text representation of a 4-byte signed integer to the text stream.
(Inherited from TextWriter)
Write(Int64)Writes the text representation of an 8-byte signed integer to the text stream.
(Inherited from TextWriter)
Write(Object)Writes the text representation of an object to the text stream by calling the ToString method on that object.
(Inherited from TextWriter)
Write(ReadOnlySpanChar)Writes a character span to the text stream.
(Overrides TextWriterWrite(ReadOnlySpanChar))
Write(Single)Writes the text representation of a 4-byte floating-point value to the text stream.
(Inherited from TextWriter)
Write(String)Writes a string to the text stream.
(Overrides TextWriterWrite(String))
Write(StringBuilder)Writes a string builder to the text stream.
(Inherited from TextWriter)
Write(UInt32)Writes the text representation of a 4-byte unsigned integer to the text stream.
(Inherited from TextWriter)
Write(UInt64)Writes the text representation of an 8-byte unsigned integer to the text stream.
(Inherited from TextWriter)
Write(String, Object)Writes a formatted string to the text stream, using the same semantics as the Format(String, Object) method.
(Inherited from TextWriter)
Write(String, Object)Writes a formatted string to the text stream, using the same semantics as the Format(String, Object) method.
(Inherited from TextWriter)
Write(Char, Int32, Int32)Writes a subarray of characters to the text stream.
(Overrides TextWriterWrite(Char, Int32, Int32))
Write(String, Object, Object)Writes a formatted string to the text stream using the same semantics as the Format(String, Object, Object) method.
(Inherited from TextWriter)
Write(String, Object, Object, Object)Writes a formatted string to the text stream, using the same semantics as the Format(String, Object, Object, Object) method.
(Inherited from TextWriter)
WriteAsync(Char)Writes a character to the text stream asynchronously.
(Overrides TextWriterWriteAsync(Char))
WriteAsync(Char)Writes a character array to the text stream asynchronously.
(Inherited from TextWriter)
WriteAsync(String)Writes a string to the text stream asynchronously.
(Overrides TextWriterWriteAsync(String))
WriteAsync(ReadOnlyMemoryChar, CancellationToken)Asynchronously writes a character memory region to the text stream.
(Overrides TextWriterWriteAsync(ReadOnlyMemoryChar, CancellationToken))
WriteAsync(StringBuilder, CancellationToken)Asynchronously writes a string builder to the text stream.
(Inherited from TextWriter)
WriteAsync(Char, Int32, Int32)Writes a subarray of characters to the text stream asynchronously.
(Overrides TextWriterWriteAsync(Char, Int32, Int32))
WriteLineWrites a line terminator to the text stream.
(Inherited from TextWriter)
WriteLine(Boolean)Writes the text representation of a Boolean value to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(Char)Writes a character to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(Char)Writes an array of characters to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(Decimal)Writes the text representation of a decimal value to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(Double)Writes the text representation of a 8-byte floating-point value to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(Int32)Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(Int64)Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(Object)Writes the text representation of an object to the text stream, by calling the ToString method on that object, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(ReadOnlySpanChar)Writes the text representation of a character span to the text stream, followed by a line terminator.
(Overrides TextWriterWriteLine(ReadOnlySpanChar))
WriteLine(Single)Writes the text representation of a 4-byte floating-point value to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(String)Writes a string to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(StringBuilder)Writes the text representation of a string builder to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(UInt32)Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(UInt64)Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(String, Object)Writes a formatted string and a new line to the text stream, using the same semantics as the Format(String, Object) method.
(Inherited from TextWriter)
WriteLine(String, Object)Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, Object).
(Inherited from TextWriter)
WriteLine(Char, Int32, Int32)Writes a subarray of characters to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLine(String, Object, Object)Writes a formatted string and a new line to the text stream, using the same semantics as the Format(String, Object, Object) method.
(Inherited from TextWriter)
WriteLine(String, Object, Object, Object)Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, Object).
(Inherited from TextWriter)
WriteLineAsyncAsynchronously writes a line terminator to the text stream.
(Overrides TextWriterWriteLineAsync)
WriteLineAsync(Char)Asynchronously writes a character to the text stream, followed by a line terminator.
(Overrides TextWriterWriteLineAsync(Char))
WriteLineAsync(Char)Asynchronously writes an array of characters to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLineAsync(String)Asynchronously writes a string to the text stream, followed by a line terminator.
(Overrides TextWriterWriteLineAsync(String))
WriteLineAsync(ReadOnlyMemoryChar, CancellationToken)Asynchronously writes the text representation of a character memory region to the text stream, followed by a line terminator.
(Overrides TextWriterWriteLineAsync(ReadOnlyMemoryChar, CancellationToken))
WriteLineAsync(StringBuilder, CancellationToken)Asynchronously writes the text representation of a string builder to the text stream, followed by a line terminator.
(Inherited from TextWriter)
WriteLineAsync(Char, Int32, Int32)Asynchronously writes a subarray of characters to the text stream, followed by a line terminator.
(Overrides TextWriterWriteLineAsync(Char, Int32, Int32))

Fields

CoreNewLineStores the newline characters used for this TextWriter.
(Inherited from TextWriter)

Extension Methods

GetStandardStream Gets the StandardStream associated with a TextWriter instance, if that instance is for either the standard output or error stream.
(Defined by StandardStreamExtensions)

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also