withStatusColumn static method

Table withStatusColumn(
  1. List<String> headers,
  2. List<List<Object?>> rows, {
  3. int statusColumn = -1,
  4. Map<String, Color>? statusColors,
})

Creates a table with status column styling.

Automatically colors values in the specified column based on content.

Implementation

static Table withStatusColumn(
  List<String> headers,
  List<List<Object?>> rows, {
  int statusColumn = -1, // -1 means last column
  Map<String, Color>? statusColors,
}) {
  final colors =
      statusColors ??
      {
        'active': Colors.success,
        'done': Colors.success,
        'ok': Colors.success,
        'success': Colors.success,
        'inactive': Colors.warning,
        'pending': Colors.warning,
        'waiting': Colors.warning,
        'error': Colors.error,
        'failed': Colors.error,
        'failure': Colors.error,
      };

  return Table()
    ..headers(headers)
    ..rows(rows)
    ..border(style_border.Border.rounded)
    ..styleFunc((row, col, data) {
      final targetCol = statusColumn < 0
          ? headers.length + statusColumn
          : statusColumn;

      if (row == Table.headerRow) {
        return Style().bold();
      }

      if (col == targetCol) {
        final lowerData = data.toLowerCase();
        for (final entry in colors.entries) {
          if (lowerData.contains(entry.key)) {
            return Style().foreground(entry.value);
          }
        }
      }

      return null;
    });
}