columnWidth function

int columnWidth(
  1. Iterable<String> values, {
  2. int min = 0,
  3. int max = 999,
})

Computes optimal column width from content lengths, clamped to bounds.

Useful for auto-sizing table/list columns based on content.

Example:

columnWidth(['Name', 'Alice', 'Bob'], min: 4, max: 20); // 5
columnWidth(['VeryLongName'], min: 4, max: 8); // 8

Implementation

int columnWidth(Iterable<String> values, {int min = 0, int max = 999}) {
  final maxLen = maxOf(values.map((s) => s.length));
  return clampInt(maxLen, min, max);
}