prependString method

void prependString(
  1. Buffer newbuf,
  2. String str
)

Implementation

void prependString(Buffer newbuf, String str) {
  // Upstream: `third_party/ultraviolet/terminal_renderer.go` (`PrependString`).
  if (str.isEmpty) return;

  final w = newbuf.width();
  final h = newbuf.height();
  _move(newbuf, 0, h - 1);

  final lines = str.split('\n');
  var offset = 0;
  for (final line in lines) {
    final lineWidth = WidthMethod.wcwidth.stringWidth(line);
    if (w > 0 && lineWidth > w) {
      offset += (lineWidth ~/ w);
    }
    if (lineWidth == 0 || (w > 0 && lineWidth % w != 0)) {
      offset++;
    }
  }

  if (offset <= 0) return;

  _buf.write(List.filled(offset, '\n').join());
  _cur.y += offset;

  // Move to top and insert new lines.
  _moveCursor(newbuf, 0, 0, false);
  _buf.write(UvAnsi.insertLine(offset));
  for (final line in lines) {
    _buf.write(line);
    _buf.write('\r\n');
  }
}