import 'package:xterm/core.dart'; import 'chat_shell_run.dart'; /// Lines of scrollback one card's terminal keeps. const int chatShellTerminalScrollback = 5000; /// The terminal emulator state of one run: the xterm [terminal] fed with the /// run's output in order, and the bridge from what the operator types (or /// the emulator answers) to the running process. /// /// It keeps no copy of typed input: [Terminal.onOutput] hands each key or /// paste straight to [onInput], which sends it. class ChatShellTerminalSession { ChatShellTerminalSession( ChatShellRun run, { required this.onInput, required this.onResize, }) : terminal = _ChatShellTerminal() { // Drawn at the process's own size until a view lays it out at its own. if (run.cols > 0 && run.rows > 0) terminal.resize(run.cols, run.rows); terminal.onOutput = _output; terminal.onResize = (cols, rows, _, _) => onResize(cols, rows); replay(run); } final Terminal terminal; /// Receives input for the process; only called while the run is live. final void Function(String data) onInput; /// Receives the size the view laid the terminal out at. final void Function(int cols, int rows) onResize; int _end = 0; bool _live = false; bool _replaying = false; /// The absolute output byte this terminal has drawn up to. int get end => _end; /// Whether typing reaches a running process. bool get live => _live; /// Draws [run]'s held output from scratch: on open, after a gap and after /// a readback replaced the card's copy. Answers the emulator gives to old /// queries in that output are not sent: the process asked them long ago. void replay(ChatShellRun run) { _live = run.running && run.terminal; _replaying = true; try { terminal.useMainBuffer(); terminal.buffer.clear(); terminal.buffer.setCursor(0, 0); terminal.setCursorVisibleMode(true); terminal.write('\x1b[0m'); if (run.truncated) { terminal.write('\x1b[2m[earlier output was dropped]\x1b[0m\r\n'); } var output = _drawable(run, run.output); // A finished run's final newline would leave an empty line below it. if (!run.running && output.endsWith('\r\n')) { output = output.substring(0, output.length - 2); } terminal.write(output); if (!run.running) terminal.setCursorVisibleMode(false); } finally { _replaying = false; } _end = run.outputEnd; } /// Draws one live chunk covering bytes [offset, end) of [run], which /// already holds it. A chunk after a hole redraws the whole held copy. void append(ChatShellRun run, String text, int offset, int end) { if (end <= _end) return; if (offset != _end) { replay(run); return; } terminal.write(_drawable(run, text)); _end = end; } /// The run ended: nothing typed reaches it any more. void finish(ChatShellRun run) { if (!_live) return; _live = false; terminal.setCursorVisibleMode(false); } void _output(String data) { if (_live && !_replaying) onInput(data); } /// Plain-pipe output has bare newlines, which a terminal draws as a line /// feed without a carriage return. static String _drawable(ChatShellRun run, String text) => run.terminal ? text : text.replaceAll('\n', '\r\n'); } /// The emulator with the scrollback the cards keep. It answers a cursor /// position query (DSR 6) with the one-based row and column the protocol /// specifies; xterm.dart 4.0.0 itself answers zero-based. class _ChatShellTerminal extends Terminal { _ChatShellTerminal() : super(maxLines: chatShellTerminalScrollback); @override void sendCursorPosition() { onOutput?.call('\x1b[${buffer.cursorY + 1};${buffer.cursorX + 1}R'); } }