resize method

void resize(
  1. int width,
  2. int height
)

Resizes the buffer to width × height, preserving content where possible.

Implementation

void resize(int width, int height) {
  if (width < 0 || height <= 0) {
    lines.clear();
    touched = <LineData?>[];
    return;
  }

  final oldHeight = lines.length;
  final oldWidth = oldHeight == 0 ? 0 : lines[0].length;

  // Resize height.
  if (height > oldHeight) {
    for (var i = oldHeight; i < height; i++) {
      lines.add(Line.filled(width));
    }
  } else if (height < oldHeight) {
    lines.removeRange(height, oldHeight);
  }

  // Resize width (rebuild lines to keep wide-placeholder invariants simple).
  if (width != oldWidth && lines.isNotEmpty) {
    for (var y = 0; y < lines.length; y++) {
      final newLine = Line.filled(width);
      final copyWidth = width < oldWidth ? width : oldWidth;
      for (var x = 0; x < copyWidth; x++) {
        newLine.cells[x] = lines[y].cells[x].clone();
      }
      lines[y] = newLine;
    }
  }

  touched = List<LineData?>.filled(lines.length, null);
}