Files
pipelines/app/lib/features/chat/shell/chat_shell_terminal_view.dart
T
alan 89882d90f3 Draw chat shell runs in an xterm terminal that takes typed input
The card's output area is now an xterm.dart emulator kept per run by the
controller: it is fed the raw stream in order, takes typing and paste while
the command runs (with a key toolbar and a soft-keyboard field on touch
platforms), resizes the process to its layout, opens printed links, shrinks
finished runs to their output and opens full screen. A reconnect resumes from
the byte the card holds; Copy output copies the server's plain text.
2026-11-09 20:08:33 +00:00

114 lines
4.0 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:xterm/xterm.dart';
import '../ui/transcript/chat_link_launcher.dart';
import 'chat_shell_metrics.dart';
import 'chat_shell_terminal_links.dart';
import 'chat_shell_terminal_session.dart';
import 'chat_shell_terminal_theme.dart';
/// Whether the soft keyboard is the main way to type here, so the terminal
/// takes typing from the key toolbar's field rather than its own hidden one.
bool chatShellTouchFirst() =>
defaultTargetPlatform == TargetPlatform.android ||
defaultTargetPlatform == TargetPlatform.iOS;
/// One run's terminal emulator view. In a card it is bounded to
/// [kChatShellOutputCollapsedLines] rows (a finished run shrinks to its
/// output); with [fill] it takes the space its parent gives (full screen).
/// It sizes the terminal to its own layout; a running process is resized to
/// match. Tapping a printed web link opens it in the browser.
class ChatShellTerminalView extends StatefulWidget {
const ChatShellTerminalView({
super.key,
required this.session,
required this.cupertino,
this.fill = false,
this.autofocus = false,
this.autoResize = true,
});
final ChatShellTerminalSession session;
final bool cupertino;
final bool fill;
final bool autofocus;
/// False while another view of the same terminal (full screen) sizes it.
final bool autoResize;
@override
State<ChatShellTerminalView> createState() => _ChatShellTerminalViewState();
}
class _ChatShellTerminalViewState extends State<ChatShellTerminalView> {
final TerminalController _controller = TerminalController();
final FocusNode _focus = FocusNode(debugLabel: 'chat-shell-terminal');
int? _fitRows;
@override
void dispose() {
_controller.dispose();
_focus.dispose();
super.dispose();
}
void _tapped(TapUpDetails details, CellOffset offset) {
final uri = chatShellLinkAt(widget.session.terminal.buffer, offset);
if (uri != null) openChatLink(context, uri);
}
/// A finished run's box shrinks to the rows its output uses.
void _fit() {
if (!mounted || widget.fill || widget.session.live) return;
final used = widget.session.terminal.buffer.absoluteCursorY + 1;
final rows = used.clamp(1, kChatShellOutputCollapsedLines);
if (rows != _fitRows) setState(() => _fitRows = rows);
}
@override
Widget build(BuildContext context) {
final session = widget.session;
final live = session.live;
final style = chatShellTerminalStyle(context, widget.cupertino);
const padding = EdgeInsets.all(8);
Widget view = Semantics(
identifier: 'chat-shell-terminal',
label: live ? 'Terminal, type to send input' : 'Terminal output',
child: TerminalView(
session.terminal,
controller: _controller,
focusNode: _focus,
autofocus: widget.autofocus && live,
autoResize: widget.autoResize,
theme: chatShellTerminalTheme(context, widget.cupertino),
textStyle: style,
padding: padding,
readOnly: !live,
hardwareKeyboardOnly: !live || chatShellTouchFirst(),
keyboardType: TextInputType.visiblePassword,
cursorType: TerminalCursorType.block,
onTapUp: _tapped,
),
);
// The emulator would otherwise inset itself by the window's safe area.
view = MediaQuery.removePadding(
context: context,
removeTop: true,
removeBottom: true,
removeLeft: true,
removeRight: true,
child: view,
);
view = ClipRRect(borderRadius: BorderRadius.circular(8), child: view);
if (widget.fill) return view;
if (!live) WidgetsBinding.instance.addPostFrameCallback((_) => _fit());
final rows = live
? kChatShellOutputCollapsedLines
: (_fitRows ?? kChatShellOutputCollapsedLines);
final line = chatShellTerminalLineHeight(context, style);
// A pixel of slack keeps rounding from losing the last row.
return SizedBox(height: rows * line + padding.vertical + 1, child: view);
}
}