Merge main into the terminal flow-control branch
Conflicts resolved keeping both sides: main's active-client rules, input lane, reply marking and key input, and this branch's flow control, acknowledgements, subscriptions and sliced emulator feed. terminal_ack joins the terminal lane; the feed marks each live slice it writes as drawing, so the emulator's answers still go to onReply. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:xterm/core.dart';
|
||||
|
||||
import '../../terminal/terminal_layout.dart';
|
||||
import 'chat_shell_run.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;
|
||||
@@ -12,31 +15,62 @@ const int chatShellTerminalScrollback = 5000;
|
||||
///
|
||||
/// It keeps no copy of typed input: [Terminal.onOutput] hands each key or
|
||||
/// paste straight to [onInput], which sends it.
|
||||
///
|
||||
/// Several clients may draw one terminal. The emulator's automatic answers
|
||||
/// to queries in the output (cursor position, device attributes) go to
|
||||
/// [onReply] instead, so the server can let only the active client's
|
||||
/// answers through; a view gaining focus calls [activate], which makes this
|
||||
/// client the active one at its size ([onActivate]).
|
||||
class ChatShellTerminalSession {
|
||||
ChatShellTerminalSession(
|
||||
ChatShellRun run, {
|
||||
required this.onInput,
|
||||
required this.onResize,
|
||||
}) : terminal = _ChatShellTerminal() {
|
||||
this.onReply,
|
||||
this.onActivate,
|
||||
bool Function()? optionAsMeta,
|
||||
}) : optionAsMeta =
|
||||
optionAsMeta ?? (() => TerminalLayout.instance.optionAsMeta),
|
||||
terminal = _ChatShellTerminal() {
|
||||
terminal.inputHandler = CascadeInputHandler([
|
||||
ChatShellInputHandler(optionAsMeta: this.optionAsMeta),
|
||||
defaultInputHandler,
|
||||
]);
|
||||
_feed = ChatShellTerminalFeed(_write);
|
||||
// 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;
|
||||
terminal.onResize = (cols, rows, _, _) => onResize(cols, rows);
|
||||
terminal.onResize = (cols, rows, _, _) {
|
||||
_viewSize = (cols, rows);
|
||||
onResize(cols, rows);
|
||||
};
|
||||
replay(run);
|
||||
}
|
||||
|
||||
final Terminal terminal;
|
||||
|
||||
/// Receives input for the process; only called while the run is live.
|
||||
/// Whether a Mac's Option key is Meta (Settings → Appearance → Terminal).
|
||||
final bool Function() optionAsMeta;
|
||||
|
||||
/// Receives what the operator types for the process; only called while
|
||||
/// the run is live.
|
||||
final void Function(String data) onInput;
|
||||
|
||||
/// Receives the size the view laid the terminal out at.
|
||||
final void Function(int cols, int rows) onResize;
|
||||
|
||||
/// Receives the emulator's automatic answers to queries in the live
|
||||
/// output; [onInput] receives them when this is null.
|
||||
final void Function(String data)? onReply;
|
||||
|
||||
/// Receives this client's size when a view of it gains focus.
|
||||
final void Function(int cols, int rows)? onActivate;
|
||||
|
||||
late final ChatShellTerminalFeed _feed;
|
||||
bool _live = false;
|
||||
bool _replaying = false;
|
||||
bool _drawing = false;
|
||||
(int, int)? _viewSize;
|
||||
|
||||
/// The absolute output byte this terminal has drawn up to, or will once
|
||||
/// the output waiting in its feed is written.
|
||||
@@ -51,6 +85,16 @@ class ChatShellTerminalSession {
|
||||
/// Whether typing reaches a running process.
|
||||
bool get live => _live;
|
||||
|
||||
/// The size a view last laid the terminal out at; null before any did.
|
||||
(int, int)? get viewSize => _viewSize;
|
||||
|
||||
/// A view of this terminal gained focus: this client becomes the one the
|
||||
/// process is sized for and whose answers it receives.
|
||||
void activate() {
|
||||
final size = _viewSize;
|
||||
if (_live && size != null) onActivate?.call(size.$1, size.$2);
|
||||
}
|
||||
|
||||
/// Draws [run]'s held output from scratch: on open, after a gap and after
|
||||
/// a readback replaced the card's copy. Answers the emulator gives to old
|
||||
/// queries in that output are not sent: the process asked them long ago.
|
||||
@@ -58,6 +102,7 @@ class ChatShellTerminalSession {
|
||||
/// (ChatShellTerminalFeed), so it never stalls the UI.
|
||||
void replay(ChatShellRun run) {
|
||||
_live = run.running && run.terminal;
|
||||
terminal.reflowEnabled = !_live;
|
||||
_feed.restart(run.outputStart);
|
||||
_replaying = true;
|
||||
try {
|
||||
@@ -96,18 +141,23 @@ class ChatShellTerminalSession {
|
||||
void finish(ChatShellRun run) {
|
||||
if (!_live) return;
|
||||
_live = false;
|
||||
terminal.reflowEnabled = true;
|
||||
_feed.then(() => terminal.setCursorVisibleMode(false));
|
||||
}
|
||||
|
||||
/// Stops drawing what still waits (the emulator is going away).
|
||||
void dispose() => _feed.dispose();
|
||||
|
||||
/// Writes one slice. What the emulator sends while drawing live output
|
||||
/// answers that output (onReply); during a replay it is not sent at all.
|
||||
void _write(String text, bool replay) {
|
||||
_replaying = replay;
|
||||
_drawing = !replay;
|
||||
try {
|
||||
terminal.write(text);
|
||||
} finally {
|
||||
_replaying = false;
|
||||
_drawing = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +185,13 @@ class ChatShellTerminalSession {
|
||||
}
|
||||
|
||||
void _output(String data) {
|
||||
if (_live && !_replaying) onInput(data);
|
||||
if (!_live || _replaying) return;
|
||||
final reply = onReply;
|
||||
if (_drawing && reply != null) {
|
||||
reply(data);
|
||||
} else {
|
||||
onInput(data);
|
||||
}
|
||||
}
|
||||
|
||||
/// Plain-pipe output has bare newlines, which a terminal draws as a line
|
||||
@@ -147,21 +203,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 &&
|
||||
|
||||
Reference in New Issue
Block a user