diff --git a/app/test/third_party_xterm/sgr_test.dart b/app/test/third_party_xterm/sgr_test.dart new file mode 100644 index 00000000..b3e90905 --- /dev/null +++ b/app/test/third_party_xterm/sgr_test.dart @@ -0,0 +1,98 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:xterm/xterm.dart'; + +import 'xterm_fixture.dart'; + +void main() { + CellData styled(String sgr) { + final t = FixtureTerminal()..feed('\x1b[${sgr}mX'); + return t.cell(0, 0); + } + + int underlineStyle(CellData cell) => cell.flags & CellAttr.underline == 0 + ? 0 + : (cell.flags & CellAttr.underlineStyleMask) >> + CellAttr.underlineStyleShift; + + test('4:3 is a curly underline, not a yellow background', () { + final cell = styled('4:3'); + expect(underlineStyle(cell), 3); + expect(cell.background, 0); + expect(cell.flags & CellAttr.italic, 0); + }); + + test('underline styles 4:0 to 4:5 and plain 4 / 24 / 21', () { + expect(underlineStyle(styled('4')), 1); + expect(underlineStyle(styled('4:0')), 0); + expect(underlineStyle(styled('4:1')), 1); + expect(underlineStyle(styled('4:2')), 2); + expect(underlineStyle(styled('4:4')), 4); + expect(underlineStyle(styled('4:5')), 5); + expect(underlineStyle(styled('4:3;24')), 0); + expect(underlineStyle(styled('4:3;4:0')), 0); + expect(underlineStyle(styled('21')), 2); + expect(styled('21').flags & CellAttr.bold, 0); + }); + + test('colon direct colours with and without a colour space id', () { + expect(styled('38:2::10:20:30').foreground, rgb(10, 20, 30)); + expect(styled('38:2:1:10:20:30').foreground, rgb(10, 20, 30)); + expect(styled('38:2:10:20:30').foreground, rgb(10, 20, 30)); + expect(styled('48:2::1:2:3').background, rgb(1, 2, 3)); + }); + + test('colon and semicolon indexed colours', () { + expect(styled('38:5:196').foreground, palette(196)); + expect(styled('48:5:21').background, palette(21)); + expect(styled('38;5;196').foreground, palette(196)); + expect(styled('38;2;1;2;3;1').foreground, rgb(1, 2, 3)); + expect(styled('38;2;1;2;3;1').flags & CellAttr.bold, isNot(0)); + }); + + test('colon groups end at the next semicolon', () { + final cell = styled('38:5:2;41;1'); + expect(cell.foreground, palette(2)); + expect(cell.background, named(NamedColor.red)); + expect(cell.flags & CellAttr.bold, isNot(0)); + }); + + test('underline colour 58 / 59 is consumed, not applied elsewhere', () { + var cell = styled('58:2::255:0:0;4:3'); + expect(cell.foreground, 0); + expect(cell.background, 0); + expect(underlineStyle(cell), 3); + cell = styled('58;5;9;1'); + expect(cell.foreground, 0); + expect(cell.flags & CellAttr.bold, isNot(0)); + cell = styled('58;2;1;2;3;59;7'); + expect(cell.flags & CellAttr.inverse, isNot(0)); + expect(cell.background, 0); + }); + + test('truncated extended colours do not throw or misapply', () { + expect(styled('38;5').foreground, 0); + expect(styled('38;2;1;2').foreground, 0); + expect(styled('48').background, 0); + }); + + test('22 clears bold and faint; 53/55 toggle overline', () { + expect(styled('1;2;22').flags & (CellAttr.bold | CellAttr.faint), 0); + expect(styled('53').flags & CellAttr.overline, isNot(0)); + expect(styled('53;55').flags & CellAttr.overline, 0); + }); + + test('a private-prefixed m (modifyOtherKeys) is not SGR', () { + final t = FixtureTerminal()..feed('\x1b[>4;2mX'); + expect(t.cell(0, 0).flags, 0); + expect(t.row(0), 'X'); + }); + + test('empty parameters default: CSI ; 5 H and CSI 3 H', () { + final t = FixtureTerminal()..feed('\x1b[;5H'); + expect(t.buffer.cursorX, 4); + expect(t.buffer.cursorY, 0); + t.feed('\x1b[3H'); + expect(t.buffer.cursorX, 0); + expect(t.buffer.cursorY, 2); + }); +} diff --git a/app/test/third_party_xterm/xterm_fixture.dart b/app/test/third_party_xterm/xterm_fixture.dart new file mode 100644 index 00000000..6dbce0e8 --- /dev/null +++ b/app/test/third_party_xterm/xterm_fixture.dart @@ -0,0 +1,41 @@ +import 'package:xterm/xterm.dart'; + +/// A terminal fed raw bytes in byte-fixture tests, with every reply it sends +/// to the program collected in [replies]. +class FixtureTerminal { + FixtureTerminal({int width = 20, int height = 5}) { + terminal.onOutput = replies.add; + terminal.resize(width, height); + } + + final terminal = Terminal(maxLines: 100); + final replies = []; + + Buffer get buffer => terminal.buffer; + + void feed(String bytes) => terminal.write(bytes); + + /// The text of screen row [y], trailing blanks trimmed. + String row(int y) => + buffer.lines[buffer.scrollBack + y].getText().trimRight(); + + /// The cell at column [x] of screen row [y]. + CellData cell(int x, int y) { + final data = CellData.empty(); + buffer.lines[buffer.scrollBack + y].getCellData(x, data); + return data; + } + + /// The replies sent so far, joined, and then forgotten. + String takeReplies() { + final all = replies.join(); + replies.clear(); + return all; + } +} + +int rgb(int r, int g, int b) => CellColor.rgb | (r << 16) | (g << 8) | b; + +int palette(int index) => CellColor.palette | index; + +int named(int index) => CellColor.named | index; diff --git a/app/third_party/xterm/lib/src/core/cell.dart b/app/third_party/xterm/lib/src/core/cell.dart index c52039ff..0ef0afd7 100644 --- a/app/third_party/xterm/lib/src/core/cell.dart +++ b/app/third_party/xterm/lib/src/core/cell.dart @@ -44,6 +44,13 @@ abstract class CellAttr { static const inverse = 1 << 5; static const invisible = 1 << 6; static const strikethrough = 1 << 7; + + /// The underline style while [underline] is set: 1 single (also 0), 2 + /// double, 3 curly, 4 dotted, 5 dashed. + static const underlineStyleShift = 8; + static const underlineStyleMask = 7 << underlineStyleShift; + + static const overline = 1 << 11; } abstract class CellColor { diff --git a/app/third_party/xterm/lib/src/core/cursor.dart b/app/third_party/xterm/lib/src/core/cursor.dart index ce08610d..0bb3c17e 100644 --- a/app/third_party/xterm/lib/src/core/cursor.dart +++ b/app/third_party/xterm/lib/src/core/cursor.dart @@ -23,8 +23,19 @@ class CursorStyle { attrs |= CellAttr.italic; } - void setUnderline() { - attrs |= CellAttr.underline; + /// Turns underline on in [style] (see [CellAttr.underlineStyleMask]). + void setUnderline([int style = 1]) { + attrs = (attrs & ~CellAttr.underlineStyleMask) | + CellAttr.underline | + ((style & 7) << CellAttr.underlineStyleShift); + } + + void setOverline() { + attrs |= CellAttr.overline; + } + + void unsetOverline() { + attrs &= ~CellAttr.overline; } void setBlink() { @@ -56,7 +67,7 @@ class CursorStyle { } void unsetUnderline() { - attrs &= ~CellAttr.underline; + attrs &= ~(CellAttr.underline | CellAttr.underlineStyleMask); } void unsetBlink() { @@ -89,6 +100,15 @@ class CursorStyle { bool get isInvisible => (attrs & CellAttr.invisible) != 0; + bool get isStrikethrough => (attrs & CellAttr.strikethrough) != 0; + + bool get isOverline => (attrs & CellAttr.overline) != 0; + + /// The underline style (1-5), or 0 without underline. + int get underlineStyle => isUnderline + ? (attrs & CellAttr.underlineStyleMask) >> CellAttr.underlineStyleShift + : 0; + void setForegroundColor16(int color) { foreground = color | CellColor.named; } 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 166fb485..677cb2cb 100644 --- a/app/third_party/xterm/lib/src/core/escape/handler.dart +++ b/app/third_party/xterm/lib/src/core/escape/handler.dart @@ -163,6 +163,13 @@ abstract class EscapeHandler { void setCursorUnderline(); + /// Underline in [style]: 1 single, 2 double, 3 curly, 4 dotted, 5 dashed. + void setCursorUnderlineStyle(int style); + + void setCursorOverline(); + + void unsetCursorOverline(); + void setCursorBlink(); void setCursorInverse(); 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 0c7e74a9..ccb73d34 100644 --- a/app/third_party/xterm/lib/src/core/escape/parser.dart +++ b/app/third_party/xterm/lib/src/core/escape/parser.dart @@ -1,6 +1,6 @@ -import 'package:xterm/src/core/color.dart'; import 'package:xterm/src/core/mouse/mode.dart'; import 'package:xterm/src/core/escape/handler.dart'; +import 'package:xterm/src/core/escape/sgr.dart'; import 'package:xterm/src/utils/ascii.dart'; import 'package:xterm/src/utils/byte_consumer.dart'; import 'package:xterm/src/utils/char_code.dart'; @@ -193,10 +193,26 @@ class EscapeParser { final consumed = _consumeCsi(); if (!consumed) return false; - final csiHandler = _csiHandlers[_csi.finalByte]; + final finalByte = _csi.finalByte; + + // Sequences with intermediate bytes (`CSI Ps SP q`, `CSI ! p`, + // `CSI ? Ps $ p`) are functions distinct from the plain final byte. + if (_csi.intermediates != 0) { + _csiHandleWithIntermediates(); + return true; + } + + // A private prefix (`CSI > 4 ; 2 m`, `CSI ? u`) selects a different + // function too; only the handlers that read the prefix accept one. + if (_csi.prefix != null && !_prefixedFinalBytes.contains(finalByte)) { + handler.unknownCSI(finalByte); + return true; + } + + final csiHandler = _csiHandlers[finalByte]; if (csiHandler == null) { - handler.unknownCSI(_csi.finalByte); + handler.unknownCSI(finalByte); } else { csiHandler(); } @@ -204,30 +220,48 @@ class EscapeParser { return true; } + /// Final bytes whose handlers interpret a private prefix themselves. + static final _prefixedFinalBytes = { + 'c'.codeUnitAt(0), + 'h'.codeUnitAt(0), + 'l'.codeUnitAt(0), + 'n'.codeUnitAt(0), + 'J'.codeUnitAt(0), + 'K'.codeUnitAt(0), + }; + + /// Dispatches a CSI that carries intermediate bytes. + void _csiHandleWithIntermediates() { + handler.unknownCSI(_csi.finalByte); + } + /// The last parsed [_Csi]. This is a mutable singletion by design to reduce /// object allocations. - final _csi = _Csi(finalByte: 0, params: []); + final _csi = _Csi(); /// Parse a CSI from the head of the queue. Return false if the CSI isn't /// complete. After a CSI is successfully parsed, [_csi] is updated. + /// + /// Parameters are separated by `;`; a `:` introduces a subparameter of the + /// preceding parameter (ITU T.416, used by `4:3` and `38:2::r:g:b`). An + /// empty parameter reads as 0, so `CSI ; 5 H` has the parameters 0 and 5. bool _consumeCsi() { if (_queue.isEmpty) { return false; } - _csi.params.clear(); + _csi.reset(); // test whether the csi is a `CSI ? Ps ...` or `CSI Ps ...` final prefix = _queue.peek(); - if (prefix >= Ascii.colon && prefix <= Ascii.questionMark) { + if (prefix >= Ascii.lessThan && prefix <= Ascii.questionMark) { _csi.prefix = prefix; _queue.consume(); - } else { - _csi.prefix = null; } var param = 0; var hasParam = false; + var nextIsSub = false; while (true) { // The sequence isn't completed, just ignore it. if (_queue.isEmpty) { @@ -236,37 +270,42 @@ class EscapeParser { final char = _queue.consume(); - if (char == Ascii.semicolon) { - if (hasParam) { - _csi.params.add(param); - } - param = 0; - continue; - } - if (char >= Ascii.num0 && char <= Ascii.num9) { hasParam = true; - param *= 10; - param += char - Ascii.num0; + if (param < _maxParamValue) { + param = param * 10 + char - Ascii.num0; + } continue; } - if (char > Ascii.NULL && char < Ascii.num0) { - // intermediates.add(char); + if (char == Ascii.semicolon || char == Ascii.colon) { + _csi.add(param, nextIsSub); + nextIsSub = char == Ascii.colon; + param = 0; + hasParam = true; + continue; + } + + if (char >= Ascii.space && char < Ascii.num0) { + _csi.intermediates = (_csi.intermediates << 8 | char) & 0xFFFFFF; continue; } if (char >= Ascii.atSign && char <= Ascii.tilde) { if (hasParam) { - _csi.params.add(param); + _csi.add(param, nextIsSub); } _csi.finalByte = char; return true; } + + // Other bytes (C0 controls, stray private markers) are ignored. } } + static const _maxParamValue = 100000000; + late final _csiHandlers = FastLookupTable<_CsiHandler>({ // 'a'.codeUnitAt(0): _csiHandleCursorHorizontalRelative, 'b'.codeUnitAt(0): _csiHandleRepeatPreviousCharacter, @@ -314,12 +353,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_sb/ void _csiHandleRepeatPreviousCharacter() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - if (amount == 0) amount = 1; - } + final amount = _param(0); handler.repeatPreviousCharacter(amount); } @@ -342,28 +376,23 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_sd/ void _csiHandleLinePositionAbsolute() { - var y = 1; - - if (_csi.params.isNotEmpty) { - y = _csi.params[0]; - } - - handler.setCursorY(y - 1); + handler.setCursorY(_param(0) - 1); } /// `ESC [ Ps ; Ps f` Alias: Set Cursor Position /// /// https://terminalguide.namepad.de/seq/csi_sf/ void _csiHandleCursorPosition() { - var row = 1; - var col = 1; + handler.setCursor(_param(1) - 1, _param(0) - 1); + } - if (_csi.params.length == 2) { - row = _csi.params[0]; - col = _csi.params[1]; - } - - handler.setCursor(col - 1, row - 1); + /// The parameter at [index], or [fallback] when it is missing or 0 (an + /// empty parameter reads as 0, which means "default" for counts and + /// positions). + int _param(int index, [int fallback = 1]) { + final params = _csi.params; + if (index >= params.length || params[index] == 0) return fallback; + return params[index]; } /// `ESC [ Ps g` Tab Clear (TBC) @@ -408,215 +437,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_sm/ void _csiHandleSgr() { - final params = _csi.params; - - if (params.isEmpty) { - return handler.resetCursorStyle(); - } - - // This is a workaround for a bug in the analyzer. - // ignore: dead_code - for (var i = 0; i < _csi.params.length; i++) { - final param = params[i]; - switch (param) { - case 0: - handler.resetCursorStyle(); - continue; - case 1: - handler.setCursorBold(); - continue; - case 2: - handler.setCursorFaint(); - continue; - case 3: - handler.setCursorItalic(); - continue; - case 4: - handler.setCursorUnderline(); - continue; - case 5: - handler.setCursorBlink(); - continue; - case 7: - handler.setCursorInverse(); - continue; - case 8: - handler.setCursorInvisible(); - continue; - case 9: - handler.setCursorStrikethrough(); - continue; - - case 21: - handler.unsetCursorBold(); - continue; - case 22: - handler.unsetCursorFaint(); - continue; - case 23: - handler.unsetCursorItalic(); - continue; - case 24: - handler.unsetCursorUnderline(); - continue; - case 25: - handler.unsetCursorBlink(); - continue; - case 27: - handler.unsetCursorInverse(); - continue; - case 28: - handler.unsetCursorInvisible(); - continue; - case 29: - handler.unsetCursorStrikethrough(); - continue; - - case 30: - handler.setForegroundColor16(NamedColor.black); - continue; - case 31: - handler.setForegroundColor16(NamedColor.red); - continue; - case 32: - handler.setForegroundColor16(NamedColor.green); - continue; - case 33: - handler.setForegroundColor16(NamedColor.yellow); - continue; - case 34: - handler.setForegroundColor16(NamedColor.blue); - continue; - case 35: - handler.setForegroundColor16(NamedColor.magenta); - continue; - case 36: - handler.setForegroundColor16(NamedColor.cyan); - continue; - case 37: - handler.setForegroundColor16(NamedColor.white); - continue; - case 38: - final mode = params[i + 1]; - switch (mode) { - case 2: - final r = params[i + 2]; - final g = params[i + 3]; - final b = params[i + 4]; - handler.setForegroundColorRgb(r, g, b); - i += 4; - break; - case 5: - final index = params[i + 2]; - handler.setForegroundColor256(index); - i += 2; - break; - } - continue; - case 39: - handler.resetForeground(); - continue; - - case 40: - handler.setBackgroundColor16(NamedColor.black); - continue; - case 41: - handler.setBackgroundColor16(NamedColor.red); - continue; - case 42: - handler.setBackgroundColor16(NamedColor.green); - continue; - case 43: - handler.setBackgroundColor16(NamedColor.yellow); - continue; - case 44: - handler.setBackgroundColor16(NamedColor.blue); - continue; - case 45: - handler.setBackgroundColor16(NamedColor.magenta); - continue; - case 46: - handler.setBackgroundColor16(NamedColor.cyan); - continue; - case 47: - handler.setBackgroundColor16(NamedColor.white); - continue; - case 48: - final mode = params[i + 1]; - switch (mode) { - case 2: - final r = params[i + 2]; - final g = params[i + 3]; - final b = params[i + 4]; - handler.setBackgroundColorRgb(r, g, b); - i += 4; - break; - case 5: - final index = params[i + 2]; - handler.setBackgroundColor256(index); - i += 2; - break; - } - continue; - case 49: - handler.resetBackground(); - continue; - - case 90: - handler.setForegroundColor16(NamedColor.brightBlack); - continue; - case 91: - handler.setForegroundColor16(NamedColor.brightRed); - continue; - case 92: - handler.setForegroundColor16(NamedColor.brightGreen); - continue; - case 93: - handler.setForegroundColor16(NamedColor.brightYellow); - continue; - case 94: - handler.setForegroundColor16(NamedColor.brightBlue); - continue; - case 95: - handler.setForegroundColor16(NamedColor.brightMagenta); - continue; - case 96: - handler.setForegroundColor16(NamedColor.brightCyan); - continue; - case 97: - handler.setForegroundColor16(NamedColor.brightWhite); - continue; - - case 100: - handler.setBackgroundColor16(NamedColor.brightBlack); - continue; - case 101: - handler.setBackgroundColor16(NamedColor.brightRed); - continue; - case 102: - handler.setBackgroundColor16(NamedColor.brightGreen); - continue; - case 103: - handler.setBackgroundColor16(NamedColor.brightYellow); - continue; - case 104: - handler.setBackgroundColor16(NamedColor.brightBlue); - continue; - case 105: - handler.setBackgroundColor16(NamedColor.brightMagenta); - continue; - case 106: - handler.setBackgroundColor16(NamedColor.brightCyan); - continue; - case 107: - handler.setBackgroundColor16(NamedColor.brightWhite); - continue; - - default: - handler.unsupportedStyle(param); - continue; - } - } + applySgr(handler, _csi.rawParams, _csi.rawIsSub); } /// `ESC [ Ps n` Device Status Report [Dispatch] (DSR) @@ -637,20 +458,10 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_sr/ void _csiHandleSetMargins() { - var top = 1; - int? bottom; - if (_csi.params.length > 2) return; - if (_csi.params.isNotEmpty) { - top = _csi.params[0]; - - if (_csi.params.length == 2) { - bottom = _csi.params[1] - 1; - } - } - - handler.setMargins(top - 1, bottom); + final bottom = _param(1, 0); + handler.setMargins(_param(0) - 1, bottom == 0 ? null : bottom - 1); } /// `ESC [ Ps t` Window operations [DISPATCH] @@ -713,12 +524,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_ca/ void _csiHandleCursorUp() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - if (amount == 0) amount = 1; - } + final amount = _param(0); handler.moveCursorY(-amount); } @@ -727,12 +533,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_cb/ void _csiHandleCursorDown() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - if (amount == 0) amount = 1; - } + final amount = _param(0); handler.moveCursorY(amount); } @@ -741,12 +542,7 @@ class EscapeParser { /// /// Cursor Right (CUF) void _csiHandleCursorForward() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - if (amount == 0) amount = 1; - } + final amount = _param(0); handler.moveCursorX(amount); } @@ -755,12 +551,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_cd/ void _csiHandleCursorBackward() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - if (amount == 0) amount = 1; - } + final amount = _param(0); handler.moveCursorX(-amount); } @@ -769,12 +560,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_ce/ void _csiHandleCursorNextLine() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - if (amount == 0) amount = 1; - } + final amount = _param(0); handler.cursorNextLine(amount); } @@ -783,12 +569,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_cf/ void _csiHandleCursorPrecedingLine() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - if (amount == 0) amount = 1; - } + final amount = _param(0); handler.cursorPrecedingLine(amount); } @@ -850,11 +631,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_cl/ void _csiHandleInsertLines() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - } + final amount = _param(0); handler.insertLines(amount); } @@ -863,11 +640,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_cm/ void _csiHandleDeleteLines() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - } + final amount = _param(0); handler.deleteLines(amount); } @@ -876,11 +649,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_cp/ void _csiHandleDelete() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - } + final amount = _param(0); handler.deleteChars(amount); } @@ -889,11 +658,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_cs/ void _csiHandleScrollUp() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - } + final amount = _param(0); handler.scrollUp(amount); } @@ -902,11 +667,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_ct_1param/ void _csiHandleScrollDown() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - } + final amount = _param(0); handler.scrollDown(amount); } @@ -915,11 +676,7 @@ class EscapeParser { /// /// https://terminalguide.namepad.de/seq/csi_cx/ void _csiHandleEraseCharacters() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - } + final amount = _param(0); handler.eraseChars(amount); } @@ -932,11 +689,7 @@ class EscapeParser { /// contents to the right. The contents of the amount right-most columns in /// the scroll region are lost. The cursor position is not changed. void _csiHandleInsertBlankCharacters() { - var amount = 1; - - if (_csi.params.isNotEmpty) { - amount = _csi.params[0]; - } + final amount = _param(0); handler.insertBlankChars(amount); } @@ -1126,18 +879,41 @@ class EscapeParser { } class _Csi { - _Csi({ - required this.params, - required this.finalByte, - // required this.intermediates, - }); + /// At most this many parameters are kept; further ones are dropped. + static const maxParams = 32; int? prefix; - List params; + /// Intermediate bytes (0x20-0x2F), packed one per byte, last one lowest. + int intermediates = 0; - int finalByte; - // final List intermediates; + /// The parameters, not counting colon subparameters. + final List params = []; + + /// Every parameter including subparameters, in order. + final List rawParams = []; + + /// Whether the parameter at the same index of [rawParams] was introduced + /// by a colon. + final List rawIsSub = []; + + int finalByte = 0; + + void reset() { + prefix = null; + intermediates = 0; + params.clear(); + rawParams.clear(); + rawIsSub.clear(); + finalByte = 0; + } + + void add(int value, bool isSub) { + if (rawParams.length >= maxParams) return; + rawParams.add(value); + rawIsSub.add(isSub); + if (!isSub) params.add(value); + } @override String toString() { diff --git a/app/third_party/xterm/lib/src/core/escape/sgr.dart b/app/third_party/xterm/lib/src/core/escape/sgr.dart new file mode 100644 index 00000000..69e2b4e0 --- /dev/null +++ b/app/third_party/xterm/lib/src/core/escape/sgr.dart @@ -0,0 +1,152 @@ +import 'package:xterm/src/core/color.dart'; +import 'package:xterm/src/core/escape/handler.dart'; + +/// Applies the parameters of `CSI Pm m` (SGR) to [handler]. +/// +/// [params] holds every parameter in order and [isSub] marks the ones that +/// were introduced by a colon, so a parameter followed by colon +/// subparameters forms one group: `4:3` is curly underline, not underline +/// followed by `3` (italic), and `38:2::r:g:b` is one direct colour. +/// +/// The legacy semicolon forms `38;5;n` and `38;2;r;g;b` consume the +/// parameters that follow. A truncated sequence stops processing instead of +/// reading past the end. +/// +/// https://terminalguide.namepad.de/seq/csi_sm/ +void applySgr(EscapeHandler handler, List params, List isSub) { + if (params.isEmpty) { + handler.resetCursorStyle(); + return; + } + + var i = 0; + while (i < params.length) { + final code = params[i++]; + + final subStart = i; + while (i < params.length && isSub[i]) { + i++; + } + final subCount = i - subStart; + + switch (code) { + case 0: + handler.resetCursorStyle(); + case 1: + handler.setCursorBold(); + case 2: + handler.setCursorFaint(); + case 3: + handler.setCursorItalic(); + case 4: + final style = subCount > 0 ? params[subStart] : 1; + if (style == 0) { + handler.unsetCursorUnderline(); + } else { + handler.setCursorUnderlineStyle(style > 5 ? 1 : style); + } + case 5: + case 6: + handler.setCursorBlink(); + case 7: + handler.setCursorInverse(); + case 8: + handler.setCursorInvisible(); + case 9: + handler.setCursorStrikethrough(); + case 21: + // ECMA-48 and xterm: doubly underlined (not "bold off"). + handler.setCursorUnderlineStyle(2); + case 22: + handler.unsetCursorBold(); + handler.unsetCursorFaint(); + case 23: + handler.unsetCursorItalic(); + case 24: + handler.unsetCursorUnderline(); + case 25: + handler.unsetCursorBlink(); + case 27: + handler.unsetCursorInverse(); + case 28: + handler.unsetCursorInvisible(); + case 29: + handler.unsetCursorStrikethrough(); + case >= 30 && <= 37: + handler.setForegroundColor16(NamedColor.black + code - 30); + case 38: + case 48: + case 58: + if (subCount > 0) { + _extendedColor(handler, code, params, subStart, subCount); + } else { + final used = _semicolonColorLength(params, isSub, i); + if (used < 0) return; // truncated: nothing sensible follows + _extendedColor(handler, code, params, i, used); + i += used; + } + case 39: + handler.resetForeground(); + case >= 40 && <= 47: + handler.setBackgroundColor16(NamedColor.black + code - 40); + case 49: + handler.resetBackground(); + case 53: + handler.setCursorOverline(); + case 55: + handler.unsetCursorOverline(); + case 59: + // Default underline colour; underline colours are not rendered. + break; + case >= 90 && <= 97: + handler.setForegroundColor16(NamedColor.brightBlack + code - 90); + case >= 100 && <= 107: + handler.setBackgroundColor16(NamedColor.brightBlack + code - 100); + default: + handler.unsupportedStyle(code); + } + } +} + +/// How many parameters after [at] the semicolon form of an extended colour +/// takes (`5;n` or `2;r;g;b`), or -1 when the sequence ends too early. +int _semicolonColorLength(List params, List isSub, int at) { + if (at >= params.length) return -1; + final needed = switch (params[at]) { + 5 => 2, + 2 => 4, + _ => 1, + }; + if (at + needed > params.length) return -1; + return needed; +} + +/// Applies an extended colour whose mode is `params[at]` and whose values +/// follow it, [count] parameters in all. [target] is 38 (foreground), 48 +/// (background) or 58 (underline colour, accepted and ignored). +void _extendedColor( + EscapeHandler handler, + int target, + List params, + int at, + int count, +) { + final mode = params[at]; + if (mode == 5 && count >= 2) { + final index = params[at + 1]; + if (index > 255) return; + if (target == 38) handler.setForegroundColor256(index); + if (target == 48) handler.setBackgroundColor256(index); + return; + } + if (mode == 2 && count >= 4) { + // `2:Pi:r:g:b` carries a colour space id first; `2:r:g:b` and the + // semicolon form `2;r;g;b` do not. + final rgbAt = count >= 5 ? at + 2 : at + 1; + final r = params[rgbAt].clamp(0, 255); + final g = params[rgbAt + 1].clamp(0, 255); + final b = params[rgbAt + 2].clamp(0, 255); + if (target == 38) handler.setForegroundColorRgb(r, g, b); + if (target == 48) handler.setBackgroundColorRgb(r, g, b); + } +} diff --git a/app/third_party/xterm/lib/src/terminal.dart b/app/third_party/xterm/lib/src/terminal.dart index 461e2084..95ca09e4 100644 --- a/app/third_party/xterm/lib/src/terminal.dart +++ b/app/third_party/xterm/lib/src/terminal.dart @@ -782,6 +782,21 @@ class Terminal with Observable implements TerminalState, EscapeHandler { _cursorStyle.setUnderline(); } + @override + void setCursorUnderlineStyle(int style) { + _cursorStyle.setUnderline(style); + } + + @override + void setCursorOverline() { + _cursorStyle.setOverline(); + } + + @override + void unsetCursorOverline() { + _cursorStyle.unsetOverline(); + } + @override void setCursorBlink() { _cursorStyle.setBlink(); diff --git a/app/third_party/xterm/lib/src/utils/debugger.dart b/app/third_party/xterm/lib/src/utils/debugger.dart index 303bbbbe..c7c2d54a 100644 --- a/app/third_party/xterm/lib/src/utils/debugger.dart +++ b/app/third_party/xterm/lib/src/utils/debugger.dart @@ -471,6 +471,21 @@ class _TerminalDebuggerHandler implements EscapeHandler { onCommand('setCursorUnderline'); } + @override + void setCursorUnderlineStyle(int style) { + onCommand('setCursorUnderlineStyle $style'); + } + + @override + void setCursorOverline() { + onCommand('setCursorOverline'); + } + + @override + void unsetCursorOverline() { + onCommand('unsetCursorOverline'); + } + @override void setCursorBlink() { onCommand('setCursorBlink');