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.
This commit is contained in:
@@ -1,119 +1,65 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../ui/text/monospace.dart';
|
||||
import '../conversation/chat_card_styles.dart';
|
||||
import 'chat_shell_metrics.dart';
|
||||
import 'chat_shell_run.dart';
|
||||
import 'chat_shell_runs_controller.dart';
|
||||
import 'chat_shell_terminal_keys.dart';
|
||||
import 'chat_shell_terminal_view.dart';
|
||||
|
||||
/// The terminal-style output area of one command card: the host's monospace
|
||||
/// body on a quiet inset surface, scrollable inside a bounded height and
|
||||
/// anchored at the newest line while the command runs.
|
||||
class ChatShellOutputView extends StatefulWidget {
|
||||
/// The output area of one command card: the run's terminal emulator, which
|
||||
/// takes typing while the command runs (with the key toolbar below it), or
|
||||
/// its retained output read-only once it ended.
|
||||
class ChatShellOutputView extends StatelessWidget {
|
||||
const ChatShellOutputView({
|
||||
super.key,
|
||||
required this.run,
|
||||
required this.controller,
|
||||
required this.cupertino,
|
||||
required this.expanded,
|
||||
required this.viewKey,
|
||||
required this.fullScreen,
|
||||
});
|
||||
|
||||
final ChatShellRun run;
|
||||
final ChatShellRunsController controller;
|
||||
final bool cupertino;
|
||||
final bool expanded;
|
||||
|
||||
@override
|
||||
State<ChatShellOutputView> createState() => _ChatShellOutputViewState();
|
||||
}
|
||||
/// Replaced when the full-screen view closes, so the card's view sizes the
|
||||
/// terminal to itself again.
|
||||
final Key viewKey;
|
||||
|
||||
class _ChatShellOutputViewState extends State<ChatShellOutputView> {
|
||||
final ScrollController _scroll = ScrollController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scroll.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
/// True while the full-screen view sizes the terminal.
|
||||
final bool fullScreen;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final run = widget.run;
|
||||
final cupertino = widget.cupertino;
|
||||
final body = chatCardBodyStyle(context, cupertino) ?? const TextStyle();
|
||||
final fontSize = (body.fontSize ?? 14) * 0.95;
|
||||
final style = body.copyWith(
|
||||
fontFamily: appMonospaceFamily,
|
||||
fontSize: fontSize,
|
||||
height: kChatShellOutputLineHeight,
|
||||
);
|
||||
final secondary = chatCardSecondaryStyle(context, cupertino);
|
||||
var text = run.output;
|
||||
final clipped = text.length > kChatShellRenderedOutputLimit;
|
||||
if (clipped) {
|
||||
text = text.substring(text.length - kChatShellRenderedOutputLimit);
|
||||
if (!run.running && run.output.isEmpty) {
|
||||
return Text(
|
||||
'No output.',
|
||||
key: const Key('chat-shell-output-empty'),
|
||||
style: chatCardSecondaryStyle(context, cupertino),
|
||||
);
|
||||
}
|
||||
// The final newline ends the last line; it is not an empty line to draw.
|
||||
if (text.endsWith('\n')) text = text.substring(0, text.length - 1);
|
||||
final notes = [
|
||||
if (run.truncated) 'Earlier output was dropped.',
|
||||
if (clipped)
|
||||
'Showing only the newest part; Copy output copies all of it.',
|
||||
];
|
||||
final session = controller.terminals.session(run);
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (notes.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 4),
|
||||
child: Text(
|
||||
notes.join(' '),
|
||||
key: const Key('chat-shell-output-truncated'),
|
||||
style: secondary,
|
||||
),
|
||||
),
|
||||
DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: chatCardFillColor(context, cupertino),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(
|
||||
maxHeight: chatShellOutputMaxHeight(
|
||||
context,
|
||||
fontSize: fontSize,
|
||||
expanded: widget.expanded,
|
||||
),
|
||||
),
|
||||
child: _scrollable(
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: text.isEmpty
|
||||
? Text(
|
||||
run.running ? 'No output yet.' : 'No output.',
|
||||
style: secondary,
|
||||
)
|
||||
: Text(
|
||||
text,
|
||||
key: const Key('chat-shell-output-text'),
|
||||
style: style,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
ChatShellTerminalView(
|
||||
key: viewKey,
|
||||
session: session,
|
||||
cupertino: cupertino,
|
||||
autoResize: !fullScreen,
|
||||
),
|
||||
if (session.live && !fullScreen)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: ChatShellTerminalKeys(
|
||||
session: session,
|
||||
cupertino: cupertino,
|
||||
field: chatShellTouchFirst(),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// A short output sizes the box to itself; a long one scrolls inside it,
|
||||
/// starting from (and following) the newest line.
|
||||
Widget _scrollable(Widget child) {
|
||||
final scroll = SingleChildScrollView(
|
||||
controller: _scroll,
|
||||
reverse: true,
|
||||
child: SizedBox(width: double.infinity, child: child),
|
||||
);
|
||||
return widget.cupertino
|
||||
? CupertinoScrollbar(controller: _scroll, child: scroll)
|
||||
: Scrollbar(controller: _scroll, child: scroll);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user