renderTable static method

String renderTable(
  1. List<Map<String, dynamic>> keys, {
  2. required String? currentKeyId,
})

Pure — table shape only, so it unit-tests in-process without a Dio.

Implementation

static String renderTable(
  List<Map<String, dynamic>> keys, {
  required String? currentKeyId,
}) {
  if (keys.isEmpty) {
    return 'No API keys yet. Run `appflight keys create --name <label>`.';
  }

  final header = ['LABEL', 'PREFIX', 'CREATED', 'LAST USED'];
  final rows = keys.map((key) {
    final isCurrent = key['id'] == currentKeyId;
    return [
      '${key['name']}${isCurrent ? ' (current)' : ''}',
      key['prefix'] as String,
      _formatDate(key['createdAt'] as String?),
      _formatDate(key['lastUsedAt'] as String?, fallback: 'never'),
    ];
  });
  final all = [header, ...rows];
  final widths = List.generate(
    4,
    (i) => all.map((r) => r[i].length).reduce((a, b) => a > b ? a : b),
  );
  String renderRow(List<String> row) =>
      List.generate(4, (i) => row[i].padRight(widths[i]))
          .join('  ')
          .trimRight();
  return all.map(renderRow).join('\n');
}