Terminal class

Terminal is an interface to interact with command line applications. It translates escape sequences from the application into updates to the buffer and events such as onTitleChange or onBell, as well as translating user input into escape sequences that the application can understand.

The emulator is libghostty-vt. Feed it the program's output with write (or writeBytes) and send onOutput (or onOutputBytes) to the program's input:

final terminal = Terminal(
  maxLines: 10000,
  onOutput: (data) => pty.write(utf8.encode(data)),
  onResize: (width, height, pixelWidth, pixelHeight) =>
      pty.resize(height, width),
);
pty.output.listen((data) => terminal.write(utf8.decode(data)));
// ...
terminal.dispose(); // not in xterm: frees libghostty-vt's memory

Differences from xterm 4.0.0 are listed in COMPATIBILITY.md. The ones to know first: the terminal holds native memory, so call dispose when done (a finalizer is the safety net); buffer lines are snapshots (read them again after a write); and the EscapeHandler methods (setCursor, eraseDisplay, …) write escape sequences, so they throw StateError while the program's output stopped inside a sequence.

Implemented types

Constructors

Terminal({int maxLines = 1000, void onBell()?, void onTitleChange(String title)?, void onIconChange(String title)?, void onOutput(String title)?, void onResize(int width, int height, int pixelWidth, int pixelHeight)?, TerminalTargetPlatform platform = TerminalTargetPlatform.unknown, TerminalInputHandler? inputHandler = defaultInputHandler, TerminalMouseHandler? mouseHandler = defaultMouseHandler, void onPrivateOSC(String code, List<String> args)?, bool reflowEnabled = true, Set<int>? wordSeparators, void onOutputBytes(Uint8List data)?})
Creates an 80 × 24 terminal.

Properties

altBuffer → Buffer
The alternate buffer of full-screen programs. Readable only while it is active (see mainBuffer).
no setter
altBufferMouseScrollMode → bool
Whether the wheel sends arrow keys on the alternate screen (DEC mode 1007).
no setteroverride
appKeypadMode → bool
Whether the keypad is in application mode (DECNKM, DEC mode 66).
no setteroverride
autoWrapMode → bool
Whether autowrap (DECAWM, DEC mode 7) is set.
no setteroverride
bracketedPasteMode → bool
Whether bracketed paste (DEC mode 2004) is set.
no setteroverride
buffer → Buffer
Current active buffer of the terminal. This is initially mainBuffer and can be switched back and forth from altBuffer to mainBuffer when the underlying program requests it.
no setter
cursor → CursorStyle
The current text style (the SGR pen): a new copy on every read, so changing it does not change the terminal; use setCursorBold and the other methods for that.
no setteroverride
cursorBlinkMode → bool
Whether the cursor blinks (DEC mode 12).
no setteroverride
cursorKeysMode → bool
Whether application cursor keys (DECCKM, DEC mode 1) are set.
no setteroverride
cursorVisibleMode → bool
Whether the cursor is visible (DECTCEM, DEC mode 25).
no setteroverride
hashCode → int
The hash code for this object.
no setterinherited
inputHandler ↔ TerminalInputHandler?
The TerminalInputHandler used by this terminal. defaultInputHandler is used when not specified. User of this class can provide their own implementation of TerminalInputHandler or extend defaultInputHandler with CascadeInputHandler.
getter/setter pair
insertMode → bool
Whether insert mode (IRM, ANSI mode 4) is set.
no setteroverride
isUsingAltBuffer → bool
Whether the alternate buffer is active.
no setter
lineFeedMode → bool
Whether line feed mode (LNM, ANSI mode 20) is set.
no setteroverride
lines → IndexAwareCircularBuffer<BufferLine>
Lines of the active buffer.
no setter
listeners → Set<void Function()>
The registered listeners, called in insertion order.
finalinherited
mainBuffer → Buffer
The main buffer, with scrollback. Readable only while it is active: libghostty-vt 1.0.0 exposes only the active screen, so while the alternate buffer is in use it reads as blank lines (see Buffer).
no setter
maxLines → int
The number of lines that the scrollback buffer can hold. If the buffer exceeds this size, the lines at the top of the buffer will be removed.
final
mouseHandler ↔ TerminalMouseHandler?
The TerminalMouseHandler that turns mouseInput into reports for the program. defaultMouseHandler when not specified.
getter/setter pair
mouseMode → MouseMode
The mouse events the program asked for (DEC modes 9, 1000, 1002, 1003).
no setteroverride
mouseReportMode → MouseReportMode
The mouse report format the program asked for (DEC modes 1005, 1006, 1015; 1016, SGR with pixels, reads as MouseReportMode.sgr).
no setteroverride
onBell ↔ void Function()?
Function that is called when the program requests the terminal to ring the bell. If not set, the terminal will do nothing.
getter/setter pair
onIconChange ↔ void Function(String title)?
Function that is called when the program requests the terminal to change the icon of the window. icon is the name of the icon.
getter/setter pair
onOutput ↔ void Function(String title)?
Function that is called when the terminal emits data to the underlying program. This is typically caused by user inputs from textInput, keyInput, mouseInput, or paste.
getter/setter pair
onOutputBytes ↔ void Function(Uint8List data)?
Called with the exact bytes of each chunk the terminal sends to the program, before onOutput is called with the same chunk as text. Text produced in Dart (textInput, keyInput, …) arrives UTF-8 encoded. Not in xterm.
getter/setter pair
onPrivateOSC ↔ void Function(String code, List<String> args)?
The callback that is called when the terminal receives a unrecognized escape sequence.
getter/setter pair
onResize ↔ void Function(int width, int height, int pixelWidth, int pixelHeight)?
Function that is called when the dimensions of the terminal change.
getter/setter pair
onTitleChange ↔ void Function(String title)?
Function that is called when the program requests the terminal to change the title of the window to title.
getter/setter pair
originMode → bool
Whether origin mode (DECOM, DEC mode 6) is set.
no setteroverride
platform → TerminalTargetPlatform
Flag to toggle os specific behaviors.
final
reflowEnabled ↔ bool
Whether the terminal performs reflow when the viewport size changes or simply truncates lines. true by default.
getter/setter pairoverride-getter
reportFocusMode → bool
Whether focus events are reported (DEC mode 1004).
no setteroverride
reverseDisplayMode → bool
Whether reverse video (DECSCNM, DEC mode 5) is set.
no setteroverride
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
viewHeight → int
Number of rows in this terminal.
no setteroverride
viewWidth → int
Number of cells in a terminal row.
no setteroverride
wordSeparators → Set<int>?
Characters that break selection when double clicking. If not set, the Buffer.defaultWordSeparators will be used.
final

Methods

addListener(void listener()) → void
Registers listener.
inherited
backspaceReturn() → void
Moves the cursor one cell left (BS).
bell() → void
Calls onBell, as the program's BEL does.
carriageReturn() → void
Moves the cursor to the first column (CR).
charInput(int charCode, {bool alt = false, bool ctrl = false}) → bool
Similary to keyInput, but takes a character as input instead of a TerminalKey.
clearAllTabStops() → void
Clears every tab stop (TBC 3).
clearAltBuffer() → void
Erases the alternate buffer (ED 2 on it), whichever buffer is active.
clearTabStopUnderCursor() → void
Clears the tab stop at the cursor column (TBC 0).
cursorNextLine(int amount) → void
Moves the cursor amount rows down, to the first column.
cursorPrecedingLine(int amount) → void
Moves the cursor amount rows up, to the first column.
deleteChars(int amount) → void
Deletes amount cells at the cursor (DCH).
deleteLines(int amount) → void
Deletes amount lines at the cursor (DL).
designateCharset(int charset, int name) → void
Designates character set name (its final character, such as B or 0) as Gcharset (0–3).
dispose() → void
Frees libghostty-vt's terminal and every native object this terminal created (anchors included). Safe to call more than once. Not in xterm, whose terminal needs no cleanup; a forgotten terminal is freed by a finalizer some time after it is garbage collected.
eraseChars(int amount) → void
Erases amount cells from the cursor (ECH).
eraseDisplay() → void
Erases the whole screen (ED 2).
eraseDisplayAbove() → void
Erases from the start of the screen to the cursor (ED 1).
eraseDisplayBelow() → void
Erases from the cursor to the end of the screen (ED 0).
eraseLine() → void
Erases the line (EL 2).
eraseLineLeft() → void
Erases from the start of the line to the cursor (EL 1).
eraseLineRight() → void
Erases from the cursor to the end of the line (EL 0).
eraseScrollbackOnly() → void
Erases the scrollback (ED 3).
index() → void
Moves the cursor down, scrolling at the bottom margin (IND).
insertBlankChars(int amount) → void
Inserts amount blank cells at the cursor (ICH).
insertLines(int amount) → void
Inserts amount blank lines at the cursor (IL).
keyInput(TerminalKey key, {bool shift = false, bool alt = false, bool ctrl = false}) → bool
Sends a key event to the underlying program.
lineFeed() → void
Moves the cursor down one line (LF).
mouseInput(TerminalMouseButton button, TerminalMouseButtonState buttonState, CellOffset position) → bool
Handle a mouse event and return true if it was handled.
moveCursorX(int offset) → void
Moves the cursor offset columns right, or left if negative.
moveCursorY(int n) → void
Moves the cursor n rows down, or up if negative.
nextLine() → void
Moves the cursor to the first column of the next line (NEL).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Calls every listener.
inherited
paste(String text) → void
Similar to textInput, except that when the program tells the terminal that it supports bracketedPasteMode, the text is wrapped in escape sequences to indicate that it is a paste operation. Prefer this method over textInput when pasting text.
removeListener(void listener()) → void
Unregisters listener.
inherited
repeatPreviousCharacter(int count) → void
Repeats the last printed character count times (REP).
resetBackground() → void
Resets the background to the default colour (SGR 49).
resetCursorStyle() → void
Resets the text style (SGR 0).
resetForeground() → void
Resets the foreground to the default colour (SGR 39).
resize(int newWidth, int newHeight, [int? pixelWidth, int? pixelHeight]) → void
Resize the terminal screen. newWidth and newHeight should be greater than 0. Text reflow is currently not implemented and will be avaliable in the future.
restoreCursor() → void
Restores the cursor saved by saveCursor (DECRC).
reverseIndex() → void
Moves the cursor up, scrolling at the top margin (RI).
saveCursor() → void
Saves the cursor position and attributes (DECSC).
scrollDown(int amount) → void
Scrolls the scroll region down by amount lines (SD).
scrollUp(int amount) → void
Scrolls the scroll region up by amount lines (SU).
sendCursorPosition() → void
Sends a cursor position report to onOutput, with the 0-based position xterm's method sends.
sendOperatingStatus() → void
Sends the "terminal OK" status report to onOutput.
sendPrimaryDeviceAttributes() → void
Sends xterm's primary device attributes reply to onOutput.
sendSecondaryDeviceAttributes() → void
Sends xterm's secondary device attributes reply to onOutput.
sendSize() → void
Sends the text area size report to onOutput.
sendTertiaryDeviceAttributes() → void
Sends xterm's tertiary device attributes reply to onOutput.
setAltBufferMouseScrollMode(bool enabled) → void
Sets or resets wheel-as-arrows on the alternate screen (DEC mode 1007).
setAppKeypadMode(bool enabled) → void
Sets or resets the application keypad (DECNKM, DEC mode 66).
setAutoWrapMode(bool enabled) → void
Sets or resets autowrap (DECAWM).
setBackgroundColor16(int color) → void
Sets the background to named colour color (0–15).
setBackgroundColor256(int index) → void
Sets the background to palette colour index (0–255).
setBackgroundColorRgb(int r, int g, int b) → void
Sets the background to the RGB colour r, g, b.
setBracketedPasteMode(bool enabled) → void
Sets or resets bracketed paste (DEC mode 2004).
setColumnMode(bool enabled) → void
Does nothing, as in xterm (132-column mode, DECCOLM).
setCursor(int x, int y) → void
Moves the cursor to column x, row y (0-based; relative to the scroll region in origin mode) (CUP).
Sets blink (SGR 5).
setCursorBlinkMode(bool enabled) → void
Sets or resets cursor blinking (DEC mode 12).
setCursorBold() → void
Sets bold (SGR 1).
setCursorFaint() → void
Sets faint (SGR 2).
setCursorInverse() → void
Sets inverse video (SGR 7).
setCursorInvisible() → void
Sets invisible (SGR 8).
setCursorItalic() → void
Sets italic (SGR 3).
setCursorKeysMode(bool enabled) → void
Sets or resets application cursor keys (DECCKM).
setCursorStrikethrough() → void
Sets strikethrough (SGR 9).
setCursorUnderline() → void
Sets underline (SGR 4).
setCursorVisibleMode(bool enabled) → void
Shows or hides the cursor (DECTCEM).
setCursorX(int x) → void
Moves the cursor to column x (0-based) (CHA).
setCursorY(int y) → void
Moves the cursor to row y (0-based) (VPA).
setForegroundColor16(int color) → void
Sets the foreground to named colour color (0–15).
setForegroundColor256(int index) → void
Sets the foreground to palette colour index (0–255).
setForegroundColorRgb(int r, int g, int b) → void
Sets the foreground to the RGB colour r, g, b.
setIconName(String name) → void
Calls onIconChange with name, as xterm does.
setInsertMode(bool enabled) → void
Sets or resets insert mode (IRM).
setLineFeedMode(bool enabled) → void
Sets or resets line feed mode (LNM).
setMargins(int top, [int? bottom]) → void
Sets the scroll region to rows top to bottom (0-based, inclusive; the last row by default) (DECSTBM). The cursor stays where it is, as in xterm.
setMouseMode(MouseMode mode) → void
Sets the mouse events reported to the program (DEC modes 9, 1000, 1002, 1003).
setMouseReportMode(MouseReportMode mode) → void
Sets the mouse report format (DEC modes 1005, 1006, 1015).
setOriginMode(bool enabled) → void
Sets or resets origin mode (DECOM).
setReportFocusMode(bool enabled) → void
Sets or resets focus event reports (DEC mode 1004).
setReverseDisplayMode(bool enabled) → void
Sets or resets reverse video (DECSCNM).
setTapStop() → void
Sets a tab stop at the cursor column (HTS; sic, xterm's name).
setTitle(String name) → void
Sets the window title (OSC 2), which calls onTitleChange. Control characters in name are dropped.
setUnknownDecMode(int mode, bool enabled) → void
Does nothing, as in xterm (it handles unknown DEC modes).
setUnknownMode(int mode, bool enabled) → void
Does nothing, as in xterm (it handles unknown ANSI modes).
shiftIn() → void
Switches to the G0 character set (SI).
shiftOut() → void
Switches to the G1 character set (SO).
tab() → void
Moves the cursor to the next tab stop (HT).
textInput(String text) → void
Sends regular text input to the underlying program.
toString() → String
A string representation of this object.
override
unknownCSI(int finalByte) → void
Does nothing, as in xterm (it handles unknown CSI sequences).
unknownOSC(String code, List<String> args) → void
Calls onPrivateOSC with code and args, as xterm does.
unknownSBC(int char) → void
Does nothing, as in xterm (it handles unknown control characters).
unkownEscape(int char) → void
Does nothing, as in xterm (sic, xterm's name).
Unsets blink (SGR 25).
unsetCursorBold() → void
Unsets bold only (SGR 22, then faint again if it was set).
unsetCursorFaint() → void
Unsets faint only (SGR 22, then bold again if it was set).
unsetCursorInverse() → void
Unsets inverse video (SGR 27).
unsetCursorInvisible() → void
Unsets invisible (SGR 28).
unsetCursorItalic() → void
Unsets italic (SGR 23).
unsetCursorStrikethrough() → void
Unsets strikethrough (SGR 29).
unsetCursorUnderline() → void
Unsets underline (SGR 24).
unsupportedStyle(int param) → void
Does nothing, as in xterm (it handles unsupported SGR parameters).
useAltBuffer() → void
Switches to the alternate buffer, without clearing it or saving the cursor (DEC mode 47).
useMainBuffer() → void
Switches back to the main buffer (DEC mode 47).
write(String data) → void
Writes the data from the underlying program to the terminal. Calling this updates the states of the terminal and emits events such as onBell or onTitleChange when the escape sequences in data request it.
writeBytes(Uint8List data) → void
Writes data, bytes from the underlying program, to the terminal: as write, without a round trip through a String. Not in xterm.
writeChar(int char) → void
Writes the character char at the cursor. A control character is dropped (xterm stores it as a glyph).

Operators

operator ==(Object other) → bool
The equality operator.
inherited