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
@@ -1,13 +1,14 @@
import 'package:flutter/foundation.dart';
import 'package:xterm/core.dart';
import '../../terminal/terminal_layout.dart';
import 'chat_shell_run.dart';
import 'chat_shell_terminal_emulator.dart';
import 'chat_shell_terminal_events.dart';
import 'chat_shell_terminal_feed.dart';
import 'chat_shell_terminal_key_input.dart';
/// Lines of scrollback one card's terminal keeps.
const int chatShellTerminalScrollback = 5000;
/// Lines a terminal keeps unless the setting says otherwise.
const int chatShellTerminalScrollback = terminalScrollbackDefault;
/// The terminal emulator state of one run: the xterm [terminal] fed with the
/// run's output in order, and the bridge from what the operator types (or
@@ -29,9 +30,19 @@ class ChatShellTerminalSession {
this.onReply,
this.onActivate,
bool Function()? optionAsMeta,
}) : optionAsMeta =
optionAsMeta ?? (() => TerminalLayout.instance.optionAsMeta),
terminal = _ChatShellTerminal() {
TerminalLayout? layout,
}) : layout = layout ?? TerminalLayout.instance,
optionAsMeta =
optionAsMeta ??
(() => (layout ?? TerminalLayout.instance).optionAsMeta),
terminal = ChatShellTerminal(
maxLines: (layout ?? TerminalLayout.instance).scrollback,
) {
events = ChatShellTerminalEvents(
terminal,
quiet: () => _replaying || !_live,
);
this.layout.addListener(_layoutChanged);
terminal.inputHandler = CascadeInputHandler([
ChatShellInputHandler(optionAsMeta: this.optionAsMeta),
defaultInputHandler,
@@ -49,6 +60,12 @@ class ChatShellTerminalSession {
final Terminal terminal;
/// The terminal preferences (text size, scrollback, bell) views follow.
final TerminalLayout layout;
/// The bell, clipboard and folder the program reports.
late final ChatShellTerminalEvents events;
/// Whether a Mac's Option key is Meta (Settings → Appearance → Terminal).
final bool Function() optionAsMeta;
@@ -146,7 +163,14 @@ class ChatShellTerminalSession {
}
/// Stops drawing what still waits (the emulator is going away).
void dispose() => _feed.dispose();
void dispose() {
layout.removeListener(_layoutChanged);
events.dispose();
_feed.dispose();
}
/// A changed scrollback length applies to this running terminal too.
void _layoutChanged() => terminal.maxLines = layout.scrollback;
/// Writes one slice. What the emulator sends while drawing live output
/// answers that output (onReply); during a replay it is not sent at all.
@@ -199,58 +223,3 @@ class ChatShellTerminalSession {
static String _drawable(ChatShellRun run, String text) =>
run.terminal ? text : text.replaceAll('\n', '\r\n');
}
/// The emulator with the scrollback the cards keep. It answers a cursor
/// position query (DSR 6) with the one-based row and column the protocol
/// specifies; xterm.dart 4.0.0 itself answers zero-based.
///
/// A live terminal does not reflow on resize: xterm.dart 4.0.0's reflow
/// leaves the cursor on a row other than its line's, so the shell's redraw
/// lands on old output. Without it a resize behaves as xterm's: narrowing
/// cuts rows at the new width, fewer rows scroll the top into scrollback
/// (keeping the cursor's row at the bottom), more rows bring it back.
/// Finished output, which nothing writes to any more, reflows to fit.
class _ChatShellTerminal extends Terminal {
_ChatShellTerminal()
: super(maxLines: chatShellTerminalScrollback, platform: _platform());
/// The package's own Alt handler (a fallback after the app's) leaves
/// Option alone only when it knows it is on a Mac.
static TerminalTargetPlatform _platform() =>
defaultTargetPlatform == TargetPlatform.macOS ||
defaultTargetPlatform == TargetPlatform.iOS
? TerminalTargetPlatform.macos
: TerminalTargetPlatform.unknown;
@override
void sendCursorPosition() {
onOutput?.call('\x1b[${buffer.cursorY + 1};${buffer.cursorX + 1}R');
}
@override
void resize(int width, int height, [int? pixelWidth, int? pixelHeight]) {
final oldWidth = viewWidth;
if (!reflowEnabled && width >= 1 && width < oldWidth) {
// A cut row keeps its cells past the new width, which a later widening
// would show again beside what was written since: they are erased.
for (final buffer in [mainBuffer, altBuffer]) {
buffer.lines.forEach((line) {
if (line.length > width) {
line.eraseRange(width, line.length, CursorStyle.empty);
}
});
}
}
super.resize(width, height, pixelWidth, pixelHeight);
if (!reflowEnabled) return;
// Rewrapping keeps every blank row below the cursor, and the extra rows
// push the top of finished output into scrollback: they are dropped.
final lines = buffer.lines;
while (buffer.height > viewHeight &&
buffer.height - 1 > buffer.absoluteCursorY &&
lines[buffer.height - 1].getText().trim().isEmpty) {
lines.pop();
buffer.setCursor(buffer.cursorX, buffer.cursorY + 1);
}
}
}