xterm: implement RIS (ESC c) and DECSTR (CSI ! p)
ESC c was ignored, so `reset` could not recover a terminal left in the alternate screen with a hidden cursor, a scroll region or mouse modes. RIS now resets every mode, the attributes, character sets, tab stops, margins and saved cursors, returns to the main screen and clears it (scrollback is kept, as in xterm). DECSTR resets modes, attributes and margins without touching the text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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);
|
||||
});
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
+21
-1
@@ -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
|
||||
|
||||
+39
@@ -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
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user