dumpTable static method

String dumpTable(
  1. Iterable rows
)

Implementation

static String dumpTable(Iterable rows) {
  rows = _asList(rows);
  var mapRows = _asListMap(rows as List<dynamic>);
  var keys = _allKeys(mapRows);
  var colSizes = Map<String, int>();

  keys.forEach((k) {
    var max = k.length;
    mapRows.forEach((row) {
      var col = row![k];
      if (col != null) {
        var valSize = "${col}".length;
        if (valSize > max) {
          max = valSize;
        }
      }
    });
    colSizes[k] = max;
  });

  // sum + ' padding ' + |
  int rowWidth = colSizes.values.fold(0, (dynamic p, c) => p + c) +
      (colSizes.length * 2) +
      (colSizes.length + 1);
  var sb = <String>[];
  sb.add("+${'-' * (rowWidth - 2)}+");
  var head = '|';
  keys.forEach((k) {
    head += _alignCenter(k, colSizes[k]!) + '|';
  });
  sb.add(head);
  sb.add("|${'-' * (rowWidth - 2)}|");

  mapRows.forEach((row) {
    var to = '|';
    keys.forEach((k) {
      to += '' + _alignAuto(row![k], colSizes[k]!) + '|';
    });
    sb.add(to);
  });

  sb.add("+${'-' * (rowWidth - 2)}+");

  return sb.join('\n');
}