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) {
  // 1. Adjust the height.
  if (newHeight > oldHeight) {
    // Grow larger
    for (var i = 0; i < newHeight - oldHeight; i++) {
      if (newHeight > lines.length) {
        lines.push(_newEmptyLine(newWidth));
      } else {
        _cursorY++;
      }
    }
  } else {
    // Shrink smaller
    for (var i = 0; i < oldHeight - newHeight; i++) {
      if (_cursorY > newHeight - 1) {
        _cursorY--;
      } else {
        lines.pop();
      }
    }
  }

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

  // 2. Adjust the width.
  if (newWidth != oldWidth) {
    if (terminal.reflowEnabled && !isAltBuffer) {
      final reflowResult = reflow(lines, oldWidth, newWidth);

      while (reflowResult.length < newHeight) {
        reflowResult.add(_newEmptyLine(newWidth));
      }

      lines.replaceWith(reflowResult);
    } else {
      lines.forEach((item) => item.resize(newWidth));
    }
  }
}