e13cb99ade
A headless xterm-compatible emulator (internal/termstate) follows each machine terminal's PTY output under the same lock as the 1 MiB tail. A terminal_output from before the retained tail (new page, reload, gap) now gets output_snapshot: an escape stream that resets the client and restores scrollback (1000 lines), main screen, the alternate screen when in use (via ?1049h so the program's exit brings the shell back), saved cursors, tabs, scroll region, cursor incl. pending wrap, modes (mouse tracking/encoding, bracketed paste, DECCKM, keypad, autowrap, origin, focus, ...), charsets, pen and title, as of output_end; frames continue there. Reads inside the tail stay raw. No Go library was adequate: charmbracelet/x/vt and vito/midterm scroll at 0.2-4 MB/s and hide modes/pen/region; vt10x is unmaintained. termstate runs at 160-200 MB/s. The app treats a snapshot reply as a replay without the dropped-output line, and every replay now starts with RIS so stale modes end. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
2.6 KiB
Dart
81 lines
2.6 KiB
Dart
import 'dart:convert';
|
|
|
|
/// Characters of output one tab holds before its oldest are dropped (about
|
|
/// the server's 1 MiB tail).
|
|
const int terminalHeldLimit = 1 << 20;
|
|
|
|
/// The text of [text] after its first [bytes] UTF-8 bytes (a cut inside a
|
|
/// character drops that character's remaining bytes).
|
|
String terminalSkipBytes(String text, int bytes) {
|
|
if (bytes <= 0) return text;
|
|
final encoded = utf8.encode(text);
|
|
if (bytes >= encoded.length) return '';
|
|
var cut = bytes;
|
|
while (cut < encoded.length && (encoded[cut] & 0xC0) == 0x80) {
|
|
cut++;
|
|
}
|
|
return utf8.decode(encoded.sublist(cut), allowMalformed: true);
|
|
}
|
|
|
|
/// One terminal's output as the client holds it: the text covering
|
|
/// absolute bytes [start, end) of the server's stream named [stream].
|
|
class TerminalStream {
|
|
String text = '';
|
|
int start = 0;
|
|
int end = 0;
|
|
String stream = '';
|
|
|
|
/// Whether [text] starts with the server's snapshot of the terminal's
|
|
/// state at [start] rather than raw output (until its start is dropped).
|
|
bool snapshot = false;
|
|
|
|
/// Whether nothing was received yet.
|
|
bool get empty => stream.isEmpty && end == 0;
|
|
|
|
/// Replaces the held copy with a read of the server's retained tail,
|
|
/// covering bytes [outputStart, outputEnd) (the server's offsets: the
|
|
/// text's own UTF-8 length differs where invalid bytes were replaced), or
|
|
/// with its [snapshot] of the terminal at [outputStart].
|
|
void reset(
|
|
String output,
|
|
int outputStart,
|
|
int outputEnd,
|
|
String name, {
|
|
bool snapshot = false,
|
|
}) {
|
|
text = output;
|
|
start = outputStart;
|
|
end = outputEnd;
|
|
stream = name;
|
|
this.snapshot = snapshot;
|
|
_bound();
|
|
}
|
|
|
|
/// Adds the part of [chunk] (covering [offset, chunkEnd)) past [end] and
|
|
/// returns it; null when the chunk starts after [end] (a gap).
|
|
String? add(String chunk, int offset, int chunkEnd) {
|
|
if (chunkEnd <= end) return '';
|
|
if (offset > end) return null;
|
|
final fresh = terminalSkipBytes(chunk, end - offset);
|
|
text += fresh;
|
|
end = chunkEnd;
|
|
_bound();
|
|
return fresh;
|
|
}
|
|
|
|
void _bound() {
|
|
if (text.length <= terminalHeldLimit + terminalHeldLimit ~/ 2) return;
|
|
var cut = text.length - terminalHeldLimit;
|
|
// Never split a surrogate pair.
|
|
final unit = text.codeUnitAt(cut);
|
|
if (unit >= 0xDC00 && unit <= 0xDFFF) cut++;
|
|
final dropped = text.substring(0, cut);
|
|
text = text.substring(cut);
|
|
snapshot = false;
|
|
// The server's offsets are not re-derived from the text: only the
|
|
// dropped part is measured (approximately, for replaced bytes).
|
|
start += utf8.encode(dropped).length;
|
|
if (start > end) start = end;
|
|
}
|
|
}
|