truncateMiddle function

String truncateMiddle(
  1. String text,
  2. int maxChars
)

Shortens text to at most maxChars, keeping the start and the end.

Long command output usually has the useful parts (the command echo and the final error or summary) at the edges.

Implementation

String truncateMiddle(String text, int maxChars) {
  if (text.length <= maxChars) {
    return text;
  }
  final half = maxChars ~/ 2;
  final omitted = text.length - half * 2;
  return '${text.substring(0, half)}\n\n... [$omitted characters truncated] ...\n\n'
      '${text.substring(text.length - half)}';
}