Merge branch 'worktree-agent-acd04dfbcf1f35152'

This commit is contained in:
2026-11-25 23:45:40 +00:00
103 changed files with 15814 additions and 5 deletions
@@ -0,0 +1,75 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
import 'paint_fixture.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final size = fixturePainter().cellSize;
final w = size.width.ceil();
final h = size.height.ceil();
Future<Pixels> cell(CellData data) => Pixels.paint(
w,
h * 2, // room for decorations drawn below the cell
(canvas) => fixturePainter().paintCell(canvas, Offset.zero, data),
);
/// The rows of the cell that contain painted pixels.
Future<List<int>> inkRows(CellData data) async {
final pixels = await cell(data);
return [
for (var y = 0; y < h * 2; y++)
if (pixels.rowHasInk(y)) y,
];
}
test('a plain space draws nothing', () async {
expect(await inkRows(cellOf(' ')), isEmpty);
});
test('strikethrough (SGR 9) draws a line through the middle', () async {
final rows = await inkRows(cellOf(' ', flags: CellAttr.strikethrough));
expect(rows, isNotEmpty);
expect(rows.every((y) => y > h * 0.2 && y < h * 0.8), isTrue);
});
test('overline (SGR 53) draws at the top, underline at the bottom', () async {
final over = await inkRows(cellOf(' ', flags: CellAttr.overline));
expect(over, isNotEmpty);
expect(over.first, lessThan(h * 0.3));
final under = await inkRows(cellOf(' ', flags: CellAttr.underline));
expect(under, isNotEmpty);
expect(under.last, greaterThan(h * 0.6));
});
test('double underline (SGR 21) draws two lines', () async {
const doubled = CellAttr.underline | (2 << CellAttr.underlineStyleShift);
final single = await inkRows(cellOf(' ', flags: CellAttr.underline));
final two = await inkRows(cellOf(' ', flags: doubled));
expect(two.length, greaterThan(single.length));
});
test(
'concealed text (SGR 8) keeps its background, hides the glyph',
() async {
final shown = await cell(cellOf('M'));
expect(shown.count(foregroundColor), greaterThan(0));
final hidden = await cell(cellOf('M', flags: CellAttr.invisible));
expect(hidden.count(foregroundColor), 0);
final inverse = await cell(
cellOf('M', flags: CellAttr.invisible | CellAttr.inverse),
);
expect(
inverse.count(foregroundColor),
greaterThan(0),
reason: 'background',
);
expect(inverse.count(backgroundColor), 0, reason: 'no glyph');
},
);
}
@@ -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(), '');
});
}
@@ -0,0 +1,65 @@
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('DSR 6 reports the one-based cursor position', () {
final t = FixtureTerminal()..feed('\x1b[3;7H\x1b[6n');
expect(t.takeReplies(), '\x1b[3;7R');
});
test('zero counts mean one', () {
final t = FixtureTerminal()..feed('\x1b[0C\x1b[0B');
expect(t.buffer.cursorX, 1);
expect(t.buffer.cursorY, 1);
});
}
@@ -0,0 +1,97 @@
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
import 'paint_fixture.dart';
import 'xterm_fixture.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('DECSCUSR', () {
test('stores the requested shape and blinking', () {
final t = FixtureTerminal();
expect(t.terminal.cursorShape, isNull);
const expected = {
1: (TerminalCursorType.block, true),
2: (TerminalCursorType.block, false),
3: (TerminalCursorType.underline, true),
4: (TerminalCursorType.underline, false),
5: (TerminalCursorType.verticalBar, true),
6: (TerminalCursorType.verticalBar, false),
};
for (final MapEntry(key: ps, value: (shape, blinks))
in expected.entries) {
t.feed('\x1b[$ps q');
expect(t.terminal.cursorShape, shape, reason: 'Ps $ps');
expect(t.terminal.cursorShapeBlinks, blinks, reason: 'Ps $ps');
}
t.feed('\x1b[0 q');
expect(t.terminal.cursorShape, isNull);
t.feed('\x1b[6 q\x1bc');
expect(t.terminal.cursorShape, isNull);
});
test('does not print and is reported by DECRQSS', () {
final t = FixtureTerminal()..feed('\x1b[5 qa');
expect(t.row(0), 'a');
t.feed('\x1bP\$q q\x1b\\');
expect(t.takeReplies(), '\x1bP1\$r5 q\x1b\\');
});
});
group('cursor painting', () {
final cell = fixturePainter().cellSize;
final w = cell.width.ceil();
final h = cell.height.ceil();
final area = Rect.fromLTWH(0, 0, cell.width, cell.height);
Future<Pixels> cursor(TerminalCursorType type, {CellData? under}) =>
Pixels.paint(
w * 2,
h,
(canvas) => fixturePainter().paintCursor(
canvas,
Offset.zero,
cursorType: type,
cell: under,
),
);
test('a block cursor draws the character under it inverted', () async {
final empty = await cursor(TerminalCursorType.block);
expect(empty.count(backgroundColor, area), 0);
expect(empty.count(cursorColor, area), greaterThan(0));
final withChar = await cursor(
TerminalCursorType.block,
under: cellOf('M'),
);
// The test font's glyphs fill the whole cell; before the fix the
// block hid them and every pixel was the cursor colour.
expect(
withChar.count(backgroundColor, area),
greaterThan(0),
reason: 'the glyph is drawn in the background colour',
);
});
test('a block cursor over a wide character spans two cells', () async {
final pixels = await cursor(
TerminalCursorType.block,
under: cellOf('中', width: 2),
);
expect(pixels.at(w + w ~/ 2, 0), cursorColor);
});
test('underline and bar cursors sit in the cell, not at the top', () async {
final underline = await cursor(TerminalCursorType.underline);
expect(underline.at(w ~/ 2, h - 1), cursorColor);
expect(underline.at(w ~/ 2, 0), isNot(cursorColor));
final bar = await cursor(TerminalCursorType.verticalBar);
expect(bar.at(0, h ~/ 2), cursorColor);
expect(bar.at(w - 1, h ~/ 2), isNot(cursorColor));
});
});
}
@@ -0,0 +1,46 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
import 'xterm_fixture.dart';
void main() {
test('focusInput sends CSI I / CSI O only with ?1004 set', () {
final t = FixtureTerminal();
expect(t.terminal.focusInput(true), isFalse);
expect(t.takeReplies(), '');
t.feed('\x1b[?1004h');
expect(t.terminal.focusInput(true), isTrue);
t.terminal.focusInput(false);
expect(t.takeReplies(), '\x1b[I\x1b[O');
t.feed('\x1b[?1004l');
t.terminal.focusInput(true);
expect(t.takeReplies(), '');
});
testWidgets('TerminalView reports its focus changes', (tester) async {
final terminal = Terminal();
final replies = <String>[];
terminal.onOutput = replies.add;
final focus = FocusNode();
addTearDown(focus.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: TerminalView(
terminal,
focusNode: focus,
hardwareKeyboardOnly: true,
),
),
),
);
terminal.write('\x1b[?1004h');
focus.requestFocus();
await tester.pump();
focus.unfocus();
await tester.pump();
expect(replies.join(), '\x1b[I\x1b[O');
});
}
@@ -0,0 +1,45 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
import 'xterm_fixture.dart';
void main() {
test('leaving the 1049 alternate screen restores the saved cursor', () {
final t = FixtureTerminal()..feed('main\x1b[3;5H\x1b[31m');
t.feed('\x1b[?1049h');
expect(t.terminal.isUsingAltBuffer, isTrue);
t.feed('\x1b[1;1H\x1b[0malt screen');
t.feed('\x1b[?1049l');
expect(t.terminal.isUsingAltBuffer, isFalse);
expect(t.buffer.cursorX, 4);
expect(t.buffer.cursorY, 2);
expect(t.row(0), 'main');
t.feed('z');
expect(t.cell(4, 2).foreground, named(NamedColor.red));
});
test('DECCKM (CSI ? 1 h/l) is its own mode, not the keypad mode', () {
final t = FixtureTerminal();
expect(t.terminal.cursorKeysMode, isFalse);
t.feed('\x1b[?1h');
expect(t.terminal.cursorKeysMode, isTrue);
expect(t.terminal.appKeypadMode, isFalse);
t.feed('\x1b[?1l\x1b=');
expect(t.terminal.cursorKeysMode, isFalse);
expect(t.terminal.appKeypadMode, isTrue);
});
test('arrow keys follow DECCKM, not DECKPAM', () {
final t = FixtureTerminal();
t.terminal.keyInput(TerminalKey.arrowUp);
expect(t.takeReplies(), '\x1b[A');
t.feed('\x1b=');
t.terminal.keyInput(TerminalKey.arrowUp);
expect(t.takeReplies(), '\x1b[A');
t.feed('\x1b>\x1b[?1h');
t.terminal.keyInput(TerminalKey.arrowUp);
expect(t.takeReplies(), '\x1bOA');
});
}
@@ -0,0 +1,105 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
import 'xterm_fixture.dart';
void main() {
const down = TerminalMouseButtonState.down;
const up = TerminalMouseButtonState.up;
const at = CellOffset(4, 2); // column 5, row 3 one-based
String report(
String modes,
TerminalMouseButton button,
TerminalMouseButtonState state, {
bool motion = false,
bool ctrl = false,
}) {
final t = FixtureTerminal()..feed(modes);
t.terminal.mouseInput(button, state, at, motion: motion, ctrl: ctrl);
return t.takeReplies();
}
test('SGR wheel is 64 up and 65 down', () {
const sgr = '\x1b[?1000h\x1b[?1006h';
expect(report(sgr, TerminalMouseButton.wheelUp, down), '\x1b[<64;5;3M');
expect(report(sgr, TerminalMouseButton.wheelDown, down), '\x1b[<65;5;3M');
expect(report(sgr, TerminalMouseButton.wheelUp, up), '');
});
test('default encoding: 32 + code, 32 + column, 32 + row', () {
const normal = '\x1b[?1000h';
expect(
report(normal, TerminalMouseButton.left, down),
'\x1b[M\x20\x25\x23',
);
expect(report(normal, TerminalMouseButton.left, up), '\x1b[M\x23\x25\x23');
expect(
report(normal, TerminalMouseButton.wheelDown, down),
'\x1b[M\x61\x25\x23',
);
});
test('SGR reports which button was released', () {
const sgr = '\x1b[?1000h\x1b[?1006h';
expect(report(sgr, TerminalMouseButton.middle, down), '\x1b[<1;5;3M');
expect(report(sgr, TerminalMouseButton.middle, up), '\x1b[<1;5;3m');
expect(report(sgr, TerminalMouseButton.right, up), '\x1b[<2;5;3m');
});
test('modifiers add 4/8/16', () {
const sgr = '\x1b[?1000h\x1b[?1006h';
expect(
report(sgr, TerminalMouseButton.left, down, ctrl: true),
'\x1b[<16;5;3M',
);
});
group('motion', () {
test('1000 reports no motion', () {
expect(
report(
'\x1b[?1000h\x1b[?1006h',
TerminalMouseButton.left,
down,
motion: true,
),
'',
);
});
test('1002 reports drags with the button held, plus 32', () {
const drag = '\x1b[?1002h\x1b[?1006h';
expect(
report(drag, TerminalMouseButton.left, down, motion: true),
'\x1b[<32;5;3M',
);
expect(report(drag, TerminalMouseButton.none, down, motion: true), '');
});
test('1003 reports motion without a button as 35', () {
expect(
report(
'\x1b[?1003h\x1b[?1006h',
TerminalMouseButton.none,
down,
motion: true,
),
'\x1b[<35;5;3M',
);
expect(
report('\x1b[?1003h', TerminalMouseButton.none, down, motion: true),
'\x1b[M\x43\x25\x23',
);
});
});
test('X10 mode (9) reports presses only', () {
expect(
report('\x1b[?9h', TerminalMouseButton.left, down),
'\x1b[M\x20\x25\x23',
);
expect(report('\x1b[?9h', TerminalMouseButton.left, up), '');
expect(report('\x1b[?9h', TerminalMouseButton.wheelUp, down), '');
});
}
@@ -0,0 +1,159 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/xterm.dart';
/// A TerminalView placed away from the window's origin, so a report that
/// used window coordinates instead of the view's own would be wrong.
class _Harness {
_Harness(this.tester);
final WidgetTester tester;
final terminal = Terminal();
final controller = TerminalController();
final replies = <String>[];
Future<void> pump() async {
terminal.onOutput = replies.add;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Padding(
padding: const EdgeInsets.only(left: 100, top: 60),
child: SizedBox(
width: 400,
height: 200,
child: TerminalView(
terminal,
controller: controller,
hardwareKeyboardOnly: true,
),
),
),
),
),
);
}
TerminalViewState get _view =>
tester.state<TerminalViewState>(find.byType(TerminalView));
/// The global centre of zero-based screen cell ([x], [y]).
Offset cell(int x, int y) {
final render = _view.renderTerminal;
final size = render.cellSize;
return render.localToGlobal(
Offset((x + 0.5) * size.width, (y + 0.5) * size.height),
);
}
double get lineHeight => _view.renderTerminal.lineHeight;
String take() {
final all = replies.join();
replies.clear();
return all;
}
}
void main() {
testWidgets('a left drag with 1002 is reported, not selected', (
tester,
) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('\x1b[?1002h\x1b[?1006h');
await tester.pump();
final gesture = await tester.startGesture(
h.cell(1, 1),
kind: PointerDeviceKind.mouse,
);
await gesture.moveTo(h.cell(4, 1));
await gesture.moveTo(h.cell(6, 2));
await gesture.up();
await tester.pumpAndSettle();
expect(h.take(), '\x1b[<0;2;2M\x1b[<32;5;2M\x1b[<32;7;3M\x1b[<0;7;3m');
expect(h.controller.selection, isNull);
});
testWidgets('1003 reports plain motion; 1000 does not', (tester) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('\x1b[?1003h\x1b[?1006h');
final gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer(location: h.cell(0, 0));
await gesture.moveTo(h.cell(3, 2));
expect(h.take(), '\x1b[<35;4;3M');
h.terminal.write('\x1b[?1003l\x1b[?1000h');
await gesture.moveTo(h.cell(5, 2));
expect(h.take(), '');
await gesture.removePointer();
});
testWidgets('a middle click is reported as middle press and release', (
tester,
) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('\x1b[?1000h\x1b[?1006h');
final gesture = await tester.startGesture(
h.cell(2, 0),
kind: PointerDeviceKind.mouse,
buttons: kMiddleMouseButton,
);
await gesture.up();
await tester.pumpAndSettle();
expect(h.take(), '\x1b[<1;3;1M\x1b[<1;3;1m');
});
testWidgets('Shift+drag selects text even while the program has the mouse', (
tester,
) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('hello world\x1b[?1002h\x1b[?1006h');
await tester.pump();
await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft);
final gesture = await tester.startGesture(
h.cell(0, 0),
kind: PointerDeviceKind.mouse,
);
await gesture.moveTo(h.cell(2, 0));
await gesture.moveTo(h.cell(4, 0));
await gesture.up();
await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft);
await tester.pumpAndSettle();
expect(h.take(), '');
final selection = h.controller.selection;
expect(selection, isNotNull);
expect(h.terminal.buffer.getText(selection), startsWith('hell'));
});
testWidgets('the wheel reports 65 down / 64 up at the view cell', (
tester,
) async {
final h = _Harness(tester);
await h.pump();
h.terminal.write('\x1b[?1049h\x1b[?1000h\x1b[?1006h');
await tester.pump();
final pointer = TestPointer(1, PointerDeviceKind.mouse);
final at = h.cell(3, 1);
await tester.sendEventToBinding(pointer.hover(at));
await tester.sendEventToBinding(
pointer.scroll(Offset(0, h.lineHeight * 2)),
);
await tester.pump();
expect(h.take(), '\x1b[<65;4;2M\x1b[<65;4;2M');
await tester.sendEventToBinding(pointer.scroll(Offset(0, -h.lineHeight)));
await tester.pump();
expect(h.take(), '\x1b[<64;4;2M');
});
}
+105
View File
@@ -0,0 +1,105 @@
import 'package:flutter_test/flutter_test.dart';
import 'xterm_fixture.dart';
void main() {
group('OSC 10/11/12 colour queries', () {
FixtureTerminal themed() {
final t = FixtureTerminal();
t.terminal.setReportedColors(
foreground: 0xFFC0C0C0,
background: 0xFF102030,
cursor: 0xFFFF8000,
);
return t;
}
test('answer with the theme colours and the query terminator', () {
final t = themed()..feed('\x1b]11;?\x1b\\');
expect(t.takeReplies(), '\x1b]11;rgb:1010/2020/3030\x1b\\');
t.feed('\x1b]10;?\x07');
expect(t.takeReplies(), '\x1b]10;rgb:c0c0/c0c0/c0c0\x07');
t.feed('\x1b]12;?\x07');
expect(t.takeReplies(), '\x1b]12;rgb:ffff/8080/0000\x07');
});
test('a multi-colour query answers each colour in turn', () {
final t = themed()..feed('\x1b]10;?;?\x07');
expect(
t.takeReplies(),
'\x1b]10;rgb:c0c0/c0c0/c0c0\x07\x1b]11;rgb:1010/2020/3030\x07',
);
});
test('setting a colour sends nothing and prints nothing', () {
final t = themed()..feed('\x1b]11;#000000\x07x');
expect(t.takeReplies(), '');
expect(t.row(0), 'x');
});
});
group('OSC 8 hyperlinks', () {
test('link cells without printing the sequence', () {
final t = FixtureTerminal()
..feed('a\x1b]8;id=1;https://example.com/a;b\x1b\\link\x1b]8;;\x1b\\z');
expect(t.row(0), 'alinkz');
final line = t.buffer.lines[0];
expect(t.terminal.hyperlinkAt(line, 0), isNull);
for (var x = 1; x <= 4; x++) {
expect(t.terminal.hyperlinkAt(line, x), 'https://example.com/a;b');
}
expect(t.terminal.hyperlinkAt(line, 5), isNull);
});
test('a link survives SGR changes and ends with an empty URI', () {
final t = FixtureTerminal()
..feed('\x1b]8;;https://x.test\x07\x1b[1ma\x1b[0mb\x1b]8;;\x07c');
final line = t.buffer.lines[0];
expect(t.terminal.hyperlinkAt(line, 0), 'https://x.test');
expect(t.terminal.hyperlinkAt(line, 1), 'https://x.test');
expect(t.terminal.hyperlinkAt(line, 2), isNull);
expect(t.cell(1, 0).flags & 0xFFFF, 0, reason: 'no SGR attributes');
});
});
group('OSC 52 clipboard', () {
test('a set reaches the callback decoded', () {
final t = FixtureTerminal();
final sets = <(String, String)>[];
t.terminal.onClipboardSet = (text, selection) =>
sets.add((text, selection));
t.feed('\x1b]52;c;aGVsbG8gd8O2cmxk\x07x');
expect(sets, [('hello wörld', 'c')]);
expect(t.row(0), 'x');
});
test('a read request and invalid data are ignored', () {
final t = FixtureTerminal();
var calls = 0;
t.terminal.onClipboardSet = (_, _) => calls++;
t.feed('\x1b]52;c;?\x07\x1b]52;c;not base64!\x07');
expect(calls, 0);
expect(t.takeReplies(), '');
});
});
test('OSC 7 reports the working directory', () {
final t = FixtureTerminal();
final dirs = <String>[];
t.terminal.onWorkingDirectoryChange = dirs.add;
t.feed('\x1b]7;file://host/home/me/a;b\x1b\\');
expect(dirs, ['file://host/home/me/a;b']);
expect(t.row(0), '');
});
test('titles keep their semicolons; an aborted OSC prints nothing', () {
final t = FixtureTerminal();
final titles = <String>[];
t.terminal.onTitleChange = titles.add;
t.feed('\x1b]2;vim; a.txt\x07');
t.feed('\x1b]2;cut\x1b[1mB');
expect(titles, ['vim; a.txt']);
expect(t.row(0), 'B');
expect(t.cell(0, 0).flags, isNot(0));
});
}
@@ -0,0 +1,107 @@
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/painting.dart';
import 'package:xterm/src/ui/painter.dart';
import 'package:xterm/xterm.dart';
const cursorColor = Color(0xFFFF0000);
const backgroundColor = Color(0xFF0000FF);
const foregroundColor = Color(0xFF00FF00);
/// A theme whose cursor, background and foreground are pure red, blue and
/// green, so painted pixels say which of them they are.
const fixtureTheme = TerminalTheme(
cursor: cursorColor,
selection: Color(0x80FFFFFF),
foreground: foregroundColor,
background: backgroundColor,
black: Color(0xFF000000),
red: Color(0xFF800000),
green: Color(0xFF008000),
yellow: Color(0xFF808000),
blue: Color(0xFF000080),
magenta: Color(0xFF800080),
cyan: Color(0xFF008080),
white: Color(0xFFC0C0C0),
brightBlack: Color(0xFF808080),
brightRed: Color(0xFFFF8080),
brightGreen: Color(0xFF80FF80),
brightYellow: Color(0xFFFFFF80),
brightBlue: Color(0xFF8080FF),
brightMagenta: Color(0xFFFF80FF),
brightCyan: Color(0xFF80FFFF),
brightWhite: Color(0xFFFFFFFF),
searchHitBackground: Color(0xFFFFFF00),
searchHitBackgroundCurrent: Color(0xFFFF8000),
searchHitForeground: Color(0xFF000000),
);
TerminalPainter fixturePainter() => TerminalPainter(
theme: fixtureTheme,
textStyle: const TerminalStyle(fontSize: 20, height: 1),
textScaler: TextScaler.noScaling,
);
/// The pixels [paint] draws on a transparent canvas of [width] x [height].
class Pixels {
Pixels._(this.width, this.height, this._rgba);
final int width;
final int height;
final ByteData _rgba;
static Future<Pixels> paint(
int width,
int height,
void Function(Canvas canvas) paint,
) async {
final recorder = ui.PictureRecorder();
paint(Canvas(recorder));
final image = await recorder.endRecording().toImage(width, height);
final data = await image.toByteData(format: ui.ImageByteFormat.rawRgba);
image.dispose();
return Pixels._(width, height, data!);
}
/// The colour at ([x], [y]), opaque or transparent.
Color at(int x, int y) {
final i = (y * width + x) * 4;
return Color.fromARGB(
_rgba.getUint8(i + 3),
_rgba.getUint8(i),
_rgba.getUint8(i + 1),
_rgba.getUint8(i + 2),
);
}
/// Whether row [y] has any painted (non-transparent) pixel; decorations
/// are antialiased, so their exact colour varies.
bool rowHasInk(int y) {
for (var x = 0; x < width; x++) {
if (_rgba.getUint8((y * width + x) * 4 + 3) != 0) return true;
}
return false;
}
/// How many pixels in [rect] are exactly [color].
int count(Color color, [Rect? rect]) {
final area =
rect ?? Rect.fromLTWH(0, 0, width.toDouble(), height.toDouble());
var n = 0;
for (var y = area.top.floor(); y < area.bottom.ceil(); y++) {
for (var x = area.left.floor(); x < area.right.ceil(); x++) {
if (at(x, y) == color) n++;
}
}
return n;
}
}
/// A cell holding [char] with [flags] and default colours.
CellData cellOf(String char, {int flags = 0, int width = 1}) => CellData(
foreground: 0,
background: 0,
flags: flags,
content: char.codeUnitAt(0) | (width << CellContent.widthShift),
);
@@ -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);
});
}
+98
View File
@@ -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);
});
}
@@ -0,0 +1,47 @@
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 = <String>[];
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 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();
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;