writeString method

void writeString(
  1. int x,
  2. int y,
  3. String text,
  4. Style style,
)

Writes styled text to the buffer starting at (x, y).

Newlines (\n) will wrap to y + 1 at the starting column x. Any characters that fall out of bounds are clipped.

Implementation

void writeString(int x, int y, String text, Style style) {
  // Fast-path for pure ASCII text: no wide characters, no variation selectors,
  // and no multi-code-unit grapheme clusters.
  if (_isPureAscii(text)) {
    _writeAsciiString(x, y, text, style);
    return;
  }

  var currentX = x;
  var currentY = y;
  final fg = style.foreground?.argb ?? 0;
  final bg = style.background?.argb ?? 0;
  final ulColor = style.underlineColor?.argb ?? 0;
  final uri = style.uri;
  final modifiers = style.modifiers;
  final hasFg = style.foreground != null;
  final hasBg = style.background != null;

  for (final char in text.characters) {
    if (char == '\n') {
      currentX = x;
      currentY++;
      continue;
    }
    final charWidth = graphemeWidth(char);
    if (charWidth == 0) continue;
    _writeGraphemeAt(
      currentX,
      currentY,
      char,
      charWidth == 2,
      fg,
      bg,
      modifiers,
      ulColor,
      uri,
      hasFg,
      hasBg,
    );
    currentX += charWidth;
  }
}