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:
2026-11-25 23:27:50 +00:00
parent 30b32921fe
commit 31d00e057c
7 changed files with 174 additions and 1 deletions
@@ -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);
});
}