163 lines
5.2 KiB
Dart
163 lines
5.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.
|
|
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);
|
|
|
|
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('~')}';
|
|
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,
|
|
},
|
|
},
|
|
];
|
|
}
|