Merge branch 'worktree-agent-a9a25296deadb0eaa'

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 01:15:53 +00:00
30 changed files with 4077 additions and 99 deletions
@@ -0,0 +1,311 @@
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/src/ui/painter.dart';
import 'package:xterm/src/ui/row_pictures.dart';
import 'package:xterm/xterm.dart';
import 'paint_fixture.dart';
/// Box drawing, block and Powerline characters are geometry that fills the
/// cell, so they join across cells and rows; text runs land on the grid.
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final painter = fixturePainter();
final w = painter.cellSize.width.round();
final h = painter.cellSize.height.round();
/// A line of [cols] cells written with [text] by a terminal.
BufferLine lineOf(String text, {int cols = 6}) {
final terminal = Terminal(maxLines: 100)..resize(cols, 5);
terminal.write(text);
return terminal.buffer.lines[0];
}
/// [lines] painted one under the other.
Future<Pixels> paintLines(List<BufferLine> lines, {int cols = 6}) =>
Pixels.paint(cols * w, lines.length * h, (canvas) {
for (var i = 0; i < lines.length; i++) {
painter.paintLine(canvas, Offset(0, i * h.toDouble()), lines[i]);
}
});
Rect cellRect(int x, int y) => Rect.fromLTWH(
x * w.toDouble(),
y * h.toDouble(),
w.toDouble(),
h.toDouble(),
);
test('cells are whole pixels high', () {
final odd = TerminalPainter(
theme: fixtureTheme,
textStyle: const TerminalStyle(fontSize: 13, height: 1.15),
textScaler: TextScaler.noScaling,
);
final height = odd.cellSize.height;
expect(height, height.roundToDouble());
});
test('a full block fills its cell exactly', () async {
final pixels = await paintLines([lineOf('\u2588')]);
expect(pixels.count(foregroundColor, cellRect(0, 0)), w * h);
expect(pixels.count(foregroundColor, cellRect(1, 0)), 0);
});
test('half and eighth blocks fill their part of the cell', () async {
final pixels = await paintLines([lineOf('\u2580\u2584\u258C\u2581')]);
expect(pixels.count(foregroundColor, cellRect(0, 0)), w * h ~/ 2);
expect(pixels.at(w ~/ 2, 0), foregroundColor); // upper half
expect(pixels.at(w + w ~/ 2, h - 1), foregroundColor); // lower half
expect(pixels.at(w + w ~/ 2, 0).a, 0);
expect(pixels.at(2 * w, h ~/ 2), foregroundColor); // left half
expect(pixels.at(3 * w - 1, h ~/ 2).a, 0);
// The lower eighth: whole pixel rows, about an eighth of the height.
final rows = [
for (var y = 0; y < h; y++)
if (pixels.count(
foregroundColor,
Rect.fromLTWH(3.0 * w, y.toDouble(), w.toDouble(), 1),
) ==
w)
y,
];
expect(rows.length, closeTo(h / 8, 1));
expect(rows.last, h - 1);
for (var y = 0; y < h; y++) {
if (!rows.contains(y)) expect(pixels.at(3 * w + w ~/ 2, y).a, 0);
}
});
test('a horizontal line runs unbroken across cells', () async {
final pixels = await paintLines([lineOf('\u2500\u2500\u2500')]);
final row = [
for (var y = 0; y < h; y++)
if (pixels.at(1, y) == foregroundColor) y,
];
expect(row, isNotEmpty);
for (var x = 0; x < 3 * w; x++) {
expect(pixels.at(x, row.first), foregroundColor, reason: 'x=$x');
}
});
test('a vertical line runs unbroken across rows', () async {
final pixels = await paintLines([lineOf('\u2502'), lineOf('\u2502')]);
final column = [
for (var x = 0; x < w; x++)
if (pixels.at(x, 1) == foregroundColor) x,
];
expect(column, isNotEmpty);
for (var y = 0; y < 2 * h; y++) {
expect(pixels.at(column.first, y), foregroundColor, reason: 'y=$y');
}
});
test('corners and tees join the lines they meet', () async {
// \u250C\u2500\u2510 over \u2502 \u2502: the corner's arms reach the cell edges.
final pixels = await paintLines([
lineOf('\u250C\u2500\u2510'),
lineOf('\u2502 \u2502'),
]);
final lineY = [
for (var y = 0; y < h; y++)
if (pixels.at(w + 1, y) == foregroundColor) y,
].first;
final lineX = [
for (var x = 0; x < w; x++)
if (pixels.at(x, h + 1) == foregroundColor) x,
].first;
// The corner's right arm reaches its right edge, the down arm its bottom.
expect(pixels.at(w - 1, lineY), foregroundColor);
expect(pixels.at(lineX, h - 1), foregroundColor);
// Nothing left of or above the corner's elbow.
expect(pixels.at(0, lineY).a, 0);
expect(pixels.at(lineX, 0).a, 0);
});
test('double lines draw two strokes', () async {
final pixels = await paintLines([lineOf('\u2550')]);
final rows = [
for (var y = 0; y < h; y++)
if (pixels.at(w ~/ 2, y) == foregroundColor) y,
];
// Two separate strokes: a gap between the first and last painted rows.
expect(rows.length, lessThan(rows.last - rows.first + 1));
for (var x = 0; x < w; x++) {
expect(pixels.at(x, rows.first), foregroundColor);
expect(pixels.at(x, rows.last), foregroundColor);
}
});
test('a Powerline arrow fills the cell height at its base', () async {
final pixels = await paintLines([lineOf('\uE0B0\uE0B2')]);
// Right-pointing: the whole left column; left-pointing: the right (the
// top and bottom pixels are the antialiased points of the triangle).
for (var y = 1; y < h - 1; y++) {
expect(pixels.at(0, y), foregroundColor, reason: 'y=$y');
expect(pixels.at(2 * w - 1, y), foregroundColor, reason: 'y=$y');
}
expect(pixels.at(0, 0).a, greaterThan(0.4));
expect(pixels.at(0, h - 1).a, greaterThan(0.4));
// The tip reaches the far side only in the middle.
expect(pixels.at(w - 1, h ~/ 2).a, greaterThan(0));
expect(pixels.at(w - 1, 0).a, 0);
expect(pixels.at(w, 0).a, 0);
// About half the cell.
final filled = pixels.count(foregroundColor, cellRect(0, 0));
expect(filled, closeTo(w * h / 2, w * h * 0.1));
});
test('Powerline diagonals fill a triangle', () async {
final pixels = await paintLines([lineOf('\uE0B8\uE0BC')]);
expect(pixels.at(0, h - 1), foregroundColor); // lower left
expect(pixels.at(w - 2, 0).a, 0);
expect(pixels.at(w, 0), foregroundColor); // upper left
expect(pixels.at(2 * w - 1, h - 1).a, 0);
});
/// [line] drawn cell by cell with [TerminalPainter.paintCell].
Future<Pixels> paintCells(BufferLine line, int cols) =>
Pixels.paint(cols * w, h, (canvas) {
final cell = CellData.empty();
for (var x = 0; x < cols; x++) {
line.getCellData(x, cell);
painter.paintCell(canvas, Offset(x * w.toDouble(), 0), cell);
}
});
Future<void> expectSamePixels(Pixels a, Pixels b) async {
for (var y = 0; y < a.height; y++) {
for (var x = 0; x < a.width; x++) {
expect(a.at(x, y), b.at(x, y), reason: '($x, $y)');
}
}
}
test('a line of text paints exactly as cell by cell', () async {
// Long enough to be one paragraph, with spans of three styles.
const cols = 24;
final line = lineOf(
'plain \x1b[31mred\x1b[0m and \x1b[1;34mbold\x1b[0m text!',
cols: cols,
);
await expectSamePixels(
await paintLines([line], cols: cols),
await paintCells(line, cols),
);
});
test('a short line is drawn cell by cell, the same', () async {
final line = lineOf('Ab c-d');
await expectSamePixels(await paintLines([line]), await paintCells(line, 6));
});
test('text after a wide character and an emoji stays on the grid', () async {
const cols = 24;
final line = lineOf('\u4E2D\u{1F600}x text after them', cols: cols);
final pixels = await paintLines([line], cols: cols);
// Each plain character's cell is inked, the spaces are not.
for (var x = 4; x < 4 + 'x text after them'.length; x++) {
final blank = ' x text after them'[x - 3] == ' ';
final ink = pixels.count(foregroundColor, cellRect(x, 0));
expect(ink > 0, !blank, reason: 'cell $x');
}
});
test('an underline does not run under undecorated blanks', () async {
const cols = 24;
final line = lineOf(
'\x1b[4munderline\x1b[24m \x1b[4mmore text\x1b[0m',
cols: cols,
);
final pixels = await paintLines([line], cols: cols);
for (var x = 9; x < 12; x++) {
expect(pixels.count(foregroundColor, cellRect(x, 0)), 0, reason: 'x=$x');
}
await expectSamePixels(pixels, await paintCells(line, cols));
});
test('a styled run breaks where the colour changes', () async {
final pixels = await paintLines([lineOf('a\x1b[31mb\x1b[0mc')]);
expect(pixels.count(foregroundColor, cellRect(0, 0)), greaterThan(0));
expect(pixels.count(foregroundColor, cellRect(1, 0)), 0);
expect(pixels.count(fixtureTheme.red, cellRect(1, 0)), greaterThan(0));
expect(pixels.count(foregroundColor, cellRect(2, 0)), greaterThan(0));
});
test('backgrounds merge into runs that meet without a gap', () async {
final pixels = await paintLines([lineOf('\x1b[44m \x1b[0m')]);
for (var x = 0; x < 3 * w; x++) {
expect(pixels.at(x, h ~/ 2), fixtureTheme.blue, reason: 'x=$x');
}
expect(pixels.at(3 * w + 1, h ~/ 2).a, 0);
});
test('a grapheme cluster is drawn in its own cell', () async {
final pixels = await paintLines([lineOf('e\u0301\u{1F1F5}\u{1F1F9}x')]);
expect(pixels.count(foregroundColor, cellRect(0, 0)), greaterThan(0));
expect(pixels.count(foregroundColor, cellRect(3, 0)), greaterThan(0));
});
group('row pictures', () {
test('a line is recorded again only when it changes', () {
final cache = RowPictureCache(settleAfter: 100);
final line = lineOf('abc');
var recorded = 0;
void paint(Canvas canvas, bool settled) => recorded++;
final first = cache.pictureOf(line, 0, paint);
expect(cache.pictureOf(line, 0, paint), same(first));
expect(recorded, 1);
line.setCodePoint(0, 0x7A);
expect(cache.pictureOf(line, 0, paint), isNot(same(first)));
expect(recorded, 2);
// A new painter generation (theme, font) records every line again.
cache.pictureOf(line, 1, paint);
expect(recorded, 3);
cache.clear();
});
test('a line drawn unchanged settles into laid out text, within the '
'frame budget', () {
final cache = RowPictureCache(settleAfter: 2, settlePerFrame: 1);
final lines = [lineOf('one'), lineOf('two')];
final settledCalls = <bool>[];
void paint(Canvas canvas, bool settled) => settledCalls.add(settled);
for (var frame = 0; frame < 4; frame++) {
cache.beginFrame();
for (final line in lines) {
cache.pictureOf(line, 0, paint);
}
}
// Recorded new (not settled), then each settled in its own frame.
expect(settledCalls, [false, false, true, true]);
expect(cache.isSettled(lines[0]), isTrue);
expect(cache.isSettled(lines[1]), isTrue);
// A change starts the line over.
lines[0].setCodePoint(0, 0x7A);
cache.beginFrame();
cache.pictureOf(lines[0], 0, paint);
expect(settledCalls.last, isFalse);
expect(cache.isSettled(lines[0]), isFalse);
cache.clear();
});
test('trim keeps the most recently used lines', () {
final cache = RowPictureCache();
final lines = [for (var i = 0; i < 4; i++) lineOf('$i')];
for (final line in lines) {
cache.pictureOf(line, 0, (_, _) {});
}
cache.pictureOf(lines[0], 0, (_, _) {});
cache.trim(2);
expect(cache.length, 2);
var recorded = 0;
cache.pictureOf(lines[0], 0, (_, _) => recorded++);
expect(recorded, 0);
cache.pictureOf(lines[1], 0, (_, _) => recorded++);
expect(recorded, 1);
cache.clear();
});
});
}
@@ -0,0 +1,203 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:xterm/src/utils/unicode_width.dart';
import 'package:xterm/xterm.dart';
import 'xterm_fixture.dart';
/// Widths and grapheme clusters: what each character sequence occupies and
/// where the cursor ends, byte for byte as a program writes it.
void main() {
int widthOf(CellData cell) => cell.content >> CellContent.widthShift;
String grapheme(FixtureTerminal t, int x, int y) =>
t.buffer.lines[t.buffer.scrollBack + y].getGrapheme(x);
group('widths follow glibc wcwidth', () {
test('ASCII, Latin, CJK, emoji, combining, format characters', () {
expect(UnicodeWidth.width(0x41), 1);
expect(UnicodeWidth.width(0xE9), 1); // \u00E9
expect(UnicodeWidth.width(0x4E2D), 2); // \u4E2D
expect(UnicodeWidth.width(0xFF21), 2); // fullwidth A
expect(UnicodeWidth.width(0x1F600), 2); // \u{1F600}
expect(UnicodeWidth.width(0x1FAE8), 2); // Unicode 15 shaking face
expect(UnicodeWidth.width(0x0301), 0); // combining acute
expect(UnicodeWidth.width(0x200D), 0); // ZWJ
expect(UnicodeWidth.width(0xFE0F), 0); // VS16
expect(UnicodeWidth.width(0x1160), 0); // Hangul medial vowel filler
expect(UnicodeWidth.width(0x00AD), 1); // soft hyphen, as glibc
expect(UnicodeWidth.width(0x0600), 1); // prepended mark, as glibc
expect(UnicodeWidth.width(0x2764), 1); // \u2764 text default
expect(UnicodeWidth.width(0x1F1F5), 1); // regional indicator
expect(UnicodeWidth.width(0xE0B0), 1); // Powerline (private use)
expect(UnicodeWidth.width(0x2500), 1); // box drawing
});
});
group('combining marks join the previous cell', () {
test('e + U+0301 is one cell, and the cursor moves one column', () {
final t = FixtureTerminal();
t.feed('e\u0301|');
expect(grapheme(t, 0, 0), 'e\u0301');
expect(t.charAt(1, 0), '|');
expect(t.buffer.cursorX, 2);
expect(t.row(0), 'e\u0301|');
});
test('several marks stack on one base', () {
final t = FixtureTerminal();
t.feed('a\u0300\u0316\u0317b');
expect(grapheme(t, 0, 0), 'a\u0300\u0316\u0317');
expect(t.charAt(1, 0), 'b');
});
test('a mark with nothing before it on the line takes no column', () {
final t = FixtureTerminal();
t.feed('\u0301x');
expect(t.charAt(0, 0), 'x');
expect(t.buffer.cursorX, 1);
});
test('a mark after a wide character joins it, not its second column', () {
final t = FixtureTerminal();
t.feed('\u4E2D\u0301x');
expect(grapheme(t, 0, 0), '\u4E2D\u0301');
expect(widthOf(t.cell(0, 0)), 2);
expect(t.charAt(2, 0), 'x');
});
test('a mark in the pending-wrap column joins the last cell', () {
final t = FixtureTerminal(width: 4);
t.feed('abcd\u0301');
expect(grapheme(t, 3, 0), 'd\u0301');
expect(t.row(1), '');
t.feed('e');
expect(t.row(1), 'e');
});
test('Hangul conjoining jamo make one syllable cell', () {
final t = FixtureTerminal();
t.feed('\u1100\u1161\u11A8|');
expect(grapheme(t, 0, 0), '\u1100\u1161\u11A8');
expect(t.charAt(2, 0), '|');
});
});
group('emoji sequences', () {
test('VS16 widens a text-default emoji to two columns', () {
final t = FixtureTerminal();
t.feed('\u2764\uFE0F|');
expect(grapheme(t, 0, 0), '\u2764\uFE0F');
expect(widthOf(t.cell(0, 0)), 2);
expect(t.charAt(2, 0), '|');
expect(t.buffer.cursorX, 3);
});
test('VS16 at the last column cannot widen and stays one column', () {
final t = FixtureTerminal(width: 4);
t.feed('abc\u2764\uFE0F');
expect(grapheme(t, 3, 0), '\u2764\uFE0F');
expect(widthOf(t.cell(3, 0)), 1);
});
test('VS15 keeps one column', () {
final t = FixtureTerminal();
t.feed('\u2764\uFE0E|');
expect(widthOf(t.cell(0, 0)), 1);
expect(t.charAt(1, 0), '|');
});
test('a skin tone joins its base: two columns, not four', () {
final t = FixtureTerminal();
t.feed('\u{1F44D}\u{1F3FD}|');
expect(grapheme(t, 0, 0), '\u{1F44D}\u{1F3FD}');
expect(widthOf(t.cell(0, 0)), 2);
expect(t.charAt(2, 0), '|');
});
test('a ZWJ family is one two-column cell', () {
const family = '\u{1F468}\u200D\u{1F469}\u200D\u{1F467}';
final t = FixtureTerminal();
t.feed('$family|');
expect(grapheme(t, 0, 0), family);
expect(widthOf(t.cell(0, 0)), 2);
expect(t.charAt(2, 0), '|');
expect(t.row(0), '$family|');
});
test('two regional indicators are one two-column flag', () {
final t = FixtureTerminal();
t.feed('\u{1F1F5}\u{1F1F9}|');
expect(grapheme(t, 0, 0), '\u{1F1F5}\u{1F1F9}');
expect(widthOf(t.cell(0, 0)), 2);
expect(t.charAt(2, 0), '|');
});
test('a third regional indicator starts the next flag', () {
final t = FixtureTerminal();
t.feed('\u{1F1F5}\u{1F1F9}\u{1F1EC}\u{1F1E7}');
expect(grapheme(t, 0, 0), '\u{1F1F5}\u{1F1F9}');
expect(grapheme(t, 2, 0), '\u{1F1EC}\u{1F1E7}');
expect(t.buffer.cursorX, 4);
});
test('a keycap is one cell', () {
final t = FixtureTerminal();
t.feed('1\uFE0F\u20E3|');
expect(grapheme(t, 0, 0), '1\uFE0F\u20E3');
expect(t.charAt(2, 0), '|');
});
test('a letter after ZWJ does not join: only pictographs do', () {
final t = FixtureTerminal();
t.feed('a\u200Db');
expect(grapheme(t, 0, 0), 'a\u200D');
expect(t.charAt(1, 0), 'b');
});
});
group('wide characters', () {
test('a wide character in the last column wraps whole', () {
final t = FixtureTerminal(width: 5);
t.feed('abcd\u4E2D');
expect(t.row(0), 'abcd');
expect(t.row(1), '\u4E2D');
expect(t.buffer.lines[t.buffer.scrollBack + 1].isWrapped, isTrue);
});
test('overwriting the second column of a wide character blanks it', () {
final t = FixtureTerminal();
t.feed('\u4E2D\x1b[2Gx');
expect(t.charAt(0, 0), '');
expect(t.charAt(1, 0), 'x');
});
});
group('copy returns whole graphemes', () {
test('selected text keeps marks, sequences and flags', () {
const text = 'e\u0301 \u{1F44D}\u{1F3FD} \u{1F1F5}\u{1F1F9} \u4E2D';
final t = FixtureTerminal(width: 30);
t.feed(text);
final range = BufferRangeLine(
CellOffset(0, t.buffer.scrollBack),
CellOffset(30, t.buffer.scrollBack),
);
expect(t.buffer.getText(range).trimRight(), text);
});
test('a selection ending inside a wide cluster leaves it out', () {
final t = FixtureTerminal();
t.feed('a\u{1F1F5}\u{1F1F9}b');
final line = t.buffer.lines[t.buffer.scrollBack];
expect(line.getText(0, 2), 'a');
expect(line.getText(0, 3), 'a\u{1F1F5}\u{1F1F9}');
});
test('clusters survive reflow', () {
final t = FixtureTerminal(width: 6);
t.feed('abcde\u0301f\u{1F1F5}\u{1F1F9}');
t.terminal.resize(4, 5);
t.terminal.resize(10, 5);
expect(t.row(0), 'abcde\u0301f\u{1F1F5}\u{1F1F9}');
});
});
}