chunks function

List<String> chunks(
  1. String str,
  2. int maxLen, {
  3. bool stripColors = false,
  4. String wrapLine(
    1. String
    ) = noop,
})

Split string into chunks of maxLen characters.

Implementation

// @internal
List<String> chunks(
  String str,
  int maxLen, {
  bool stripColors = false,
  String Function(String) wrapLine = noop,
}) {
  final words = str.split(' ');
  final chunks = <String>[];
  var chunk = '';
  for (final word in words) {
    // if (chunk.contains('\n')) {
    //   final lines = chunk.split('\n');
    //   for (var i = 0; i < lines.length - 1; i++) {
    //     chunks.add(wrapLine(lines[i]));
    //   }
    //   chunk = '';
    // }
    final chunkLength = stripColors ? stripColor(chunk).length : chunk.length;
    final wordLength = stripColors ? stripColor(word).length : word.length;
    if (chunkLength + wordLength > maxLen) {
      chunks.add(wrapLine(chunk));
      chunk = '';
    }
    chunk += '$word ';
  }
  chunks.add(wrapLine(chunk));
  return chunks;
}