wrapText method

String wrapText([
  1. int wrapLength = 76
])

Take an input string and wrap it across multiple lines.

Implementation

String wrapText([int wrapLength = 76]) {
  if (isEmpty) {
    return '';
  }

  final words = split(' ');
  final textLine = StringBuffer(words.first);
  final outputText = StringBuffer();

  for (final word in words.skip(1)) {
    if ((textLine.length + word.length) > wrapLength) {
      textLine.write('\n');
      outputText.write(textLine);
      textLine
        ..clear()
        ..write(word);
    } else {
      textLine.write(' $word');
    }
  }

  outputText.write(textLine);
  return outputText.toString().trimRight();
}