writeTable static method

CappConsole writeTable(
  1. List<List<String>> data, {
  2. bool dubleBorder = false,
  3. CappColors color = CappColors.error,
})

writeTable method is used to print a table in the console. The data is the list of lists of strings that will be shown in the table. The dubleBorder is a boolean that indicates if the table has a double border or not. The color is the color of the table.

Implementation

static CappConsole writeTable(
  List<List<String>> data, {
  bool dubleBorder = false,
  CappColors color = CappColors.error,
}) {
  String res = '';
  String set = dubleBorder ? "═║╔╦╗╠╬╣╚╩╝" : "─│┌┬┐├┼┤└┴┘";
  List<int> columnWidths = List.filled(data[0].length, 0);
  for (var row in data) {
    for (int i = 0; i < row.length; i++) {
      columnWidths[i] =
          row[i].length > columnWidths[i] ? row[i].length : columnWidths[i];
    }
  }

  res += _printBorder(columnWidths, set[2], set[4], false, true, set);

  for (var i = 0; i < data.length; i++) {
    StringBuffer buffer = StringBuffer();
    buffer.write(set[1]);
    for (int j = 0; j < data[i].length; j++) {
      var cell = (i == 0)
          ? data[i][j]
              .padLeft((columnWidths[j] + data[i][j].length) ~/ 2)
              .padRight((columnWidths[j]))
          : data[i][j].padRight(columnWidths[j]);
      buffer.write(" " + cell + " ");
      buffer.write(set[1]);
    }
    res += buffer.toString() + '\n';

    if (i < data.length - 1) {
      if (columnWidths.length - 2 != i) {
        res += _printBorder(columnWidths, set[5], set[7], false, false, set);
      } else {
        res += _printBorder(columnWidths, set[5], set[7], false, false, set);
      }
    }
  }

  res += _printBorder(columnWidths, set[8], set[10], true, false, set);
  return write(res, color);
}