TerminalNativeCellDeltaFrame.between constructor

TerminalNativeCellDeltaFrame.between(
  1. TerminalNativeFrame? previous,
  2. TerminalNativeFrame current
)

Computes changed cells between previous and current.

Implementation

factory TerminalNativeCellDeltaFrame.between(
  TerminalNativeFrame? previous,
  TerminalNativeFrame current,
) {
  final lines = <TerminalNativeLineDelta>[];
  final previousLines = previous?.lines ?? const <TerminalNativeLine>[];
  final maxLineCount = current.lines.length > previousLines.length
      ? current.lines.length
      : previousLines.length;

  for (var y = 0; y < maxLineCount; y++) {
    final currentLine = y < current.lines.length ? current.lines[y] : null;
    final previousLine = y < previousLines.length ? previousLines[y] : null;
    final currentCells = currentLine?.cells ?? const <TerminalNativeCell>[];
    final previousCells = previousLine?.cells ?? const <TerminalNativeCell>[];
    final maxCellCount = currentCells.length > previousCells.length
        ? currentCells.length
        : previousCells.length;
    final deltas = <TerminalNativeCellDelta>[];

    for (var x = 0; x < maxCellCount; x++) {
      final prev = x < previousCells.length ? previousCells[x] : null;
      final next = x < currentCells.length ? currentCells[x] : null;
      if (_nativeCellEquals(prev, next)) continue;
      deltas.add(
        TerminalNativeCellDelta(column: x, previous: prev, current: next),
      );
    }

    if (deltas.isNotEmpty) {
      lines.add(
        TerminalNativeLineDelta(
          index: y,
          cells: List<TerminalNativeCellDelta>.unmodifiable(deltas),
        ),
      );
    }
  }

  return TerminalNativeCellDeltaFrame(
    width: current.width,
    height: current.height,
    lines: List<TerminalNativeLineDelta>.unmodifiable(lines),
  );
}