018d37f103
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>
212 lines
7.7 KiB
Dart
212 lines
7.7 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.
|
|
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';
|
|
|
|
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['terminalUnicode'] == '1') {
|
|
_output['term_build'] = _output['term_build']! + _unicodeShowcase;
|
|
}
|
|
final mode = switch (params['terminal']) {
|
|
'panel' => TerminalMode.panel,
|
|
'float' => TerminalMode.float,
|
|
_ => null,
|
|
};
|
|
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,
|
|
},
|
|
},
|
|
];
|
|
}
|