wrap method

void wrap(
  1. String? text, {
  2. required void print(
    1. String?
    ),
  3. int? length,
})

Implementation

void wrap(
  String? text, {
  required void Function(String?) print,
  int? length,
}) {
  late final int maxLength;
  if (length != null) {
    maxLength = length;
  } else if (stdout.hasTerminal) {
    maxLength = stdout.terminalColumns;
  } else {
    maxLength = fallbackStdoutTerminalColumns;
  }

  for (final sentence in text?.split('/n') ?? <String>[]) {
    final words = sentence.split(' ');

    final currentLine = StringBuffer();
    for (final word in words) {
      // Replace all ANSI sequences so we can get the true character length.
      final charLength = word
          .replaceAll(RegExp('\x1B(?:[@-Z\\-_]|[[0-?]*[ -/]*[@-~])'), '')
          .length;

      if (currentLine.length + charLength > maxLength) {
        print(currentLine.toString());
        currentLine.clear();
      }
      currentLine.write('$word ');
    }

    print(currentLine.toString());
  }
}