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'; /// 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 { const ChatShellOutputView({ super.key, required this.run, required this.cupertino, required this.expanded, }); final ChatShellRun run; final bool cupertino; final bool expanded; @override State createState() => _ChatShellOutputViewState(); } class _ChatShellOutputViewState extends State { final ScrollController _scroll = ScrollController(); @override void dispose() { _scroll.dispose(); super.dispose(); } @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); } // 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.', ]; return Column( crossAxisAlignment: CrossAxisAlignment.stretch, 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, ), ), ), ), ), ], ); } /// 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); } }