Print class final Introduction
A sequence that is a literal text value that contains no escape codes.
While not strictly necessary for output, this type has two main purposes:
- A sealed subtype represntation of Sequence for decoding.
- A safe way to represent user-provided text that should not contain escape codes, with optional validation.
See also: AnsiDecoder for decoding ANSI escape codes from a string.
Output
Print writes the text value to the output at the current cursor position.
Assuming a starting cursor position of █
:
█
Print writes the text value to the output, advancing the cursor to the current position after the text:
sink.writeAnsi(Print('Hello, World!'));
Resulting in:
Hello, World!█
Note that String.length is not a reliable way to determine the cursor
position after writing a Print sequence, as it does not account for
extended characters. See package:characters
for working with Unicode
characters and grapheme clusters in a predictable way, including measuring
the width of text.
Example
final sequence = Print('Hello, World!');
final buffer = StringBuffer();
sequence.writeAnsiString(buffer);
print(buffer.toString()); // 'Hello, World!'
By default, Print will escape any escape or control characters:
final sequence = Print('\x1BHello\nWorld!');
final buffer = StringBuffer();
sequence.writeAnsiString(buffer);
print(buffer.toString()); // '\\x1BHello\\nWorld!'
However, there are options to either:
- Allow ASCII control characters (e.g.
\x07
for bell) withallowAscii
. - Check for invalid characters and throw instead with Print.checkInvalid.
- Inheritance
- Available Extensions
Constructors
- Print(String text, {bool allowAscii = false})
-
Creates a Print from the given
text
with control characters escaped.factory - Print.checkInvalid(String text, {bool allowAscii = false})
-
Creates a Print from the given
text
value.factory - Print.fromUnchecked(String text)
-
Creates a Print from the given text value.
const
Properties
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
Returns a textual representation of this sequence for debugging.
override
-
writeAnsiString(
StringSink out) → void -
Write as a UTF-16 ANSI escaped string to the given StringSink.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override