Files
pipelines/app/lib/features/chat/shell/chat_shell_terminal_theme.dart
T
alan 018d37f103 Terminal: grapheme-aware widths, drawn box/Powerline glyphs, line pictures
Emulator (vendored xterm):
- Widths follow glibc wcwidth on generated Unicode 16 tables
  (tool/gen_unicode_tables.py); combining marks, variation selectors,
  ZWJ sequences, skin tones, flags and Hangul jamo join the previous cell,
  stored as interned clusters (CellContent.clusterFlag) and drawn and
  copied whole. U+FE0F and flags take two columns, as modern terminals
  do; a wide character never starts in the last column.
- Box drawing, block elements and Powerline separators are drawn as
  geometry filling the cell; rows are whole pixels high.
- Backgrounds merge per colour; each line is a picture reused until the
  line changes (BufferLine.version); a line drawn unchanged settles into
  one laid out paragraph with a span per style. TerminalStyle and
  TerminalTheme compare by value.

App: bundled Symbols Nerd Font Mono subset (568 KiB, MIT) as the
terminal's first fallback, loaded on first use; line height 1.1;
?terminalUnicode=1 fixture showcase; bench gains a progress workload.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 01:14:48 +00:00

97 lines
3.7 KiB
Dart

import 'dart:async';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:xterm/xterm.dart';
import '../../../ui/text/monospace.dart';
import '../../../ui/text/terminal_symbols_font.dart';
import '../conversation/chat_card_styles.dart';
/// The terminal's colours from the host theme: the card's inset surface as
/// background, the body text colour as foreground, the accent as cursor and
/// selection. The sixteen ANSI colours are the emulator's standard palette,
/// darkened on a light surface so coloured output stays readable.
TerminalTheme chatShellTerminalTheme(BuildContext context) {
final dark = Theme.of(context).brightness == Brightness.dark;
final background = chatCardFillColor(context);
final foreground = Theme.of(context).colorScheme.onSurface;
final accent = Theme.of(context).colorScheme.primary;
final base = TerminalThemes.defaultTheme;
Color ansi(Color color) {
if (dark) return color;
final hsl = HSLColor.fromColor(color);
return hsl.lightness > 0.4 ? hsl.withLightness(0.36).toColor() : color;
}
return TerminalTheme(
cursor: accent,
selection: accent.withValues(alpha: 0.35),
foreground: foreground,
// Opaque: the emulator paints cells over its own background.
background: Color.alphaBlend(
background,
Theme.of(context).colorScheme.surface,
),
black: dark ? base.black : foreground,
red: ansi(base.red),
green: ansi(base.green),
yellow: ansi(base.yellow),
blue: ansi(base.blue),
magenta: ansi(base.magenta),
cyan: ansi(base.cyan),
white: dark ? base.white : ansi(base.brightBlack),
brightBlack: ansi(base.brightBlack),
brightRed: ansi(base.brightRed),
brightGreen: ansi(base.brightGreen),
brightYellow: ansi(base.brightYellow),
brightBlue: ansi(base.brightBlue),
brightMagenta: ansi(base.brightMagenta),
brightCyan: ansi(base.brightCyan),
brightWhite: dark ? base.brightWhite : foreground,
searchHitBackground: base.searchHitBackground,
searchHitBackgroundCurrent: base.searchHitBackgroundCurrent,
searchHitForeground: base.searchHitForeground,
);
}
/// The terminal's text: the host's monospace family at the card body size,
/// with the bundled symbols font first among the fallbacks for Powerline and
/// Nerd Font icons (registered on first use).
///
/// Line height 1.1: the emulator rounds a row up to whole pixels (about
/// 1.13 at the default size), close to other terminals' 1.0-1.2. Box
/// drawing, block and Powerline characters fill the whole row whatever the
/// height, so frames and bars join across rows.
TerminalStyle chatShellTerminalStyle(BuildContext context) {
unawaited(ensureTerminalSymbolsFont());
final body = chatCardBodyStyle(context) ?? const TextStyle();
return TerminalStyle(
fontFamily: appMonospaceFamily,
fontFamilyFallback: const [
terminalSymbolsFamily,
...kTerminalDefaultFontFamilyFallback,
],
fontSize: (body.fontSize ?? 14) * 0.95,
height: 1.1,
);
}
/// The size of one terminal cell in [style] at the context's text scale,
/// measured exactly as the emulator measures it.
Size chatShellTerminalCellSize(BuildContext context, TerminalStyle style) {
final text = style.toTextStyle();
final builder = ui.ParagraphBuilder(text.getParagraphStyle())
..pushStyle(text.getTextStyle(textScaler: MediaQuery.textScalerOf(context)))
..addText('mmmmmmmmmm');
final paragraph = builder.build()
..layout(const ui.ParagraphConstraints(width: double.infinity));
// Rows are whole pixels, as the emulator's painter rounds them.
final size = Size(
paragraph.maxIntrinsicWidth / 10,
paragraph.height.ceilToDouble(),
);
paragraph.dispose();
return size;
}