import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/terminal/terminal_layout.dart'; import 'package:pipelines_app/features/terminal/terminal_machines.dart'; /// One machine's terminals as a server holds them, answering the terminal /// requests and pushing frames like the real one. class TerminalGateway implements ApiGateway { TerminalGateway({this.stream = 's1'}); final _frames = StreamController.broadcast(); final List requests = []; final Map terminals = {}; final Map output = {}; String stream; int _next = 0; /// When set, requests of this type fail with this message. final Map refuse = {}; @override Stream get frames => _frames.stream; List bodies(String type) => [ for (final request in requests) if (request['type'] == type) Map.from(request['terminal_request'] as Map? ?? {}), ]; JsonMap add(String id, {String cwd = '/Users/op', String text = ''}) { final record = { 'id': id, 'cwd': cwd, 'shell': '/bin/zsh', 'status': 'running', 'cols': 80, 'rows': 24, 'created_at': 1, }; terminals[id] = record; output[id] = text; return record; } JsonMap _record(String id, {bool withOutput = false, int after = 0}) { final text = output[id] ?? ''; final bytes = utf8.encode(text); final start = after.clamp(0, bytes.length); return { ...terminals[id]!, 'stream': stream, 'output_end': bytes.length, if (withOutput) 'output': utf8.decode(bytes.sublist(start)), if (withOutput) 'output_start': start, }; } /// The shell writes [text]: it is retained and pushed. void write(String id, String text, {bool push = true}) { final offset = utf8.encode(output[id] ?? '').length; output[id] = (output[id] ?? '') + text; if (push) { pushOutput(id, text, offset); } } void pushOutput(String id, String text, int offset) => _frames.add({ 'type': 'terminal_event', 'terminal_event': { 'terminal_id': id, 'output': text, 'offset': offset, 'end': offset + utf8.encode(text).length, }, }); void lifecycle(String id, Map changes) { terminals[id] = {...terminals[id]!, ...changes}; _frames.add({ 'type': 'terminal_event', 'terminal_event': {'terminal_id': id, 'terminal': _record(id)}, }); } @override Future call(String type, [JsonMap fields = const {}]) async { requests.add({'type': type, ...fields}); final refusal = refuse[type]; if (refusal != null) throw StateError(refusal); final body = (fields['terminal_request'] as Map?) ?? const {}; final id = '${body['terminal_id'] ?? ''}'; switch (type) { case 'terminal_list': return { 'type': type, 'terminals': [for (final id in terminals.keys) _record(id)], }; case 'terminal_open': final next = 'term_${++_next}'; final cwd = '${body['cwd'] ?? ''}'; add(next, cwd: cwd.isEmpty ? '/Users/op' : cwd, text: r'$ '); return {'type': type, 'terminal': _record(next)}; case 'terminal_output': return { 'type': type, 'terminal': _record( id, withOutput: true, after: (body['after'] as num?)?.toInt() ?? 0, ), }; case 'terminal_close': final record = {..._record(id), 'status': 'closed'}; terminals.remove(id); return {'type': type, 'terminal': record}; case 'terminal_input': return {'type': type}; case 'terminal_resize': return {'type': type, 'terminal': _record(id)}; } throw StateError('unexpected $type'); } Future close() => _frames.close(); } /// A machine list tests change by hand. class TestMachines extends ValueNotifier> { TestMachines(super.value); } TerminalMachine machine(String id, TerminalGateway gateway, {String? name}) => TerminalMachine(id: id, name: name ?? id, gateway: gateway); /// A layout store kept in memory. class MemoryLayoutStore implements TerminalLayoutStore { String? saved; int saves = 0; @override Future load() async => saved; @override Future save(String value) async { saved = value; saves++; return true; } }