encodeRemotePlan function
Encodes a RemotePlan to wire bytes.
Wire shape (the cell-patch design): only the changed column ranges ship, each as runs grouped by style, with styles deduplicated into a per-frame table referenced by varint index. Integers are varints. This keeps the wire close to "only changed cells, style once per run" — the efficiency ANSI gets implicitly — so it stays competitive with (and usually beats) a deflated ANSI stream over the socket.
Implementation
Uint8List encodeRemotePlan(RemotePlan plan) {
final w = _Writer();
var flags = 0;
if (plan.fullRepaint) flags |= 1;
if (plan.scrollUpRows != null) flags |= 2;
w
..u8(flags)
..varint(plan.size.cols)
..varint(plan.size.rows);
if (plan.scrollUpRows != null) w.varint(plan.scrollUpRows!);
w.varint(plan.styleTable.length);
for (final style in plan.styleTable) {
_writeStyle(w, style);
}
w.varint(plan.patches.length);
for (final patch in plan.patches) {
w
..varint(patch.row)
..varint(patch.startCol)
..varint(patch.runs.length);
for (final run in patch.runs) {
w
..varint(run.styleIndex)
..vstr(run.text);
}
}
w.varint(plan.placements.length);
for (final p in plan.placements) {
w
..vstr(p.id)
..varint(p.row)
..varint(p.col)
..varint(p.cols)
..varint(p.rows)
..varint(p.fit.index)
..varint(p.boxCols)
..varint(p.boxRows)
..varint(p.boxOffsetCol)
..varint(p.boxOffsetRow);
}
return w.take();
}