diff --git a/app/test/third_party_xterm/reset_test.dart b/app/test/third_party_xterm/reset_test.dart new file mode 100644 index 00000000..d6839067 --- /dev/null +++ b/app/test/third_party_xterm/reset_test.dart @@ -0,0 +1,75 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:xterm/xterm.dart'; + +import 'xterm_fixture.dart'; + +void main() { + /// Puts the terminal in the state a crashed full-screen program leaves. + FixtureTerminal messedUp() { + return FixtureTerminal()..feed( + 'shell\r\n' + '\x1b[?1049h' // alternate screen + '\x1b[2;4r' // scroll region + '\x1b[?25l' // cursor hidden + '\x1b[?1h\x1b=' // application cursor keys and keypad + '\x1b[?7l\x1b[4h\x1b[?6h' // no autowrap, insert, origin + '\x1b[?1002h\x1b[?1006h\x1b[?2004h\x1b[?1004h' // mouse, paste, focus + '\x1b(0' // DEC graphics in G0 + '\x1b[1;7;38;5;9m' // attributes + '\x1b[3;3Hjunk', + ); + } + + test('RIS (ESC c) recovers every mode, the screen and the cursor', () { + final t = messedUp()..feed('\x1bc'); + final term = t.terminal; + + expect(term.isUsingAltBuffer, isFalse); + expect(term.cursorVisibleMode, isTrue); + expect(term.cursorKeysMode, isFalse); + expect(term.appKeypadMode, isFalse); + expect(term.autoWrapMode, isTrue); + expect(term.insertMode, isFalse); + expect(term.originMode, isFalse); + expect(term.mouseMode, MouseMode.none); + expect(term.mouseReportMode, MouseReportMode.normal); + expect(term.bracketedPasteMode, isFalse); + expect(term.reportFocusMode, isFalse); + expect(t.buffer.marginTop, 0); + expect(t.buffer.marginBottom, 4); + expect(t.buffer.cursorX, 0); + expect(t.buffer.cursorY, 0); + for (var y = 0; y < 5; y++) { + expect(t.row(y), '', reason: 'row $y is cleared'); + } + + // Plain ASCII and plain attributes again. + t.feed('q'); + expect(t.charAt(0, 0), 'q'); + expect(t.cell(0, 0).flags, 0); + expect(t.cell(0, 0).foreground, 0); + }); + + test('RIS keeps the main screen scrollback', () { + final t = FixtureTerminal(); + for (var i = 0; i < 8; i++) { + t.feed('line $i\r\n'); + } + final scrollBack = t.buffer.scrollBack; + t.feed('\x1bc'); + expect(t.buffer.scrollBack, scrollBack); + expect(t.buffer.lines[0].getText().trimRight(), 'line 0'); + }); + + test('DECSTR (CSI ! p) resets modes and attributes but keeps the text', () { + final t = FixtureTerminal() + ..feed('keep\x1b[?25l\x1b[?1h\x1b[2;4r\x1b[4h\x1b[1m\x1b[!p'); + expect(t.row(0), 'keep'); + expect(t.terminal.cursorVisibleMode, isTrue); + expect(t.terminal.cursorKeysMode, isFalse); + expect(t.terminal.insertMode, isFalse); + expect(t.terminal.cursor.attrs, 0); + expect(t.buffer.marginTop, 0); + expect(t.buffer.marginBottom, 4); + }); +} diff --git a/app/third_party/xterm/lib/src/core/buffer/buffer.dart b/app/third_party/xterm/lib/src/core/buffer/buffer.dart index 10789382..44e7df0b 100644 --- a/app/third_party/xterm/lib/src/core/buffer/buffer.dart +++ b/app/third_party/xterm/lib/src/core/buffer/buffer.dart @@ -338,6 +338,19 @@ class Buffer { charset.restore(); } + /// Resets the cursor, the saved cursor, the character sets and the + /// margins to their power-on state (for RIS and DECSTR). The contents are + /// left alone. + void resetState() { + _cursorX = 0; + _cursorY = 0; + _savedCursorX = 0; + _savedCursorY = 0; + _savedCursorStyle.reset(); + charset.reset(); + resetVerticalMargins(); + } + /// Sets the vertical scrolling margin to [top] and [bottom]. /// Both values must be between 0 and [viewHeight] - 1. void setVerticalMargins(int top, int bottom) { diff --git a/app/third_party/xterm/lib/src/core/charset.dart b/app/third_party/xterm/lib/src/core/charset.dart index 3659810a..d062368c 100644 --- a/app/third_party/xterm/lib/src/core/charset.dart +++ b/app/third_party/xterm/lib/src/core/charset.dart @@ -40,6 +40,15 @@ class Charset { _savedIndex = _currentIndex; } + /// Back to G0 selected and no designations (ASCII), as after power-on. + void reset() { + _charsetMap = {}; + _currentIndex = 0; + _savedCharsetMap = {}; + _savedIndex = 0; + _updateCache(); + } + void restore() { _charsetMap = _savedCharsetMap; _currentIndex = _savedIndex; diff --git a/app/third_party/xterm/lib/src/core/escape/handler.dart b/app/third_party/xterm/lib/src/core/escape/handler.dart index c13a4b85..50d1f82b 100644 --- a/app/third_party/xterm/lib/src/core/escape/handler.dart +++ b/app/third_party/xterm/lib/src/core/escape/handler.dart @@ -39,6 +39,13 @@ abstract class EscapeHandler { void unkownEscape(int char); + /// `ESC c` (RIS): everything back to the power-on state, screen cleared. + void resetTerminal(); + + /// `CSI ! p` (DECSTR): modes, attributes and margins reset; the screen + /// contents stay. + void softResetTerminal(); + /* DCS */ /// `DCS $ q Pt ST` (DECRQSS): report the setting named by [setting], such diff --git a/app/third_party/xterm/lib/src/core/escape/parser.dart b/app/third_party/xterm/lib/src/core/escape/parser.dart index 15d09ef8..2b82bf3e 100644 --- a/app/third_party/xterm/lib/src/core/escape/parser.dart +++ b/app/third_party/xterm/lib/src/core/escape/parser.dart @@ -123,6 +123,7 @@ class EscapeParser { // '+'.charCode: _voidHandler(1), // TODO: G3 (vt220) '>'.charCode: _escHandleResetAppKeypadMode, // TODO: Normal Keypad '='.charCode: _escHandleSetAppKeypadMode, // TODO: Application Keypad + 'c'.charCode: _escHandleReset, 'P'.charCode: () => _beginControlString(Ascii.P), // DCS 'X'.charCode: () => _beginControlString(Ascii.X), // SOS '^'.charCode: () => _beginControlString(Ascii.caret), // PM @@ -194,6 +195,14 @@ class EscapeParser { } } + /// `ESC c` Reset to Initial State (RIS) + /// + /// https://terminalguide.namepad.de/seq/a_esc_sc/ + bool _escHandleReset() { + handler.resetTerminal(); + return true; + } + /// `ESC 7` Save Cursor (DECSC) /// /// https://terminalguide.namepad.de/seq/a_esc_a7/ @@ -315,7 +324,18 @@ class EscapeParser { /// Dispatches a CSI that carries intermediate bytes. void _csiHandleWithIntermediates() { - handler.unknownCSI(_csi.finalByte); + final finalByte = _csi.finalByte; + final intermediates = _csi.intermediates; + + // `CSI ! p` Soft Terminal Reset (DECSTR) + if (intermediates == Ascii.exclamationMark && + finalByte == Ascii.p && + _csi.prefix == null) { + handler.softResetTerminal(); + return; + } + + handler.unknownCSI(finalByte); } /// The last parsed [_Csi]. This is a mutable singletion by design to reduce diff --git a/app/third_party/xterm/lib/src/terminal.dart b/app/third_party/xterm/lib/src/terminal.dart index 523776e4..b3d2426e 100644 --- a/app/third_party/xterm/lib/src/terminal.dart +++ b/app/third_party/xterm/lib/src/terminal.dart @@ -482,6 +482,45 @@ class Terminal with Observable implements TerminalState, EscapeHandler { // no-op } + @override + void resetTerminal() { + _resetModes(); + _mouseMode = MouseMode.none; + _mouseReportMode = MouseReportMode.normal; + _cursorBlinkMode = false; + _lineFeedMode = false; + _reverseDisplayMode = false; + _reportFocusMode = false; + _altBufferMouseScrollMode = false; + _bracketedPasteMode = false; + _precedingCodepoint = 0; + _tabStops.reset(); + + _buffer = _mainBuffer; + _altBuffer.clear(); + _altBuffer.resetState(); + _mainBuffer.resetState(); + // The screen is cleared; scrollback above it is kept, as in xterm. + _mainBuffer.eraseDisplay(); + } + + @override + void softResetTerminal() { + _resetModes(); + _buffer.resetState(); + } + + /// The state shared by RIS and DECSTR. + void _resetModes() { + _cursorStyle.reset(); + _insertMode = false; + _originMode = false; + _autoWrapMode = true; + _cursorKeysMode = false; + _appKeypadMode = false; + _cursorVisibleMode = true; + } + /* DCS */ @override diff --git a/app/third_party/xterm/lib/src/utils/debugger.dart b/app/third_party/xterm/lib/src/utils/debugger.dart index 04323e6e..4a15f905 100644 --- a/app/third_party/xterm/lib/src/utils/debugger.dart +++ b/app/third_party/xterm/lib/src/utils/debugger.dart @@ -471,6 +471,16 @@ class _TerminalDebuggerHandler implements EscapeHandler { onCommand('setCursorUnderline'); } + @override + void resetTerminal() { + onCommand('resetTerminal'); + } + + @override + void softResetTerminal() { + onCommand('softResetTerminal'); + } + @override void cursorForwardTab(int amount) { onCommand('cursorForwardTab $amount');