prettyPrint method

void prettyPrint()

Pretty-print as ASCII table (for REPL / testing).

Implementation

void prettyPrint() {
  if (message != null) {
    print(message);
    return;
  }
  if (rows.isEmpty) {
    print('(empty)  affected=$affectedRows');
    return;
  }
  final cols   = rows.first.keys.toList();
  final widths = {for (final c in cols) c: c.length};
  for (final row in rows) {
    for (final c in cols) {
      final len = '${row[c]}'.length;
      if (len > widths[c]!) widths[c] = len;
    }
  }
  final sep    = '+${cols.map((c) => '-' * (widths[c]! + 2)).join('+')}+';
  final header =
      '|${cols.map((c) => ' ${c.padRight(widths[c]!)} ').join('|')}|';

  print(sep);
  print(header);
  print(sep);
  for (final row in rows) {
    print('|${cols.map((c) => ' ${"${row[c]}".padRight(widths[c]!)} ').join('|')}|');
  }
  print(sep);
  print('(${rows.length} row${rows.length == 1 ? '' : 's'})');
}