xterm: consume DCS, SOS, PM and APC strings; answer DECRQSS
ESC P, ESC X, ESC ^ and ESC _ now open a control string whose body is swallowed up to ST (ESC \), across writes; CAN/SUB or a non-ST ESC abort it. Previously the bodies were printed (nvim's DECRQSS ESC P $qm ESC \ showed as "$qm"). DECRQSS m and r are answered with DCS 1 $ r ... ST, other requests with DCS 0 $ r ST. ESC ESC restarts the escape. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
import 'xterm_fixture.dart';
|
||||
|
||||
void main() {
|
||||
test('DCS, APC, PM and SOS bodies are swallowed, not printed', () {
|
||||
final t = FixtureTerminal()
|
||||
..feed('a\x1bP+q544e\x1b\\b')
|
||||
..feed('\x1b_Gf=24;AAAA\x1b\\c')
|
||||
..feed('\x1b^private\x1b\\d')
|
||||
..feed('\x1bXstring\x1b\\e');
|
||||
expect(t.row(0), 'abcde');
|
||||
});
|
||||
|
||||
test('a control string split across writes is still swallowed', () {
|
||||
final t = FixtureTerminal()
|
||||
..feed('x\x1bPtmux;')
|
||||
..feed('\x1b')
|
||||
..feed('\x1b[31mhidden')
|
||||
..feed('\x1b')
|
||||
..feed('\\y');
|
||||
// An ESC that is not ST aborts the string, as on a VT; the next ESC
|
||||
// starts a CSI, so the output after it is ordinary text again.
|
||||
expect(t.row(0), 'xhiddeny');
|
||||
expect(t.cell(1, 0).foreground, named(1));
|
||||
});
|
||||
|
||||
test('ST split between writes ends the string', () {
|
||||
final t = FixtureTerminal()
|
||||
..feed('1\x1bPq#0;2;0;0;0#0~~@@')
|
||||
..feed('\x1b')
|
||||
..feed('\\2');
|
||||
expect(t.row(0), '12');
|
||||
});
|
||||
|
||||
test('CAN aborts a control string', () {
|
||||
final t = FixtureTerminal()..feed('\x1bPjunk\x18ok');
|
||||
expect(t.row(0), 'ok');
|
||||
});
|
||||
|
||||
test('DECRQSS m reports the current SGR', () {
|
||||
final t = FixtureTerminal()..feed('\x1b[1;4:3;38;5;196;48;2;1;2;3m');
|
||||
t.takeReplies();
|
||||
t.feed('\x1bP\$qm\x1b\\');
|
||||
expect(t.takeReplies(), '\x1bP1\$r0;1;4:3;38;5;196;48;2;1;2;3m\x1b\\');
|
||||
expect(t.row(0), '');
|
||||
});
|
||||
|
||||
test('DECRQSS r reports margins; unknown requests are refused', () {
|
||||
final t = FixtureTerminal()..feed('\x1b[2;4r');
|
||||
t.feed('\x1bP\$qr\x1b\\');
|
||||
expect(t.takeReplies(), '\x1bP1\$r2;4r\x1b\\');
|
||||
t.feed('\x1bP\$qbogus\x1b\\');
|
||||
expect(t.takeReplies(), '\x1bP0\$r\x1b\\');
|
||||
});
|
||||
|
||||
test('a DCS other than DECRQSS sends nothing', () {
|
||||
final t = FixtureTerminal()..feed('\x1bP+q544e\x1b\\');
|
||||
expect(t.takeReplies(), '');
|
||||
});
|
||||
}
|
||||
@@ -39,6 +39,12 @@ abstract class EscapeHandler {
|
||||
|
||||
void unkownEscape(int char);
|
||||
|
||||
/* DCS */
|
||||
|
||||
/// `DCS $ q Pt ST` (DECRQSS): report the setting named by [setting], such
|
||||
/// as `m` (SGR), `r` (margins) or ` q` (cursor style).
|
||||
void requestStatusString(String setting);
|
||||
|
||||
/* CSI */
|
||||
|
||||
void repeatPreviousCharacter(int n);
|
||||
|
||||
+84
-1
@@ -11,7 +11,8 @@ import 'package:xterm/src/utils/lookup_table.dart';
|
||||
///
|
||||
/// Design goals:
|
||||
/// * Zero object allocation during processing.
|
||||
/// * No internal state. Same input will always produce same output.
|
||||
/// * Little internal state: only an open control string (DCS, SOS, PM,
|
||||
/// APC) is remembered between writes, so its body is never re-scanned.
|
||||
class EscapeParser {
|
||||
final EscapeHandler handler;
|
||||
|
||||
@@ -33,6 +34,11 @@ class EscapeParser {
|
||||
|
||||
void _process() {
|
||||
while (_queue.isNotEmpty) {
|
||||
if (_stringKind != 0) {
|
||||
if (!_consumeControlString()) return;
|
||||
continue;
|
||||
}
|
||||
|
||||
tokenBegin = _queue.totalConsumed;
|
||||
final char = _queue.consume();
|
||||
|
||||
@@ -69,6 +75,14 @@ class EscapeParser {
|
||||
if (_queue.isEmpty) return false;
|
||||
|
||||
final escapeChar = _queue.consume();
|
||||
|
||||
// `ESC ESC`: the first escape is abandoned and the second one starts
|
||||
// the sequence.
|
||||
if (escapeChar == Ascii.ESC) {
|
||||
_queue.rollback(1);
|
||||
return true;
|
||||
}
|
||||
|
||||
final escapeHandler = _escHandlers[escapeChar];
|
||||
|
||||
if (escapeHandler == null) {
|
||||
@@ -109,8 +123,77 @@ class EscapeParser {
|
||||
// '+'.charCode: _voidHandler(1), // TODO: G3 (vt220)
|
||||
'>'.charCode: _escHandleResetAppKeypadMode, // TODO: Normal Keypad
|
||||
'='.charCode: _escHandleSetAppKeypadMode, // TODO: Application Keypad
|
||||
'P'.charCode: () => _beginControlString(Ascii.P), // DCS
|
||||
'X'.charCode: () => _beginControlString(Ascii.X), // SOS
|
||||
'^'.charCode: () => _beginControlString(Ascii.caret), // PM
|
||||
'_'.charCode: () => _beginControlString(Ascii.underscore), // APC
|
||||
});
|
||||
|
||||
/// The introducer (`P`, `X`, `^` or `_`) of the control string being
|
||||
/// consumed, or 0 outside one. A control string spans writes, so this is
|
||||
/// the parser's one piece of state: a long body (sixel, tmux passthrough)
|
||||
/// is consumed as it arrives instead of being re-scanned.
|
||||
var _stringKind = 0;
|
||||
|
||||
/// The start of a DCS body, kept to answer requests such as DECRQSS.
|
||||
final _stringBody = StringBuffer();
|
||||
|
||||
static const _maxStringBody = 256;
|
||||
|
||||
bool _beginControlString(int kind) {
|
||||
_stringKind = kind;
|
||||
_stringBody.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Consumes control string bytes up to the String Terminator `ESC \`.
|
||||
/// The body is never printed. CAN or SUB abort the string; an ESC that
|
||||
/// does not start ST aborts it and starts a new escape sequence. Returns
|
||||
/// false when more input is needed.
|
||||
bool _consumeControlString() {
|
||||
while (_queue.isNotEmpty) {
|
||||
final char = _queue.consume();
|
||||
|
||||
if (char == Ascii.ESC) {
|
||||
if (_queue.isEmpty) {
|
||||
_queue.rollback(1);
|
||||
return false;
|
||||
}
|
||||
if (_queue.peek() == Ascii.backslash) {
|
||||
_queue.consume();
|
||||
_finishControlString(dispatch: true);
|
||||
} else {
|
||||
_queue.rollback(1);
|
||||
_finishControlString(dispatch: false);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (char == Ascii.CAN || char == Ascii.SUB) {
|
||||
_finishControlString(dispatch: false);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_stringKind == Ascii.P && _stringBody.length < _maxStringBody) {
|
||||
_stringBody.writeCharCode(char);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void _finishControlString({required bool dispatch}) {
|
||||
final kind = _stringKind;
|
||||
_stringKind = 0;
|
||||
if (!dispatch || kind != Ascii.P) return;
|
||||
|
||||
final body = _stringBody.toString();
|
||||
_stringBody.clear();
|
||||
// DECRQSS: `DCS $ q Pt ST` requests the setting named by Pt.
|
||||
if (body.startsWith(r'$q')) {
|
||||
handler.requestStatusString(body.substring(2));
|
||||
}
|
||||
}
|
||||
|
||||
/// `ESC 7` Save Cursor (DECSC)
|
||||
///
|
||||
/// https://terminalguide.namepad.de/seq/a_esc_a7/
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import 'package:xterm/src/core/cell.dart';
|
||||
import 'package:xterm/src/core/cursor.dart';
|
||||
|
||||
/// The SGR parameters that recreate [style], for a DECRQSS `m` reply:
|
||||
/// `0;1;38;5;196` and so on, always starting with a reset.
|
||||
String sgrReport(CursorStyle style) {
|
||||
final params = <String>['0'];
|
||||
if (style.isBold) params.add('1');
|
||||
if (style.isFaint) params.add('2');
|
||||
if (style.isItalis) params.add('3');
|
||||
switch (style.underlineStyle) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
params.add('4');
|
||||
default:
|
||||
params.add('4:${style.underlineStyle}');
|
||||
}
|
||||
if (style.isBlink) params.add('5');
|
||||
if (style.isInverse) params.add('7');
|
||||
if (style.isInvisible) params.add('8');
|
||||
if (style.isStrikethrough) params.add('9');
|
||||
if (style.isOverline) params.add('53');
|
||||
_color(params, style.foreground, 30, 90, 38);
|
||||
_color(params, style.background, 40, 100, 48);
|
||||
return params.join(';');
|
||||
}
|
||||
|
||||
void _color(List<String> params, int color, int base, int bright, int ext) {
|
||||
final value = color & CellColor.valueMask;
|
||||
switch (color & CellColor.typeMask) {
|
||||
case CellColor.named:
|
||||
params.add('${value < 8 ? base + value : bright + value - 8}');
|
||||
case CellColor.palette:
|
||||
params.add('$ext;5;$value');
|
||||
case CellColor.rgb:
|
||||
params
|
||||
.add('$ext;2;${value >> 16};${(value >> 8) & 0xFF};${value & 0xFF}');
|
||||
}
|
||||
}
|
||||
+17
@@ -8,6 +8,7 @@ import 'package:xterm/src/core/cursor.dart';
|
||||
import 'package:xterm/src/core/escape/emitter.dart';
|
||||
import 'package:xterm/src/core/escape/handler.dart';
|
||||
import 'package:xterm/src/core/escape/parser.dart';
|
||||
import 'package:xterm/src/core/escape/status_report.dart';
|
||||
import 'package:xterm/src/core/input/handler.dart';
|
||||
import 'package:xterm/src/core/input/keys.dart';
|
||||
import 'package:xterm/src/core/mouse/button.dart';
|
||||
@@ -481,6 +482,22 @@ class Terminal with Observable implements TerminalState, EscapeHandler {
|
||||
// no-op
|
||||
}
|
||||
|
||||
/* DCS */
|
||||
|
||||
@override
|
||||
void requestStatusString(String setting) {
|
||||
final value = switch (setting) {
|
||||
'm' => '${sgrReport(_cursorStyle)}m',
|
||||
'r' => '${_buffer.marginTop + 1};${_buffer.marginBottom + 1}r',
|
||||
_ => null,
|
||||
};
|
||||
// `DCS 1 $ r Pt ST` reports a valid setting, `DCS 0 $ r ST` an
|
||||
// unrecognised request.
|
||||
onOutput?.call(
|
||||
value == null ? '\x1bP0\$r\x1b\\' : '\x1bP1\$r$value\x1b\\',
|
||||
);
|
||||
}
|
||||
|
||||
/* CSI */
|
||||
|
||||
@override
|
||||
|
||||
@@ -471,6 +471,11 @@ class _TerminalDebuggerHandler implements EscapeHandler {
|
||||
onCommand('setCursorUnderline');
|
||||
}
|
||||
|
||||
@override
|
||||
void requestStatusString(String setting) {
|
||||
onCommand('requestStatusString $setting');
|
||||
}
|
||||
|
||||
@override
|
||||
void setCursorUnderlineStyle(int style) {
|
||||
onCommand('setCursorUnderlineStyle $style');
|
||||
|
||||
Reference in New Issue
Block a user