xterm: add SCOSC/SCORC, CHT, CBT, HPA, HPR and VPR; fix HTS

CSI s / CSI u save and restore the cursor (without parameters; prefixed
forms such as the kitty keyboard query CSI ? u are left alone). CSI I and
CSI Z move by tab stops, CSI ` sets the column, CSI a and CSI e move
relative. ESC H (HTS) set no tab stop because it called isSetAt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 23:26:18 +00:00
parent 7592aa1a82
commit 326e7710d4
7 changed files with 164 additions and 9 deletions
@@ -0,0 +1,60 @@
import 'package:flutter_test/flutter_test.dart';
import 'xterm_fixture.dart';
void main() {
test('CSI s / CSI u save and restore the cursor (SCOSC / SCORC)', () {
final t = FixtureTerminal()..feed('\x1b[2;3H\x1b[s\x1b[5;9Hx\x1b[uy');
expect(t.charAt(2, 1), 'y');
expect(t.charAt(8, 4), 'x');
expect(t.buffer.cursorX, 3);
expect(t.buffer.cursorY, 1);
});
test('CSI ? u (kitty keyboard query) does not restore the cursor', () {
final t = FixtureTerminal()..feed('\x1b[s\x1b[3;3H\x1b[?u');
expect(t.buffer.cursorX, 2);
expect(t.buffer.cursorY, 2);
expect(t.takeReplies(), '');
});
test('CHT moves forward by tab stops and stops at the last column', () {
final t = FixtureTerminal()..feed('\x1b[I');
expect(t.buffer.cursorX, 8);
t.feed('\x1b[2I');
expect(t.buffer.cursorX, 19);
});
test('CBT moves back by tab stops and stops at column 0', () {
final t = FixtureTerminal()..feed('\x1b[1;19H\x1b[Z');
expect(t.buffer.cursorX, 16);
t.feed('\x1b[5Z');
expect(t.buffer.cursorX, 0);
});
test('HTS sets a tab stop that HT and CBT use', () {
final t = FixtureTerminal()..feed('\x1b[1;4H\x1bH\r\t');
expect(t.buffer.cursorX, 3);
t.feed('\x1b[1;7H\x1b[Z');
expect(t.buffer.cursorX, 3);
});
test('HPA, HPR and VPR', () {
final t = FixtureTerminal()..feed('\x1b[7`');
expect(t.buffer.cursorX, 6);
t.feed('\x1b[3a');
expect(t.buffer.cursorX, 9);
t.feed('\x1b[a');
expect(t.buffer.cursorX, 10);
t.feed('\x1b[2e');
expect(t.buffer.cursorY, 2);
t.feed('\x1b[e');
expect(t.buffer.cursorY, 3);
});
test('zero counts mean one', () {
final t = FixtureTerminal()..feed('\x1b[0C\x1b[0B');
expect(t.buffer.cursorX, 1);
expect(t.buffer.cursorY, 1);
});
}
@@ -26,6 +26,12 @@ class FixtureTerminal {
return data;
}
/// The character at column [x] of screen row [y], '' for an empty cell.
String charAt(int x, int y) {
final code = cell(x, y).content & CellContent.codepointMask;
return code == 0 ? '' : String.fromCharCode(code);
}
/// The replies sent so far, joined, and then forgotten.
String takeReplies() {
final all = replies.join();
@@ -79,6 +79,13 @@ abstract class EscapeHandler {
void cursorPrecedingLine(int amount);
/// Moves the cursor forward [amount] tab stops (CHT), stopping at the last
/// column.
void cursorForwardTab(int amount);
/// Moves the cursor back [amount] tab stops (CBT), stopping at column 0.
void cursorBackwardTab(int amount);
void eraseDisplayBelow();
void eraseDisplayAbove();
+46 -8
View File
@@ -390,7 +390,13 @@ class EscapeParser {
static const _maxParamValue = 100000000;
late final _csiHandlers = FastLookupTable<_CsiHandler>({
// 'a'.codeUnitAt(0): _csiHandleCursorHorizontalRelative,
'a'.codeUnitAt(0): _csiHandleCursorHorizontalRelative,
'`'.codeUnitAt(0): _csiHandleCursorHorizontalAbsolute,
'e'.codeUnitAt(0): _csiHandleCursorVerticalRelative,
'I'.codeUnitAt(0): _csiHandleCursorForwardTab,
'Z'.codeUnitAt(0): _csiHandleCursorBackwardTab,
's'.codeUnitAt(0): _csiHandleSaveCursor,
'u'.codeUnitAt(0): _csiHandleRestoreCursor,
'b'.codeUnitAt(0): _csiHandleRepeatPreviousCharacter,
'c'.codeUnitAt(0): _csiHandleSendDeviceAttributes,
'd'.codeUnitAt(0): _csiHandleLinePositionAbsolute,
@@ -424,13 +430,45 @@ class EscapeParser {
/// `ESC [ Ps a` Cursor Horizontal Position Relative (HPR)
///
/// https://terminalguide.namepad.de/seq/csi_sa/
// void _csiHandleCursorHorizontalRelative() {
// if (_csi.params.isEmpty) {
// handler.cursorHorizontal(1);
// } else {
// handler.cursorHorizontal(_csi.params[0]);
// }
// }
void _csiHandleCursorHorizontalRelative() {
handler.moveCursorX(_param(0));
}
/// `ESC [ Ps e` Cursor Vertical Position Relative (VPR)
///
/// https://terminalguide.namepad.de/seq/csi_se/
void _csiHandleCursorVerticalRelative() {
handler.moveCursorY(_param(0));
}
/// `ESC [ Ps I` Cursor Horizontal Forward Tabulation (CHT)
///
/// https://terminalguide.namepad.de/seq/csi_ci/
void _csiHandleCursorForwardTab() {
handler.cursorForwardTab(_param(0));
}
/// `ESC [ Ps Z` Cursor Backward Tabulation (CBT)
///
/// https://terminalguide.namepad.de/seq/csi_cz/
void _csiHandleCursorBackwardTab() {
handler.cursorBackwardTab(_param(0));
}
/// `ESC [ s` Save Cursor (SCOSC). With parameters this would be DECSLRM
/// (left and right margins), which is not supported.
///
/// https://terminalguide.namepad.de/seq/csi_ss/
void _csiHandleSaveCursor() {
if (_csi.params.isEmpty) handler.saveCursor();
}
/// `ESC [ u` Restore Cursor (SCORC)
///
/// https://terminalguide.namepad.de/seq/csi_su/
void _csiHandleRestoreCursor() {
if (_csi.params.isEmpty) handler.restoreCursor();
}
/// `ESC [ Ps b` Repeat Previous Character (REP)
///
+10
View File
@@ -32,6 +32,16 @@ class TabStops {
return null;
}
/// Finds the closest tab stop index below [start], or null without one.
int? findPrevious(int start) {
for (var i = min(start, _stops.length) - 1; i >= 0; i--) {
if (_stops[i]) {
return i;
}
}
return null;
}
/// Sets the tab stop at [index]. If there is already a tab stop at [index],
/// this method does nothing.
///
+25 -1
View File
@@ -464,7 +464,7 @@ class Terminal with Observable implements TerminalState, EscapeHandler {
@override
void setTapStop() {
_tabStops.isSetAt(_buffer.cursorX);
_tabStops.setAt(_buffer.cursorX);
}
@override
@@ -588,6 +588,30 @@ class Terminal with Observable implements TerminalState, EscapeHandler {
_buffer.setCursorX(0);
}
@override
void cursorForwardTab(int amount) {
for (var i = 0; i < amount; i++) {
final next = _tabStops.find(_buffer.cursorX + 1, _viewWidth);
if (next == null) {
_buffer.setCursorX(_viewWidth - 1);
return;
}
_buffer.setCursorX(next);
}
}
@override
void cursorBackwardTab(int amount) {
for (var i = 0; i < amount; i++) {
final previous = _tabStops.findPrevious(_buffer.cursorX);
if (previous == null) {
_buffer.setCursorX(0);
return;
}
_buffer.setCursorX(previous);
}
}
@override
void eraseDisplayBelow() {
_buffer.eraseDisplayFromCursor();
+10
View File
@@ -471,6 +471,16 @@ class _TerminalDebuggerHandler implements EscapeHandler {
onCommand('setCursorUnderline');
}
@override
void cursorForwardTab(int amount) {
onCommand('cursorForwardTab $amount');
}
@override
void cursorBackwardTab(int amount) {
onCommand('cursorBackwardTab $amount');
}
@override
void requestStatusString(String setting) {
onCommand('requestStatusString $setting');