import 'package:flutter_test/flutter_test.dart'; import 'package:xterm/xterm.dart'; /// Every cell of every line, the cursor and the wrap flags: what two /// terminals must agree on to have drawn the same output. List state(Terminal terminal) { final buffer = terminal.buffer; final cells = [buffer.height, buffer.cursorX, buffer.cursorY]; final cell = CellData.empty(); for (var y = 0; y < buffer.height; y++) { final line = buffer.lines[y]; cells.add(line.isWrapped); for (var x = 0; x < line.length; x++) { line.getCellData(x, cell); cells.addAll([ cell.foreground, cell.background, cell.flags, cell.content, ]); } } return cells; } // Sized before the scrollback shrinks: the default 24 rows must fit first. Terminal make({int width = 10, int height = 4, int maxLines = 50}) => Terminal(maxLines: 100) ..resize(width, height) ..maxLines = maxLines; void main() { group('printable ASCII runs', () { // [setup] goes through the parser in both terminals; [text] is written // as one run in the first and one character at a time through // Buffer.writeChar (the path every character took before) in the other. void same(String setup, String text, {int width = 10}) { final fast = make(width: width)..write(setup); final slow = make(width: width)..write(setup); fast.write(text); for (final unit in text.codeUnits) { slow.buffer.writeChar(unit); } expect(state(fast), state(slow), reason: '$setup | $text'); } test('wrap across rows and into the scrollback', () { same('', 'abcdefghij' * 7 + 'xyz'); same('abc', 'defghijklmnopqrstuvwxyz0123456789'); same('\x1b[3;1H', 'a' * 95); }); test('a full row waits at the last column, as one character does', () { same('', 'abcdefghij'); same('abcdefghi', 'j'); same('abcdefghij', 'k'); }); test('without autowrap', () { same('\x1b[?7l', 'abcdefghijklmnop'); same('\x1b[?7lab', 'cdefghijklmnop'); }); test('in the cursor style (colours, bold, links)', () { same('\x1b[1;31;44m', 'styled text that wraps'); same('\x1b]8;;https://example.com\x1b\\', 'linked text'); }); test('over a wide character', () { // Writing over the second column of a wide character blanks its // first; over the first column, as before. same('中文\r\x1b[C', 'ab'); same('中文\r', 'abc'); }); test('in the DEC line drawing set', () { same('\x1b(0', 'lqqk xx mqqj'); }); test('in insert mode the run shifts nothing (as one character)', () { same('hello\r\x1b[4h', 'ab'); }); }); test('a run and the characters around it parse as before', () { final terminal = make(width: 20); terminal.write('ab\x1b[31mcd\x1b[0méf\u{1F600}g\r\nnext'); final buffer = terminal.buffer; expect(buffer.lines[0].getText(), 'abcdéf\u{1F600}g'); expect(buffer.lines[0].getForeground(2), CellColor.named | 1); expect(buffer.lines[0].getForeground(4), 0); expect(buffer.lines[1].getText().trimRight(), 'next'); }); test('a sequence split between writes resumes (rollback)', () { final terminal = make(width: 20); terminal.write('ab\x1b[3'); terminal.write('1mcd\x1b'); terminal.write('[0me'); final line = terminal.buffer.lines[0]; expect(line.getText().trimRight(), 'abcde'); expect(line.getForeground(1), 0); expect(line.getForeground(2), CellColor.named | 1); expect(line.getForeground(4), 0); }); test('a surrogate pair is one character; split halves are not', () { final terminal = make(width: 20); terminal.write('a\u{1F600}b'); final line = terminal.buffer.lines[0]; expect(line.getCodePoint(1), 0x1F600); expect(line.getWidth(1), 2); expect(line.getCodePoint(3), 'b'.codeUnitAt(0)); expect(terminal.buffer.cursorX, 4); }); test('REP repeats the last character of a run', () { final terminal = make(width: 20); terminal.write('xyz\x1b[3b'); expect(terminal.buffer.lines[0].getText().trimRight(), 'xyzzzz'); }); group('lines leaving the scrollback are reused', () { test('as empty lines: output keeps its text, wraps and widths', () { final terminal = make(width: 10, height: 3, maxLines: 6); final reference = make(width: 10, height: 3, maxLines: 1000); final output = [ for (var i = 0; i < 40; i++) i.isEven ? '\x1b[4${i % 8}mline $i that wraps\x1b[0m\r\n' : '中$i\r\n', ].join(); terminal.write(output); reference.write(output); final buffer = terminal.buffer; expect(buffer.height, 6); final tail = reference.buffer.height - 6; for (var y = 0; y < 6; y++) { final line = buffer.lines[y]; final expected = reference.buffer.lines[tail + y]; expect(line.getText(), expected.getText(), reason: 'row $y'); expect(line.isWrapped, expected.isWrapped, reason: 'row $y'); expect(line.getTrimmedLength(), expected.getTrimmedLength()); for (var x = 0; x < 10; x++) { expect(line.getBackground(x), expected.getBackground(x)); expect(line.getWidth(x), expected.getWidth(x)); } } }); test('a reused line has a new version, so its picture is redrawn', () { final terminal = make(width: 10, height: 2, maxLines: 2); terminal.write('a\r\nb'); final top = terminal.buffer.lines[0]; final version = top.version; terminal.write('\r\nc'); expect(identical(terminal.buffer.lines[1], top), isTrue); expect(top.version, isNot(version)); expect(top.getText().trimRight(), 'c'); }); test('a line with an anchor (a selection end) leaves detached', () { final terminal = make(width: 10, height: 2, maxLines: 2); terminal.write('a\r\nb'); final top = terminal.buffer.lines[0]; final anchor = terminal.buffer.createAnchor(0, 0); terminal.write('\r\nc'); expect(anchor.attached, isFalse); expect(top.attached, isFalse); expect(top.getText().trimRight(), 'a'); expect(terminal.buffer.lines[1].getText().trimRight(), 'c'); }); test('a top scroll region feeding a full scrollback wraps around', () { // Line feeds at the bottom of a region that starts at the top insert // into the scrollback rather than push; well past its capacity. String screen(Terminal terminal) => [ for (var y = 0; y < 4; y++) terminal.buffer.lines[terminal.buffer.scrollBack + y] .getText() .trimRight(), ].join('|'); final small = make(width: 10, height: 4, maxLines: 8); final large = make(width: 10, height: 4, maxLines: 1000); for (final terminal in [small, large]) { terminal.write('\x1b[1;3r\x1b[4;1Hstatus\x1b[1;1H'); for (var i = 0; i < 50; i++) { terminal.write('\x1b[3;1Hrow $i\n'); } } expect(small.buffer.height, 8); expect(screen(small), screen(large)); for (var y = 0; y < 8; y++) { expect(small.buffer.lines[y].index, y); } }); test('in a scroll region on the alternate screen', () { final terminal = make(width: 10, height: 4); terminal.write('\x1b[?1049h\x1b[2;3r\x1b[1;1Htop\x1b[4;1Hbottom'); terminal.write('\x1b[2;1H\x1b[41mone\r\n\x1b[0mtwo\r\nthree\r\nfour'); final buffer = terminal.buffer; String row(int y) => buffer.lines[y].getText().trimRight(); expect([row(0), row(1), row(2), row(3)], [ 'top', 'three', 'four', 'bottom', ]); // The reused lines carry nothing of what scrolled out. expect(buffer.lines[2].getBackground(0), 0); expect(buffer.lines[2].getBackground(9), 0); }); }); test('a write without notify leaves notifying to the host', () { final terminal = make(); var heard = 0; terminal.addListener(() => heard++); terminal.write('quiet', notify: false); expect(heard, 0); terminal.write('loud'); expect(heard, 1); expect(terminal.buffer.lines[0].getText().trimRight(), 'quietloud'); }); }