render method

String render(
  1. int width
)

Renders whitespace of the given width using these options.

Implementation

String render(int width) {
  if (width <= 0) return '';

  final glyphs = uni.graphemes(chars).toList(growable: false);
  if (glyphs.isEmpty) return ' ' * width;

  final buffer = StringBuffer();
  var j = 0;
  var currentWidth = 0;

  // Cycle through grapheme clusters to fill the width
  while (currentWidth < width) {
    final glyph = glyphs[j];
    final glyphWidth = Layout.visibleLength(glyph);

    // Don't exceed width
    if (currentWidth + glyphWidth > width) break;

    buffer.write(glyph);
    currentWidth += glyphWidth;

    j = (j + 1) % glyphs.length;
  }

  // Fill any remaining gap with spaces
  if (currentWidth < width) {
    buffer.write(' ' * (width - currentWidth));
  }

  var result = buffer.toString();

  // Apply styling if needed
  if (foreground != null || background != null) {
    var style = Style();
    if (foreground != null) {
      style = style.foreground(foreground!);
    }
    if (background != null) {
      style = style.background(background!);
    }
    result = style.render(result);
  }

  return result;
}