From 326e7710d4ee57fcff00389271de3b51186fc33b Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Wed, 25 Nov 2026 23:26:18 +0000 Subject: [PATCH] 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 --- .../cursor_movement_test.dart | 60 +++++++++++++++++++ app/test/third_party_xterm/xterm_fixture.dart | 6 ++ .../xterm/lib/src/core/escape/handler.dart | 7 +++ .../xterm/lib/src/core/escape/parser.dart | 54 ++++++++++++++--- app/third_party/xterm/lib/src/core/tabs.dart | 10 ++++ app/third_party/xterm/lib/src/terminal.dart | 26 +++++++- .../xterm/lib/src/utils/debugger.dart | 10 ++++ 7 files changed, 164 insertions(+), 9 deletions(-) create mode 100644 app/test/third_party_xterm/cursor_movement_test.dart diff --git a/app/test/third_party_xterm/cursor_movement_test.dart b/app/test/third_party_xterm/cursor_movement_test.dart new file mode 100644 index 00000000..c932c482 --- /dev/null +++ b/app/test/third_party_xterm/cursor_movement_test.dart @@ -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); + }); +} diff --git a/app/test/third_party_xterm/xterm_fixture.dart b/app/test/third_party_xterm/xterm_fixture.dart index 6dbce0e8..5975838e 100644 --- a/app/test/third_party_xterm/xterm_fixture.dart +++ b/app/test/third_party_xterm/xterm_fixture.dart @@ -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(); 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 a38a55eb..c13a4b85 100644 --- a/app/third_party/xterm/lib/src/core/escape/handler.dart +++ b/app/third_party/xterm/lib/src/core/escape/handler.dart @@ -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(); 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 cd096543..81ba6abc 100644 --- a/app/third_party/xterm/lib/src/core/escape/parser.dart +++ b/app/third_party/xterm/lib/src/core/escape/parser.dart @@ -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) /// diff --git a/app/third_party/xterm/lib/src/core/tabs.dart b/app/third_party/xterm/lib/src/core/tabs.dart index 385f9499..6ac2c052 100644 --- a/app/third_party/xterm/lib/src/core/tabs.dart +++ b/app/third_party/xterm/lib/src/core/tabs.dart @@ -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. /// diff --git a/app/third_party/xterm/lib/src/terminal.dart b/app/third_party/xterm/lib/src/terminal.dart index 613413b2..523776e4 100644 --- a/app/third_party/xterm/lib/src/terminal.dart +++ b/app/third_party/xterm/lib/src/terminal.dart @@ -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(); diff --git a/app/third_party/xterm/lib/src/utils/debugger.dart b/app/third_party/xterm/lib/src/utils/debugger.dart index 7f0319d8..04323e6e 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 cursorForwardTab(int amount) { + onCommand('cursorForwardTab $amount'); + } + + @override + void cursorBackwardTab(int amount) { + onCommand('cursorBackwardTab $amount'); + } + @override void requestStatusString(String setting) { onCommand('requestStatusString $setting');