grid static method

String grid(
  1. List<List<String>> rows,
  2. PromptTheme theme
)

Produces a multi-column grid (two columns by default) with aligned keys.

This is the most legible format for dense keybinding lists because each column width is computed ahead of time using ANSI-aware math, ensuring columns never wobble when colors are applied.

Implementation

static String grid(List<List<String>> rows, PromptTheme theme) {
  final buffer = StringBuffer();
  final color = theme.gray;

  // Compute column widths for alignment
  final col1Width = rows.fold<int>(
      0, (w, row) => max(w, (row.isNotEmpty ? row[0].length : 0)));
  final col2Width = rows.fold<int>(
      0, (w, row) => max(w, row.length > 1 ? row[1].length : 0));

  buffer.writeln('${theme.dim}Controls:${theme.reset}');
  for (final row in rows) {
    final key = row.isNotEmpty ? row[0].padRight(col1Width + 2) : '';
    final action = row.length > 1 ? row[1].padRight(col2Width + 2) : '';
    buffer.writeln('  $color$key${theme.reset}$action');
  }
  return buffer.toString().trimRight();
}