diff --git a/app/test/third_party_xterm/cursor_shape_test.dart b/app/test/third_party_xterm/cursor_shape_test.dart new file mode 100644 index 00000000..81844e65 --- /dev/null +++ b/app/test/third_party_xterm/cursor_shape_test.dart @@ -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 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)); + }); + }); +} diff --git a/app/test/third_party_xterm/paint_fixture.dart b/app/test/third_party_xterm/paint_fixture.dart new file mode 100644 index 00000000..134f5036 --- /dev/null +++ b/app/test/third_party_xterm/paint_fixture.dart @@ -0,0 +1,98 @@ +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 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), + ); + } + + /// 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), +); diff --git a/app/third_party/xterm/lib/src/core/escape/handler.dart b/app/third_party/xterm/lib/src/core/escape/handler.dart index 50d1f82b..c6658a1e 100644 --- a/app/third_party/xterm/lib/src/core/escape/handler.dart +++ b/app/third_party/xterm/lib/src/core/escape/handler.dart @@ -123,6 +123,10 @@ abstract class EscapeHandler { void unknownCSI(int finalByte); + /// `CSI Ps SP q` (DECSCUSR): 0 default, 1/2 blinking/steady block, 3/4 + /// underline, 5/6 bar. + void setCursorShape(int shape); + /* Modes */ void setInsertMode(bool enabled); diff --git a/app/third_party/xterm/lib/src/core/escape/parser.dart b/app/third_party/xterm/lib/src/core/escape/parser.dart index 2b82bf3e..30a5744b 100644 --- a/app/third_party/xterm/lib/src/core/escape/parser.dart +++ b/app/third_party/xterm/lib/src/core/escape/parser.dart @@ -335,6 +335,14 @@ class EscapeParser { return; } + // `CSI Ps SP q` Set Cursor Style (DECSCUSR) + if (intermediates == Ascii.space && + finalByte == Ascii.q && + _csi.prefix == null) { + handler.setCursorShape(_param(0, 0)); + return; + } + handler.unknownCSI(finalByte); } diff --git a/app/third_party/xterm/lib/src/terminal.dart b/app/third_party/xterm/lib/src/terminal.dart index b3d2426e..ed15f168 100644 --- a/app/third_party/xterm/lib/src/terminal.dart +++ b/app/third_party/xterm/lib/src/terminal.dart @@ -18,6 +18,7 @@ import 'package:xterm/src/core/mouse/mode.dart'; import 'package:xterm/src/core/platform.dart'; import 'package:xterm/src/core/state.dart'; import 'package:xterm/src/core/tabs.dart'; +import 'package:xterm/src/ui/cursor_type.dart'; import 'package:xterm/src/utils/ascii.dart'; import 'package:xterm/src/utils/circular_buffer.dart'; @@ -148,8 +149,24 @@ class Terminal with Observable implements TerminalState, EscapeHandler { bool _bracketedPasteMode = false; + int _cursorShapeParam = 0; + /* State getters */ + /// The cursor shape the program asked for with DECSCUSR (`CSI Ps SP q`), + /// or null when it has not asked (or asked for the default, Ps 0): the + /// view then draws its own configured cursor type. + TerminalCursorType? get cursorShape => switch (_cursorShapeParam) { + 1 || 2 => TerminalCursorType.block, + 3 || 4 => TerminalCursorType.underline, + 5 || 6 => TerminalCursorType.verticalBar, + _ => null, + }; + + /// Whether the DECSCUSR shape blinks (odd Ps). Null with no request. + bool? get cursorShapeBlinks => + cursorShape == null ? null : _cursorShapeParam.isOdd; + /// Number of cells in a terminal row. @override int get viewWidth => _viewWidth; @@ -494,6 +511,7 @@ class Terminal with Observable implements TerminalState, EscapeHandler { _altBufferMouseScrollMode = false; _bracketedPasteMode = false; _precedingCodepoint = 0; + _cursorShapeParam = 0; _tabStops.reset(); _buffer = _mainBuffer; @@ -528,6 +546,7 @@ class Terminal with Observable implements TerminalState, EscapeHandler { final value = switch (setting) { 'm' => '${sgrReport(_cursorStyle)}m', 'r' => '${_buffer.marginTop + 1};${_buffer.marginBottom + 1}r', + ' q' => '${_cursorShapeParam == 0 ? 2 : _cursorShapeParam} q', _ => null, }; // `DCS 1 $ r Pt ST` reports a valid setting, `DCS 0 $ r ST` an @@ -731,6 +750,11 @@ class Terminal with Observable implements TerminalState, EscapeHandler { // no-op } + @override + void setCursorShape(int shape) { + _cursorShapeParam = shape <= 6 ? shape : 0; + } + /* Modes */ @override diff --git a/app/third_party/xterm/lib/src/ui/painter.dart b/app/third_party/xterm/lib/src/ui/painter.dart index 64a78e08..4d70426b 100644 --- a/app/third_party/xterm/lib/src/ui/painter.dart +++ b/app/third_party/xterm/lib/src/ui/painter.dart @@ -86,42 +86,74 @@ class TerminalPainter { } /// Paints the cursor based on the current cursor type. + /// + /// [cell] is the cell under the cursor: a focused block cursor draws its + /// character again in the background colour, so the character stays + /// readable, and a cursor over a wide character spans both columns. void paintCursor( Canvas canvas, Offset offset, { required TerminalCursorType cursorType, bool hasFocus = true, + CellData? cell, }) { - final paint = Paint() - ..color = _theme.cursor - ..strokeWidth = 1; + final wide = cell != null && cell.content >> CellContent.widthShift == 2; + final size = wide ? Size(_cellSize.width * 2, _cellSize.height) : _cellSize; + final thickness = cursorThickness; + final paint = Paint()..color = _theme.cursor; if (!hasFocus) { - paint.style = PaintingStyle.stroke; - canvas.drawRect(offset & _cellSize, paint); + paint + ..style = PaintingStyle.stroke + ..strokeWidth = 1; + canvas.drawRect((offset & size).deflate(0.5), paint); return; } switch (cursorType) { case TerminalCursorType.block: - paint.style = PaintingStyle.fill; - canvas.drawRect(offset & _cellSize, paint); + canvas.drawRect(offset & size, paint); + if (cell != null) { + paintCellForeground(canvas, offset, _underCursor(cell)); + } return; case TerminalCursorType.underline: - return canvas.drawLine( - Offset(offset.dx, _cellSize.height - 1), - Offset(offset.dx + _cellSize.width, _cellSize.height - 1), + canvas.drawRect( + Rect.fromLTWH( + offset.dx, + offset.dy + size.height - thickness, + size.width, + thickness, + ), paint, ); + return; case TerminalCursorType.verticalBar: - return canvas.drawLine( - Offset(offset.dx, 0), - Offset(offset.dx, _cellSize.height), + canvas.drawRect( + Rect.fromLTWH(offset.dx, offset.dy, thickness, size.height), paint, ); + return; } } + /// The width of an underline or bar cursor: a tenth of the line height, + /// at least one pixel. + double get cursorThickness => + (_cellSize.height / 10).roundToDouble().clamp(1.0, double.infinity); + + /// [cell] as drawn on top of a block cursor: in the background colour, + /// without inverse. + CellData _underCursor(CellData cell) { + final background = _theme.background.toARGB32() & CellColor.valueMask; + return CellData( + foreground: CellColor.rgb | background, + background: 0, + flags: cell.flags & ~CellFlags.inverse, + content: cell.content, + ); + } + @pragma('vm:prefer-inline') void paintHighlight(Canvas canvas, Offset offset, int length, Color color) { final endOffset = diff --git a/app/third_party/xterm/lib/src/ui/render.dart b/app/third_party/xterm/lib/src/ui/render.dart index de2a2f7f..547d76f1 100644 --- a/app/third_party/xterm/lib/src/ui/render.dart +++ b/app/third_party/xterm/lib/src/ui/render.dart @@ -6,6 +6,7 @@ import 'package:flutter/widgets.dart'; import 'package:xterm/src/core/buffer/cell_offset.dart'; import 'package:xterm/src/core/buffer/range.dart'; import 'package:xterm/src/core/buffer/segment.dart'; +import 'package:xterm/src/core/cell.dart'; import 'package:xterm/src/core/mouse/button.dart'; import 'package:xterm/src/core/mouse/button_state.dart'; import 'package:xterm/src/terminal.dart'; @@ -428,11 +429,16 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin { } if (_shouldShowCursor) { + final buffer = _terminal.buffer; + final cell = CellData.empty(); + buffer.lines[buffer.absoluteCursorY].getCellData(buffer.cursorX, cell); _painter.paintCursor( canvas, offset + cursorOffset, - cursorType: _cursorType, + // The program's DECSCUSR request wins over the configured type. + cursorType: _terminal.cursorShape ?? _cursorType, hasFocus: _focusNode.hasFocus, + cell: _isComposingText ? null : cell, ); } } diff --git a/app/third_party/xterm/lib/src/utils/debugger.dart b/app/third_party/xterm/lib/src/utils/debugger.dart index 4a15f905..ca9125aa 100644 --- a/app/third_party/xterm/lib/src/utils/debugger.dart +++ b/app/third_party/xterm/lib/src/utils/debugger.dart @@ -471,6 +471,11 @@ class _TerminalDebuggerHandler implements EscapeHandler { onCommand('setCursorUnderline'); } + @override + void setCursorShape(int shape) { + onCommand('setCursorShape $shape'); + } + @override void resetTerminal() { onCommand('resetTerminal');