Terminal: xterm key encoding, no live reflow on resize, Alt+T/Ctrl+T reach the shell

- Live terminals no longer reflow (xterm.dart 4.0.0 left the cursor on the
  wrong row); narrowing cuts and erases cells past the width, rows scroll
  into scrollback keeping the cursor row; finished output still reflows.
- App-side key encoding before xterm.dart's defaults: Alt as Meta (ESC +
  the typed character), Ctrl symbol table, xterm modifier parameter for
  arrows/Home/End/PgUp/PgDn/Ins/Del/F-keys, DECCKM and DECKPAM, Alt+BS.
- Settings > Appearance > Terminal: Use Option as Meta key (Macs, on).
- Inside a focused terminal only Cmd+T and Ctrl+` toggle it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 23:32:29 +00:00
parent 5ab1ec9e00
commit 7d1ffe8ad3
22 changed files with 1408 additions and 61 deletions
@@ -1,6 +1,9 @@
import 'package:flutter/foundation.dart';
import 'package:xterm/core.dart';
import '../../terminal/terminal_layout.dart';
import 'chat_shell_run.dart';
import 'chat_shell_terminal_key_input.dart';
/// Lines of scrollback one card's terminal keeps.
const int chatShellTerminalScrollback = 5000;
@@ -16,7 +19,14 @@ class ChatShellTerminalSession {
ChatShellRun run, {
required this.onInput,
required this.onResize,
}) : terminal = _ChatShellTerminal() {
bool Function()? optionAsMeta,
}) : optionAsMeta =
optionAsMeta ?? (() => TerminalLayout.instance.optionAsMeta),
terminal = _ChatShellTerminal() {
terminal.inputHandler = CascadeInputHandler([
ChatShellInputHandler(optionAsMeta: this.optionAsMeta),
defaultInputHandler,
]);
// Drawn at the process's own size until a view lays it out at its own.
if (run.cols > 0 && run.rows > 0) terminal.resize(run.cols, run.rows);
terminal.onOutput = _output;
@@ -26,6 +36,9 @@ class ChatShellTerminalSession {
final Terminal terminal;
/// Whether a Mac's Option key is Meta (Settings → Appearance → Terminal).
final bool Function() optionAsMeta;
/// Receives input for the process; only called while the run is live.
final void Function(String data) onInput;
@@ -47,6 +60,7 @@ class ChatShellTerminalSession {
/// queries in that output are not sent: the process asked them long ago.
void replay(ChatShellRun run) {
_live = run.running && run.terminal;
terminal.reflowEnabled = !_live;
_replaying = true;
try {
terminal.useMainBuffer();
@@ -86,6 +100,7 @@ class ChatShellTerminalSession {
void finish(ChatShellRun run) {
if (!_live) return;
_live = false;
terminal.reflowEnabled = true;
terminal.setCursorVisibleMode(false);
}
@@ -125,21 +140,48 @@ class ChatShellTerminalSession {
/// 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);
_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');
}
/// Narrowing rewraps long lines, and xterm.dart 4.0.0 keeps every blank
/// row below the cursor while doing so: the extra rows push the top of
/// the output into scrollback. Blank rows below the cursor are dropped
/// instead, keeping the cursor on its line.
@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 &&