TerminalInterpreter class Terminal
Terminal ANSI Interpreter and Buffer Manager
Interprets ANSI escape sequences from terminal input and maintains a virtual terminal buffer state representing characters, colors, and font styles for each cell on the terminal screen.
This class processes input strings containing normal characters and ANSI control sequences, updating the internal buffer accordingly.
The buffer is a 2D grid (bufferState
) of CellState, each cell
storing the character and its styling attributes.
Maintains cursor position (currentLine
, currentColumn
) and supports
ANSI sequences for text styling, cursor movement, clearing, and colors.
Example
final interpreter = TerminalInterpreter(24, 80);
interpreter.processInput('Hello \x1B[31mRed\x1B[0m World!');
print(interpreter.charactersToString());
See Also
- ANSI escape code Wikipedia
- AnsiColorType for color representations.
- FontStyle for supported font styles.
Constructors
- TerminalInterpreter.new(int lines, int columns)
-
Creates a terminal interpreter with given
lines
andcolumns
size.
Properties
- ansiBuffer → StringBuffer
-
Buffer accumulating characters of an ongoing ANSI escape sequence.
final
-
bufferState
→ List<
List< CellState> > -
2D buffer storing the state of each terminal cell.
final
- columns → int
-
Number of columns in the terminal buffer.
final
- currentColumn ↔ int
-
Current cursor column, zero-based.
getter/setter pair
- currentLine ↔ int
-
Current cursor line (row), zero-based.
getter/setter pair
- hashCode → int
-
The hash code for this object.
no setterinherited
- lines → int
-
Number of lines (rows) in the terminal buffer.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- seenEscapeCode ↔ bool
-
Tracks if an escape code has been seen (e.g., '\x1B').
getter/setter pair
Methods
-
bgColorsToString(
) → String - Returns the background color matrix as a formatted string.
-
charactersToString(
) → String - Returns the character content of the buffer as a multi-line string.
-
fgColorsToString(
) → String - Returns the foreground color matrix as a formatted string.
-
getBgMatrix(
) → List< List< String> > - Returns a 2D list of background colors as printable strings.
-
getCharactersMatrix(
) → List< List< String> > - Returns a 2D list of characters from the buffer.
-
getFgMatrix(
) → List< List< String> > - Returns a 2D list of foreground colors as printable strings.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
processInput(
String data) → void - Processes an input string containing text and ANSI sequences.
-
toString(
) → String -
A string representation of this object.
inherited
-
writeToBufferState(
String char) → void - Writes a single character and current style attributes at the cursor.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- backMovementTerminatingCharacter → const String
- Terminating character for cursor back movement sequences (CUB - Cursor Back)
- clearLineTerminatingCharacter → const String
- Terminating character for line clearing sequences (EL - Erase in Line)
- cursorHomeTerminatingCharacter → const String
- Terminating character for cursor home position sequences (CUP - Cursor Position)
- disableTerminatingCharacter → const String
- Terminating character for disable feature sequences (RM - Reset Mode)
- downMovementTerminatingCharacter → const String
- Terminating character for cursor down movement sequences (CUD - Cursor Down)
- enableTerminatingCharacter → const String
- Terminating character for enable feature sequences (SM - Set Mode)
- forwardMovementTerminatingCharacter → const String
- Terminating character for cursor forward movement sequences (CUF - Cursor Forward)
- requestCursorTerminatingCharacter → const String
- Terminating character for cursor position request sequences (DSR - Device Status Report)
- respondCursorTerminatingCharacter → const String
- Terminating character for cursor position response sequences (CPR - Cursor Position Report)
- styleTerminatingCharacter → const String
- ANSI control sequence terminating characters. Terminating character for text style sequences (SGR - Select Graphic Rendition)
-
terminatingCharacters
→ const Set<
String> - Set of terminating characters for ANSI sequences.
- upMovementTerminatingCharacter → const String
- Terminating character for cursor up movement sequences (CUU - Cursor Up)