resize method

void resize(
  1. int newWidth,
  2. int newHeight
)

Resizes the buffer to the new dimensions, preserving existing content where it fits.

Implementation

void resize(int newWidth, int newHeight) {
  Tracer.record(_traceResizeId, Phase.begin, TraceCategory.layout);
  try {
    newWidth = max(0, newWidth);
    newHeight = max(0, newHeight);
    final newChars = List.filled(newWidth * newHeight, ' ');
    final newAttributes = Uint32List(newWidth * newHeight * 3);
    final oldUl = _underlineColors;
    final newUl = oldUl != null ? Uint32List(newWidth * newHeight) : null;
    final oldUris = _uris;
    final newUris = oldUris != null
        ? List<String?>.filled(newWidth * newHeight, null)
        : null;

    if (_emptyModifiers != 0) {
      for (var i = 2; i < newAttributes.length; i += 3) {
        newAttributes[i] = _emptyModifiers;
      }
    }

    final overlapWidth = min(width, newWidth);
    final overlapHeight = min(height, newHeight);

    if (overlapWidth > 0 && overlapHeight > 0) {
      _copyOverlappingRows(
        newWidth,
        overlapWidth,
        overlapHeight,
        newChars,
        newAttributes,
        oldUl,
        newUl,
        oldUris,
        newUris,
      );
    }

    width = newWidth;
    height = newHeight;
    characters = newChars;
    attributes = newAttributes;
    _underlineColors = newUl;
    _uris = newUris;
    _boundsClip = Rect(0, 0, width, height);
    _clipScopedRegistries();
  } finally {
    Tracer.record(_traceResizeId, Phase.end, TraceCategory.layout);
  }
}