resize method

void resize(
  1. int oldWidth,
  2. int oldHeight,
  3. int newWidth,
  4. int newHeight,
)

Implementation

void resize(int oldWidth, int oldHeight, int newWidth, int newHeight) {
  if (newWidth > oldWidth) {
    lines.forEach((item) => item.ensure(newWidth));
  }

  if (newHeight > oldHeight) {
    while (lines.length < newHeight) {
      lines.push(_newEmptyLine());
    }
    // Grow larger
    for (var i = 0; i < newHeight - oldHeight; i++) {
      if (_cursorY < oldHeight - 1) {
        lines.push(_newEmptyLine());
      } else {
        _cursorY++;
      }
    }
  } else {
    // Shrink smaller
    for (var i = 0; i < oldHeight - newHeight; i++) {
      if (_cursorY < oldHeight - 1) {
        lines.pop();
      } else {
        _cursorY++;
      }
    }
  }

  // Ensure cursor is within the screen.
  _cursorX = _cursorX.clamp(0, newWidth - 1);
  _cursorY = _cursorY.clamp(0, newHeight - 1);

  if (!isAltBuffer) {
    final reflowStrategy = newWidth > oldWidth
        ? ReflowStrategyWider(this)
        : ReflowStrategyNarrower(this);
    reflowStrategy.reflow(newWidth, newHeight, oldWidth, oldHeight);
  }
}