print method

void print(
  1. int x,
  2. int y,
  3. String string,
  4. {Style style = Style.reset,
  5. int? maxWidth}
)

Prints a string, starting at the given x and y coordinates.

If maxWidth is provided, the string will be truncated to fit within the buffer's width, which otherwise defaults to the buffer's width if omitted.

Throws if the coordinates are out of bounds.

Implementation

void print(
  int x,
  int y,
  String string, {
  Style style = Style.reset,
  int? maxWidth,
}) {
  maxWidth ??= width - x;
  var remaining = math.min(string.characters.length, maxWidth);

  // Get all non-zero width characters up to the maximum width.
  final graphemes = string.characters.takeWhile((char) {
    final width = char.characters.length;
    if (width > remaining) {
      return false;
    }
    remaining -= width;
    return true;
  });

  // Print each grapheme to the buffer.
  var i = 0;
  for (final grapheme in graphemes) {
    set(x + i, y, Cell(grapheme, style));
    i += grapheme.characters.length;
  }
}