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}'); }); }); }