wrapTraceText function

List<String> wrapTraceText(
  1. String text,
  2. int maxWidth
)

Implementation

List<String> wrapTraceText(String text, int maxWidth) {
  if (maxWidth <= 0) return [];
  if (text.length <= maxWidth) return [text];

  final words = text.split(' ');
  final lines = <String>[];
  var currentLine = '';

  for (final word in words) {
    if (word.isEmpty) continue;

    if (currentLine.isEmpty) {
      currentLine = word;
    } else if (currentLine.length + 1 + word.length <= maxWidth) {
      currentLine += ' $word';
    } else {
      lines.add(currentLine);
      currentLine = '  $word'; // Indent wrapped lines
    }
  }
  if (currentLine.isNotEmpty) {
    lines.add(currentLine);
  }
  return lines;
}