Terminal: find, zoom, scrollback, scroll keys, context menu, bell, link hover, OSC 52/7, cursor blink, stale selection

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 00:33:53 +00:00
parent fc5d3bc42b
commit 6832ec789e
32 changed files with 2260 additions and 194 deletions
+14 -4
View File
@@ -14,7 +14,8 @@ import 'package:xterm/src/utils/unicode_v11.dart';
class Buffer {
final TerminalState terminal;
final int maxLines;
/// The lines this buffer keeps, scrollback included; see [setMaxLines].
int get maxLines => lines.maxLength;
final bool isAltBuffer;
@@ -24,10 +25,10 @@ class Buffer {
Buffer(
this.terminal, {
required this.maxLines,
required int maxLines,
required this.isAltBuffer,
this.wordSeparators,
}) {
}) : lines = IndexAwareCircularBuffer<BufferLine>(maxLines) {
for (int i = 0; i < terminal.viewHeight; i++) {
lines.push(_newEmptyLine());
}
@@ -59,7 +60,16 @@ class Buffer {
/// lines of the buffer. the length of [lines] should always be equal or
/// greater than [viewHeight].
late final lines = IndexAwareCircularBuffer<BufferLine>(maxLines);
final IndexAwareCircularBuffer<BufferLine> lines;
/// Keeps at most [value] lines (never fewer than the screen's rows),
/// dropping the oldest scrollback when that is fewer than it holds.
void setMaxLines(int value) {
value = max(value, viewHeight);
final excess = lines.length - value;
if (excess > 0) lines.trimStart(excess);
lines.maxLength = value;
}
/// Total number of lines in the buffer. Always equal or greater than
/// [viewHeight].
+13 -3
View File
@@ -33,7 +33,17 @@ import 'package:xterm/src/utils/circular_buffer.dart';
class Terminal with Observable implements TerminalState, EscapeHandler {
/// The number of lines that the scrollback buffer can hold. If the buffer
/// exceeds this size, the lines at the top of the buffer will be removed.
final int maxLines;
/// Setting it applies to the running terminal: a smaller value drops the
/// oldest scrollback at once.
int get maxLines => _maxLines;
int _maxLines;
set maxLines(int value) {
if (value == _maxLines || value <= 0) return;
_maxLines = value;
_mainBuffer.setMaxLines(value);
_altBuffer.setMaxLines(value);
notifyListeners();
}
/// Function that is called when the program requests the terminal to ring
/// the bell. If not set, the terminal will do nothing.
@@ -105,7 +115,7 @@ class Terminal with Observable implements TerminalState, EscapeHandler {
final Set<int>? wordSeparators;
Terminal({
this.maxLines = 1000,
int maxLines = 1000,
this.onBell,
this.onTitleChange,
this.onIconChange,
@@ -117,7 +127,7 @@ class Terminal with Observable implements TerminalState, EscapeHandler {
this.onPrivateOSC,
this.reflowEnabled = true,
this.wordSeparators,
});
}) : _maxLines = maxLines;
late final _parser = EscapeParser(this);
+13
View File
@@ -1,4 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
@@ -44,6 +45,7 @@ class TerminalView extends StatefulWidget {
this.keyboardAppearance = Brightness.dark,
this.cursorType = TerminalCursorType.block,
this.alwaysShowCursor = false,
this.cursorBlinkVisible,
this.deleteDetection = false,
this.shortcuts,
this.onKeyEvent,
@@ -116,6 +118,11 @@ class TerminalView extends StatefulWidget {
/// [false] by default.
final bool alwaysShowCursor;
/// The phase of a blinking cursor: while its value is false the cursor is
/// not drawn. The host animates it (and stops it while unfocused); null
/// draws a steady cursor.
final ValueListenable<bool>? cursorBlinkVisible;
/// Workaround to detect delete key for platforms and IMEs that does not
/// emit hardware delete event. Prefered on mobile platforms. [false] by
/// default.
@@ -256,6 +263,7 @@ class TerminalViewState extends State<TerminalView> {
focusNode: _focusNode,
cursorType: widget.cursorType,
alwaysShowCursor: widget.alwaysShowCursor,
cursorBlinkVisible: widget.cursorBlinkVisible,
onEditableRect: _onEditableRect,
composingText: _composingText,
);
@@ -496,6 +504,7 @@ class _TerminalView extends LeafRenderObjectWidget {
required this.focusNode,
required this.cursorType,
required this.alwaysShowCursor,
this.cursorBlinkVisible,
this.onEditableRect,
this.composingText,
});
@@ -522,6 +531,8 @@ class _TerminalView extends LeafRenderObjectWidget {
final bool alwaysShowCursor;
final ValueListenable<bool>? cursorBlinkVisible;
final EditableRectCallback? onEditableRect;
final String? composingText;
@@ -540,6 +551,7 @@ class _TerminalView extends LeafRenderObjectWidget {
focusNode: focusNode,
cursorType: cursorType,
alwaysShowCursor: alwaysShowCursor,
cursorBlinkVisible: cursorBlinkVisible,
onEditableRect: onEditableRect,
composingText: composingText,
);
@@ -559,6 +571,7 @@ class _TerminalView extends LeafRenderObjectWidget {
..focusNode = focusNode
..cursorType = cursorType
..alwaysShowCursor = alwaysShowCursor
..cursorBlinkVisible = cursorBlinkVisible
..onEditableRect = onEditableRect
..composingText = composingText;
}
+16 -1
View File
@@ -1,6 +1,7 @@
import 'dart:math' show max;
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:xterm/src/core/buffer/cell_offset.dart';
@@ -33,6 +34,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
required FocusNode focusNode,
required TerminalCursorType cursorType,
required bool alwaysShowCursor,
ValueListenable<bool>? cursorBlinkVisible,
EditableRectCallback? onEditableRect,
String? composingText,
}) : _terminal = terminal,
@@ -43,6 +45,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
_focusNode = focusNode,
_cursorType = cursorType,
_alwaysShowCursor = alwaysShowCursor,
_cursorBlinkVisible = cursorBlinkVisible,
_onEditableRect = onEditableRect,
_composingText = composingText,
_painter = TerminalPainter(
@@ -146,6 +149,15 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
markNeedsPaint();
}
ValueListenable<bool>? _cursorBlinkVisible;
set cursorBlinkVisible(ValueListenable<bool>? value) {
if (value == _cursorBlinkVisible) return;
if (attached) _cursorBlinkVisible?.removeListener(markNeedsPaint);
_cursorBlinkVisible = value;
if (attached) _cursorBlinkVisible?.addListener(markNeedsPaint);
markNeedsPaint();
}
EditableRectCallback? _onEditableRect;
set onEditableRect(EditableRectCallback? value) {
if (value == _onEditableRect) return;
@@ -196,6 +208,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
_terminal.addListener(_onTerminalChange);
_controller.addListener(_onControllerUpdate);
_focusNode.addListener(_onFocusChange);
_cursorBlinkVisible?.addListener(markNeedsPaint);
}
@override
@@ -205,6 +218,7 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
_terminal.removeListener(_onTerminalChange);
_controller.removeListener(_onControllerUpdate);
_focusNode.removeListener(_onFocusChange);
_cursorBlinkVisible?.removeListener(markNeedsPaint);
}
@override
@@ -398,7 +412,8 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
}
bool get _shouldShowCursor {
return _terminal.cursorVisibleMode || _alwaysShowCursor || _isComposingText;
if (_isComposingText || _alwaysShowCursor) return true;
return _terminal.cursorVisibleMode && _cursorBlinkVisible?.value != false;
}
double get _viewportHeight {
@@ -224,8 +224,14 @@ class IndexAwareCircularBuffer<T extends IndexedItem> {
/// instead just adjusts the start index and length.
void trimStart(int count) {
if (count > _length) count = _length;
// The dropped items are detached and the rest keep their indexes, as a
// push that overflows does.
for (var i = 0; i < count; i++) {
_dropChild(i);
}
_startIndex += count;
_startIndex %= _array.length;
_absoluteStartIndex += count;
_length -= count;
}
+1
View File
@@ -3,6 +3,7 @@ export 'src/ui/controller.dart';
export 'src/ui/cursor_type.dart';
export 'src/ui/keyboard_visibility.dart';
export 'src/ui/pointer_input.dart';
export 'src/ui/render.dart' show RenderTerminal;
export 'src/ui/selection_mode.dart';
export 'src/ui/shortcut/shortcuts.dart';
export 'src/ui/terminal_text_style.dart';