xterm: DECSCUSR cursor shapes; block cursor shows the character

CSI Ps SP q is stored as Terminal.cursorShape / cursorShapeBlinks (Ps 0
returns to the view's configured cursorType; RIS clears it) and reported
by DECRQSS " q". The painter draws the requested shape. A focused block
cursor now redraws the character under it in the background colour
instead of hiding it, spans both columns of a wide character, and the
underline and bar cursors are drawn in the cursor's cell (they were drawn
at the top of the terminal).

Blinking is stored but not animated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 23:30:10 +00:00
parent 31d00e057c
commit bbb2d112ed
8 changed files with 288 additions and 14 deletions
@@ -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);
+8
View File
@@ -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);
}
+24
View File
@@ -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
+45 -13
View File
@@ -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 =
+7 -1
View File
@@ -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,
);
}
}
+5
View File
@@ -471,6 +471,11 @@ class _TerminalDebuggerHandler implements EscapeHandler {
onCommand('setCursorUnderline');
}
@override
void setCursorShape(int shape) {
onCommand('setCursorShape $shape');
}
@override
void resetTerminal() {
onCommand('resetTerminal');