Files
pipelines/app/tool/perf/terminal_fixture.dart
T
alan b59ab5fad7 Terminal round 3: readable palettes, panel and card sizing, rail, tabs
- Colours: terminal background is the theme's lowest surface; the 16 ANSI
  colours come from a chosen palette (Theme colours, Classic, Calm, Vivid;
  Settings > Terminal > Colours, saved as `palette`) adjusted in lightness
  to the background: colours 4.5:1, bright ones lighter than normal,
  white light and black dark in both themes so white-on-red/blue/magenta
  and black-on-green/yellow/cyan (ls setuid, setgid, sticky) keep 3:1.
  Selection, find matches and the current match keep text at 4.5:1.
- Bottom panel opens at 40% of the window, at least 16 rows, until
  dragged; the dragged height is remembered (old saved 300 px default
  reads as never resized).
- Navigation rail scrolls its tabs (and below 240 px Settings too), so
  items never overlap under a tall panel.
- Chat command cards: Expand / Collapse grows a live terminal to 60% of
  the window (finished output only to its rows); collapsed stays 12 rows.
- Terminal tabs: ellipsised titles (220 px) with the whole title in the
  tooltip, selected tab filled, outlined and bold, close buttons are
  IconButtons (48 px touch, 40 px desktop).
- Capture fixture: ?terminalColors=1 and ?terminalPalette=.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 02:25:16 +00:00

250 lines
9.2 KiB
Dart

// Machine terminals for the full-app probe (`?terminals=1`): the machine
// lists two running tmux-backed shells (a coloured `go test` run and a
// home shell) and terminal_open/input/resize/output/close answer as the
// server does, with the output frames that follow. Typed text is echoed and
// Enter prints a fresh prompt. `?terminal=panel` or `?terminal=float` opens
// the terminal at launch in that mode; `?terminalPalette=classic` (calm,
// vivid) picks a colour palette.
import 'dart:convert';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/terminal/terminal_layout.dart';
bool _enabled = false;
final Map<String, JsonMap> _terminals = {};
final Map<String, String> _output = {};
int _opened = 0;
const String _prompt = '\x1b[1;32moperator@mac\x1b[0m:\x1b[1;34m%s\x1b[0m\$ ';
String _promptAt(String folder) => _prompt.replaceFirst('%s', folder);
/// `?terminalAttrs=1`: emulator features to capture (app/third_party/xterm),
/// printed after the build terminal's prompt. The DCS, APC and OSC strings
/// must not print; the cursor ends on the `b` of `abc` as a block.
const String _attributeShowcase =
'printf showcase\r\n'
'\x1b[4:3mcurly 4:3\x1b[0m \x1b[21mdouble 21\x1b[0m '
'\x1b[4:4mdotted\x1b[0m \x1b[9mstrike 9\x1b[0m '
'\x1b[53moverline\x1b[0m [\x1b[8mhidden\x1b[0m]\r\n'
'\x1b[38:2::255:128:0morange 38:2::\x1b[0m '
'\x1b[48:5:24m bg 48:5:24 \x1b[0m '
'\x1bP\$qm\x1b\\\x1b_apc\x1b\\'
'\x1b]8;;https://example.com\x1b\\link\x1b]8;;\x1b\\ '
'\x1b]11;#000000\x07after strings\r\n'
'abc\x1b[2D';
/// `?terminalUnicode=1`: widths, grapheme clusters, box drawing, blocks,
/// Powerline and Nerd Font glyphs, printed after the build terminal's
/// prompt. Each cluster takes the columns its width says (combining marks
/// none, emoji and CJK two), so the first three lines' `|` fall in columns
/// 6, 11 and 12.
const String _unicodeShowcase =
'printf unicode\r\n'
'e\u0301 n\u0303 a\u0300\u0316 |\r\n'
'\u4E2D\u6587 \u{1F600} \u{1F44D}\u{1F3FD} |\r\n'
'\u{1F468}\u200D\u{1F469}\u200D\u{1F467} \u{1F1F5}\u{1F1F9} '
'\u2764\uFE0F 1\uFE0F\u20E3 |\r\n'
'\u250C\u2500\u252C\u2500\u2510 \u250F\u2501\u2533\u2501\u2513 '
'\u2554\u2550\u2566\u2550\u2557 \u256D\u2500\u2500\u2500\u256E '
'\u2588\u2589\u258A\u258B\u258C\u258D\u258E\u258F '
'\u2581\u2582\u2583\u2584\u2585\u2586\u2587\u2588\r\n'
'\u251C\u2500\u253C\u2500\u2524 \u2523\u2501\u254B\u2501\u252B '
'\u2560\u2550\u256C\u2550\u2563 \u2502 \u2502 '
'\u2591\u2591\u2592\u2592\u2593\u2593\u2588\u2588 '
'\u2596\u2597\u2598\u2599\u259A\u259B\u259C\u259D\u259E\u259F\r\n'
'\u2514\u2500\u2534\u2500\u2518 \u2517\u2501\u253B\u2501\u251B '
'\u255A\u2550\u2569\u2550\u255D \u2570\u2500\u2500\u2500\u256F '
'\u2504\u2504 \u2506 \u254C\u254C \u2571\u2572\u2573\r\n'
'\x1b[30;44m \uE0A0 main \x1b[34;42m\uE0B0\x1b[30m ~/src \x1b[32;49m\uE0B0'
'\x1b[0m \x1b[35m\uE0B6\x1b[30;45mok\x1b[35;49m\uE0B4\x1b[0m '
'\x1b[33m\uE0B2\x1b[30;43m 12:00 \x1b[33;49m\uE0BC\x1b[0m '
'\uE0B1 \uE0B3 |\r\n'
'icons: \uF418 \uF09B \uF303 \uE615 \u23FB |\r\n';
/// `?terminalColors=1`: the sixteen ANSI colours as text on the default
/// background, as backgrounds under the default, black and white text, and
/// the `ls` colour pairs that use them (setuid white on red, setgid black
/// on yellow, sticky other-writable black on green, other-writable blue on
/// green), printed into the home terminal after its prompt.
String _colourShowcase() {
final out = StringBuffer('printf colours\r\n');
const names = ['blk', 'red', 'grn', 'yel', 'blu', 'mag', 'cyn', 'wht'];
for (final base in [30, 90]) {
for (var i = 0; i < 8; i++) {
out.write('\x1b[${base + i}m${names[i]}${base + i}\x1b[0m ');
}
out.write('\r\n');
}
for (final fg in ['39', '30', '37', '97']) {
for (final base in [40, 100]) {
for (var i = 0; i < 8; i++) {
out.write('\x1b[$fg;${base + i}m${fg.padLeft(2)}/${base + i}\x1b[0m');
}
out.write(' ');
}
out.write('\r\n');
}
out.write(
'\x1b[37;41msudo\x1b[0m \x1b[30;43mwall\x1b[0m '
'\x1b[30;42mtmp\x1b[0m \x1b[34;42mshared\x1b[0m '
'\x1b[1;34mbold-dir\x1b[0m \x1b[1;32mrun.sh\x1b[0m '
'\x1b[2mdim\x1b[0m \x1b[7minverse\x1b[0m\r\n',
);
return out.toString();
}
void configureTerminalFixture(Map<String, String> params) {
_enabled = params['terminals'] == '1';
if (!_enabled) return;
final now = DateTime.now().millisecondsSinceEpoch;
_terminals
..clear()
..['term_build'] = _record(
'term_build',
'/Users/operator/src/pipelines',
now - 600000,
title: 'go test ./internal/server',
)
..['term_home'] = _record('term_home', '/Users/operator', now - 300000);
_output
..clear()
..['term_build'] =
'${_promptAt('~/src/pipelines')}go test ./internal/server -run Terminal\r\n'
'\x1b[32m=== RUN\x1b[0m TestTerminalOverWebsocket\r\n'
'\x1b[32m--- PASS\x1b[0m: TestTerminalOverWebsocket (0.19s)\r\n'
'\x1b[32m=== RUN\x1b[0m TestTerminalTmuxSurvivesRestart\r\n'
'\x1b[32m--- PASS\x1b[0m: TestTerminalTmuxSurvivesRestart (0.27s)\r\n'
'\x1b[33m--- SKIP\x1b[0m: TestTerminalWindows (0.00s)\r\n'
'\x1b[1;32mok\x1b[0m \tgit.a-wan.net/alan/pipelines/internal/server\t2.758s\r\n'
'${_promptAt('~/src/pipelines')}'
..['term_home'] =
'${_promptAt('~')}ls\r\n'
'\x1b[1;34mDesktop\x1b[0m \x1b[1;34mDocuments\x1b[0m '
'\x1b[1;34mDownloads\x1b[0m \x1b[1;34msrc\x1b[0m notes.md\r\n'
'${_promptAt('~')}';
if (params['terminalAttrs'] == '1') {
_output['term_build'] = _output['term_build']! + _attributeShowcase;
}
if (params['terminalColors'] == '1') {
_output['term_home'] = _output['term_home']! + _colourShowcase();
}
if (params['terminalUnicode'] == '1') {
_output['term_build'] = _output['term_build']! + _unicodeShowcase;
}
final mode = switch (params['terminal']) {
'panel' => TerminalMode.panel,
'float' => TerminalMode.float,
_ => null,
};
final palette = TerminalPalette.values.asNameMap()[params['terminalPalette']];
if (palette != null) TerminalLayout.instance.palette = palette;
if (mode != null) {
TerminalLayout.instance
..mode = mode
..visible = true;
}
}
JsonMap _record(String id, String cwd, int created, {String title = ''}) => {
'id': id,
'cwd': cwd,
'title': title,
'shell': '/bin/zsh',
'created_at': created,
'tmux': 'pipelines-$id',
'status': 'running',
'cols': 100,
'rows': 24,
};
JsonMap _withOutput(String id, int after) {
final bytes = utf8.encode(_output[id] ?? '');
final start = after.clamp(0, bytes.length);
return {
..._terminals[id]!,
'stream': 'fixture',
'output_end': bytes.length,
'output': utf8.decode(bytes.sublist(start)),
'output_start': start,
};
}
JsonMap _body(JsonMap request) =>
Map<String, dynamic>.from(request['terminal_request'] as Map? ?? {});
JsonMap? terminalFixtureReply(JsonMap request) {
if (!_enabled) return null;
final id = request['id'];
final type = '${request['type']}';
final body = _body(request);
final terminal = '${body['terminal_id'] ?? ''}';
switch (type) {
case 'terminal_list':
return {
'id': id,
'type': type,
'terminals': [
for (final key in _terminals.keys)
{..._terminals[key]!, 'stream': 'fixture'},
],
};
case 'terminal_open':
final next = 'term_new${++_opened}';
final cwd = '${body['cwd'] ?? ''}';
_terminals[next] = _record(
next,
cwd.isEmpty ? '/Users/operator' : cwd,
DateTime.now().millisecondsSinceEpoch,
);
_output[next] = _promptAt(cwd.isEmpty ? '~' : cwd);
return {'id': id, 'type': type, 'terminal': _terminals[next]};
case 'terminal_output':
if (!_terminals.containsKey(terminal)) break;
return {
'id': id,
'type': type,
'terminal': _withOutput(
terminal,
(body['after'] as num?)?.toInt() ?? 0,
),
};
case 'terminal_input':
case 'terminal_resize':
return {'id': id, 'type': type};
case 'terminal_close':
final record = _terminals.remove(terminal);
return {
'id': id,
'type': type,
'terminal': {...?record, 'status': 'closed'},
};
}
return null;
}
/// The output the shell writes after typed input: the echo, and a new
/// prompt on Enter.
List<JsonMap> terminalFixtureFollowUps(JsonMap request) {
if (!_enabled || request['type'] != 'terminal_input') return const [];
final body = _body(request);
final terminal = '${body['terminal_id'] ?? ''}';
if (!_terminals.containsKey(terminal)) return const [];
final data = '${body['data'] ?? ''}';
final folder = terminal == 'term_build' ? '~/src/pipelines' : '~';
final echo = data.replaceAll('\r', '\r\n${_promptAt(folder)}');
final offset = utf8.encode(_output[terminal]!).length;
_output[terminal] = _output[terminal]! + echo;
return [
{
'type': 'terminal_event',
'terminal_event': {
'terminal_id': terminal,
'output': echo,
'offset': offset,
'end': offset + utf8.encode(echo).length,
},
},
];
}