wrap method

List<NatrixText> wrap(
  1. int maxLength, [
  2. List<NatrixChar>? breakpointCharacter
])

Wraps the ansi text literal into multiple lines to for-fill the requirements of maxLength. With breakpointCharacters, it could be specified where the line breaks should preferably be set.

Implementation

List<NatrixText> wrap(
  final int maxLength, [
  List<NatrixChar>? breakpointCharacter,
]) {
  if (ansi.length < maxLength || maxLength < 1) {
    return [this];
  }
  breakpointCharacter ??= [NatrixChar(' ')];
  final List<NatrixText> texts = [];

  String buffer = ansi;
  int breakpoint = 0;
  int count = 0;
  while (count < buffer.length) {
    if (buffer[count] == " ") {
      breakpoint = count + 1;
    }
    if (count < maxLength - 1) {
      count++;
      continue;
    }
    texts.add(NatrixText(buffer.cut(0, breakpoint)));
    buffer = buffer.cut(breakpoint);
    count = 0;
  }
  if (buffer.isNotEmpty) {
    texts.add(NatrixText(buffer));
  }
  return texts;
}