| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- import 'dart:collection';
- import 'dart:math' show max, min;
- import 'package:xterm/buffer/buffer.dart';
- import 'package:xterm/buffer/buffer_line.dart';
- import 'package:xterm/mouse/selection.dart';
- import 'package:xterm/input/keys.dart';
- import 'package:xterm/input/keytab/keytab.dart';
- import 'package:xterm/input/keytab/keytab_escape.dart';
- import 'package:xterm/input/keytab/keytab_record.dart';
- import 'package:xterm/mouse/mouse_mode.dart';
- import 'package:xterm/terminal/ansi.dart';
- import 'package:xterm/terminal/cursor.dart';
- import 'package:xterm/terminal/platform.dart';
- import 'package:xterm/terminal/sbc.dart';
- import 'package:xterm/terminal/tabs.dart';
- import 'package:xterm/theme/terminal_color.dart';
- import 'package:xterm/theme/terminal_theme.dart';
- import 'package:xterm/theme/terminal_themes.dart';
- import 'package:xterm/util/debug_handler.dart';
- import 'package:xterm/util/observable.dart';
- typedef TerminalInputHandler = void Function(String);
- typedef BellHandler = void Function();
- typedef TitleChangeHandler = void Function(String);
- typedef IconChangeHandler = void Function(String);
- void _defaultInputHandler(String _) {}
- void _defaultBellHandler() {}
- void _defaultTitleHandler(String _) {}
- void _defaultIconHandler(String _) {}
- class Terminal with Observable {
- Terminal({
- this.onInput = _defaultInputHandler,
- this.onBell = _defaultBellHandler,
- this.onTitleChange = _defaultTitleHandler,
- this.onIconChange = _defaultIconHandler,
- this.platform = PlatformBehaviors.unix,
- this.theme = TerminalThemes.defaultTheme,
- required int maxLines,
- }) : _maxLines = maxLines {
- _mainBuffer = Buffer(this);
- _altBuffer = Buffer(this);
- _buffer = _mainBuffer;
- cursor = Cursor(
- fg: theme.foreground,
- bg: TerminalColor.transparent, // transparent
- flags: 0x00, // no flags
- );
- tabs.reset();
- }
- bool _dirty = false;
- bool get dirty {
- if (_dirty) {
- _dirty = false;
- return true;
- } else {
- return false;
- }
- }
- int _maxLines;
- int get maxLines {
- return max(viewHeight, _maxLines);
- }
- int _viewWidth = 80;
- int _viewHeight = 25;
- int get viewWidth => _viewWidth;
- int get viewHeight => _viewHeight;
- int get visibleHeight => min(_viewHeight, buffer.height);
- int get invisibleHeight => buffer.height - visibleHeight;
- /// ### Insert/Replace Mode (IRM)
- ///
- /// The terminal displays received characters at the cursor position.
- /// Insert/Replace mode determines how the terminal adds characters to the
- /// screen. Insert mode displays the new character and moves previously
- /// displayed characters to the right. Replace mode adds characters by
- /// replacing the character at the cursor position.
- ///
- /// You can set or reset insert/replace mode as follows.
- bool _replaceMode = true;
- bool _screenMode = false; // DECSCNM (black on white background)
- bool _autoWrapMode = true;
- bool get autoWrapMode => _autoWrapMode;
- /// ### DECOM – Origin Mode (DEC Private)
- ///
- /// This is a private parameter applicable to set mode (SM) and reset mode
- /// (RM) control sequences. The reset state causes the origin to be at the
- /// upper-left character position on the screen. Line and column numbers are,
- /// therefore, independent of current margin settings. The cursor may be
- /// positioned outside the margins with a cursor position (CUP) or horizontal
- /// and vertical position (HVP) control.
- ///
- /// The set state causes the origin to be at the upper-left character position
- /// within the margins. Line and column numbers are therefore relative to the
- /// current margin settings. The cursor is not allowed to be positioned
- /// outside the margins.
- ///
- /// The cursor is moved to the new home position when this mode is set or
- /// reset.
- ///
- /// Lines and columns are numbered consecutively, with the origin being line
- /// 1, column 1.
- bool get originMode => _originMode;
- bool _originMode = false;
- /// ### LNM – Line Feed/New Line Mode
- ///
- /// This is a parameter applicable to set mode (SM) and reset mode (RM)
- /// control sequences. The reset state causes the interpretation of the line
- /// feed (LF), defined in ANSI Standard X3.4-1977, to imply only vertical
- /// movement of the active position and causes the RETURN key (CR) to send the
- /// single code CR. The set state causes the LF to imply movement to the first
- /// position of the following line and causes the RETURN key to send the two
- /// codes (CR, LF). This is the New Line (NL) option.
- ///
- /// This mode does not affect the index (IND), or next line (NEL) format
- /// effectors.
- bool get lineFeedMode => _lineFeedMode;
- bool _lineFeedMode = true;
- /// See: [lineFeedMode]
- bool get newLineMode => !_lineFeedMode;
- /// ### Bracketed Paste Mode
- ///
- /// When bracketed paste mode is set, pasted text is bracketed with control
- /// sequences so that the program can differentiate pasted text from typed-in
- /// text. When bracketed paste mode is set, the program will receive: `ESC
- /// [200 ~`, followed by the pasted text, followed by `ESC [ 201 ~`.
- bool get bracketedPasteMode => _bracketedPasteMode;
- bool _bracketedPasteMode = false;
- bool _showCursor = true;
- bool get showCursor => _showCursor;
- /// DECCKM – Cursor Keys Mode (DEC Private)
- ///
- /// This is a private parameter applicable to set mode (SM) and reset mode
- /// (RM) control sequences. This mode is only effective when the terminal is
- /// in keypad application mode (see DECKPAM) and the ANSI/VT52 mode (DECANM)
- /// is set (see DECANM). Under these conditions, if the cursor key mode is
- /// reset, the four cursor function keys will send ANSI cursor control
- /// commands. If cursor key mode is set, the four cursor function keys will
- /// send application functions.
- bool get applicationCursorKeys => _applicationCursorKeys;
- bool _applicationCursorKeys = false;
- bool _blinkingCursor = true;
- bool get blinkingCursor => _blinkingCursor;
- late Buffer _buffer;
- late Buffer _mainBuffer;
- late Buffer _altBuffer;
- /// Queue of input characters. addLast() to add, removeFirst() to consume.
- final _queue = ListQueue<int>(81920);
- bool _slowMotion = false;
- bool get slowMotion => _slowMotion;
- MouseMode _mouseMode = MouseMode.none;
- MouseMode get mouseMode => _mouseMode;
- final TerminalTheme theme;
- // final cellAttr = CellAttrTemplate();
- late final Cursor cursor;
- final keytab = Keytab.defaultKeytab();
- final selection = Selection();
- final tabs = Tabs();
- final debug = DebugHandler();
- final TerminalInputHandler onInput;
- final BellHandler onBell;
- final TitleChangeHandler onTitleChange;
- final IconChangeHandler onIconChange;
- final PlatformBehavior platform;
- Buffer get buffer {
- return _buffer;
- }
- int get cursorX => buffer.cursorX;
- int get cursorY => buffer.cursorY;
- int get scrollOffset => buffer.scrollOffsetFromBottom;
- /// Writes data to the terminal. Terminal sequences and special characters are
- /// interpreted.
- ///
- /// See also: [Buffer.write]
- void write(String text) {
- _queue.addAll(text.runes);
- _processInput();
- refresh();
- }
- /// Writes data to the terminal. Special characters are interpreted.
- ///
- /// See also: [Buffer.writeChar]
- void writeChar(int codePoint) {
- _queue.addLast(codePoint);
- _processInput();
- refresh();
- }
- List<BufferLine> getVisibleLines() {
- return _buffer.getVisibleLines();
- }
- void _processInput() {
- while (_queue.isNotEmpty) {
- // if (_slowMotion) {
- // await Future.delayed(Duration(milliseconds: 100));
- // }
- const esc = 0x1b;
- final char = _queue.removeFirst();
- if (char == esc) {
- final finished = ansiHandler(_queue, this);
- // Terminal sequence in the queue is not completed, and no charater is
- // consumed.
- if (!finished) {
- _queue.addFirst(esc);
- break;
- }
- continue;
- }
- _processChar(char);
- }
- }
- void _processChar(int codePoint) {
- // If the character doesn't have special effect. Write it directly to the
- // buffer.
- if (codePoint > sbcMaxCodePoint) {
- debug.onChar(codePoint);
- _buffer.writeChar(codePoint);
- return;
- }
- // The character may have special effect.
- final sbcHandler = sbcHandlers[codePoint];
- if (sbcHandler != null) {
- debug.onSbc(codePoint);
- sbcHandler(codePoint, this);
- }
- }
- void refresh() {
- _dirty = true;
- notifyListeners();
- }
- void setSlowMotion(bool enabled) {
- _slowMotion = enabled;
- }
- void setOriginMode(bool enabled) {
- _originMode = enabled;
- buffer.setPosition(0, 0);
- }
- void setScreenMode(bool enabled) {
- _screenMode = true;
- }
- void setApplicationCursorKeys(bool enabled) {
- _applicationCursorKeys = enabled;
- }
- void setShowCursor(bool showCursor) {
- _showCursor = showCursor;
- }
- void setBlinkingCursor(bool enabled) {
- _blinkingCursor = enabled;
- }
- void setAutoWrapMode(bool enabled) {
- _autoWrapMode = enabled;
- }
- void setBracketedPasteMode(bool enabled) {
- _bracketedPasteMode = enabled;
- }
- void setInsertMode() {
- _replaceMode = false;
- }
- void setReplaceMode() {
- _replaceMode = true;
- }
- void setNewLineMode() {
- _lineFeedMode = false;
- }
- void setLineFeedMode() {
- _lineFeedMode = true;
- }
- void setMouseMode(MouseMode mode) {
- _mouseMode = mode;
- }
- void useMainBuffer() {
- _buffer = _mainBuffer;
- }
- void useAltBuffer() {
- _buffer = _altBuffer;
- }
- bool isUsingMainBuffer() {
- return _buffer == _mainBuffer;
- }
- bool isUsingAltBuffer() {
- return _buffer == _altBuffer;
- }
- /// 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.
- void resize(int newWidth, int newHeight) {
- newWidth = max(newWidth, 1);
- newHeight = max(newHeight, 1);
- final oldWidth = _viewWidth;
- final oldHeight = _viewHeight;
- _viewWidth = newWidth;
- _viewHeight = newHeight;
- buffer.resize(oldWidth, oldHeight, newWidth, newHeight);
- // maybe reflow should happen here.
- if (buffer == _altBuffer) {
- buffer.clearScrollback();
- }
- buffer.resetVerticalMargins();
- }
- void keyInput(
- TerminalKey key, {
- bool ctrl = false,
- bool alt = false,
- bool shift = false,
- bool mac = false,
- // bool meta,
- }) {
- debug.onMsg(key);
- for (var record in keytab.records) {
- if (record.key != key) {
- continue;
- }
- if (record.ctrl != null && record.ctrl != ctrl) {
- continue;
- }
- if (record.shift != null && record.shift != shift) {
- continue;
- }
- if (record.alt != null && record.alt != alt) {
- continue;
- }
- if (record.anyModifier == true &&
- (ctrl != true && alt != true && shift != true)) {
- continue;
- }
- if (record.anyModifier == false &&
- !(ctrl != true && alt != true && shift != true)) {
- continue;
- }
- if (record.appScreen != null && record.appScreen != isUsingAltBuffer()) {
- continue;
- }
- if (record.newLine != null && record.newLine != newLineMode) {
- continue;
- }
- if (record.appCursorKeys != null &&
- record.appCursorKeys != applicationCursorKeys) {
- continue;
- }
- if (record.mac != null && record.mac != mac) {
- continue;
- }
- // TODO: support VT52
- if (record.ansi == false) {
- continue;
- }
- if (record.action.type == KeytabActionType.input) {
- debug.onMsg('input: ${record.action.value}');
- final input = keytabUnescape(record.action.value);
- onInput(input);
- return;
- }
- }
- if (ctrl) {
- if (key.index >= TerminalKey.keyA.index &&
- key.index <= TerminalKey.keyZ.index) {
- final input = key.index - TerminalKey.keyA.index + 1;
- onInput(String.fromCharCode(input));
- return;
- }
- }
- if (alt) {
- if (key.index >= TerminalKey.keyA.index &&
- key.index <= TerminalKey.keyZ.index) {
- final input = [0x1b, key.index - TerminalKey.keyA.index + 65];
- onInput(String.fromCharCodes(input));
- return;
- }
- }
- }
- String? getSelectedText() {
- if (selection.isEmpty) {
- return null;
- }
- final builder = StringBuffer();
- for (var row = selection.start!.y; row <= selection.end!.y; row++) {
- if (row >= buffer.height) {
- break;
- }
- final line = buffer.lines[row];
- var xStart = 0;
- var xEnd = viewWidth - 1;
- if (row == selection.start!.y) {
- xStart = selection.start!.x;
- } else if (!line.isWrapped) {
- builder.write("\n");
- }
- if (row == selection.end!.y) {
- xEnd = selection.end!.x;
- }
- for (var col = xStart; col <= xEnd; col++) {
- // if (col >= line.length) {
- // break;
- // }
- if (line.cellGetWidth(col) == 0) {
- continue;
- }
- var char = line.cellGetContent(col);
- if (char == 0x00) {
- const blank = 32;
- char = blank;
- }
- builder.writeCharCode(char);
- }
- }
- return builder.toString();
- }
- void paste(String data) {
- if (bracketedPasteMode) {
- data = '\x1b[200~$data\x1b[201~';
- }
- onInput(data);
- }
- void selectWord(int x, int y) {}
- int get _tabIndexFromCursor {
- var index = buffer.cursorX;
- if (buffer.cursorX == viewWidth) {
- index = 0;
- }
- return index;
- }
- void tabSetAtCursor() {
- tabs.setAt(_tabIndexFromCursor);
- }
- void tabClearAtCursor() {
- tabs.clearAt(_tabIndexFromCursor);
- }
- void tab() {
- while (buffer.cursorX < viewWidth) {
- buffer.write(' ');
- if (tabs.isSetAt(buffer.cursorX)) {
- break;
- }
- }
- }
- }
|