import 'package:flutter/material.dart'; /// The width of a terminal transcript's glyph column: about two monospace /// cells of the ambient text, so lines after a glyph align like a CLI's. double terminalGlyphColumn(BuildContext context) { final style = DefaultTextStyle.of(context).style; final size = MediaQuery.textScalerOf(context).scale(style.fontSize ?? 14); return size * 1.3; } /// The height of one line of the ambient text, so a painted glyph centres on /// the first line of the text beside it. double terminalLineHeight(BuildContext context, [TextStyle? style]) { final ambient = style ?? DefaultTextStyle.of(context).style; final size = MediaQuery.textScalerOf(context).scale(ambient.fontSize ?? 14); return size * (ambient.height ?? 1.35); } /// The kind of mark a terminal transcript draws in its glyph column. They /// are painted rather than typed: the bundled monospace family has no /// record (U+25CF) or bracket corner (U+23BF), and the web engine would /// otherwise fetch a fallback font for them. enum TerminalGlyphKind { /// A filled dot: an answer, or a tool call (its colour gives the status). dot, /// A bracket corner under a call: its result line. elbow, } /// One painted terminal glyph, a glyph column wide and one text line tall, /// in [color]. class TerminalGlyph extends StatelessWidget { const TerminalGlyph(this.kind, {super.key, required this.color, this.style}); final TerminalGlyphKind kind; final Color color; /// The text the glyph sits beside; the ambient style when null. final TextStyle? style; @override Widget build(BuildContext context) => ExcludeSemantics( child: SizedBox( width: terminalGlyphColumn(context), height: terminalLineHeight(context, style), child: CustomPaint(painter: _GlyphPainter(kind, color)), ), ); } class _GlyphPainter extends CustomPainter { const _GlyphPainter(this.kind, this.color); final TerminalGlyphKind kind; final Color color; @override void paint(Canvas canvas, Size size) { final h = size.height; final paint = Paint() ..color = color ..isAntiAlias = true; switch (kind) { case TerminalGlyphKind.dot: canvas.drawCircle(Offset(h * 0.3, h / 2), h * 0.17, paint); case TerminalGlyphKind.elbow: paint ..style = PaintingStyle.stroke ..strokeWidth = 1; final x = h * 0.3; final path = Path() ..moveTo(x, 0) ..lineTo(x, h * 0.55) ..lineTo(size.width * 0.95, h * 0.55); canvas.drawPath(path, paint); } } @override bool shouldRepaint(_GlyphPainter old) => old.kind != kind || old.color != color; } /// [child] after a [glyph] in the glyph column, the glyph centred on the /// child's first line. class TerminalGlyphRow extends StatelessWidget { const TerminalGlyphRow({super.key, required this.glyph, required this.child}); final Widget glyph; final Widget child; @override Widget build(BuildContext context) => Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ glyph, Expanded(child: child), ], ); }