render method

  1. @override
String render()
override

Renders the component as a string.

Implementation

@override
String render() {
  final chars = borderStyle.chars;
  final lines = content.split('\n');
  final maxLen = lines
      .map((l) => Style.visibleLength(l))
      .reduce((a, b) => a > b ? a : b);
  final innerWidth = maxLen + (padding * 2);

  final buffer = StringBuffer();

  // Top border
  if (title != null) {
    final titlePart = '${chars.horizontal} $title ';
    final remaining = innerWidth - titlePart.length;
    buffer.writeln(
      '${chars.topLeft}$titlePart${chars.horizontal * remaining}${chars.topRight}',
    );
  } else {
    buffer.writeln(
      '${chars.topLeft}${chars.horizontal * innerWidth}${chars.topRight}',
    );
  }

  // Content
  final pad = ' ' * padding;
  for (final line in lines) {
    final lineLen = Style.visibleLength(line);
    final rightPad = ' ' * (maxLen - lineLen);
    buffer.writeln(
      '${chars.vertical}$pad$line$rightPad$pad${chars.vertical}',
    );
  }

  // Bottom border
  buffer.write(
    '${chars.bottomLeft}${chars.horizontal * innerWidth}${chars.bottomRight}',
  );

  return buffer.toString();
}