foldText function

String foldText(
  1. String text, [
  2. FoldOptions options = const FoldOptions()
])

Hard-wraps text to options.width, preserving each line's quote prefix.

Why: email replies must keep "> " markers on every wrapped continuation so quote depth survives folding. Words longer than the available width are emitted on their own line rather than split, which avoids an infinite loop when a single token already exceeds the width. Blank lines pass through as paragraph separators.

Example:

foldText('> hello world', const FoldOptions(width: 9));
// '> hello\n> world'

Audited: 2026-06-12 11:26 EDT

Implementation

String foldText(String text, [FoldOptions options = const FoldOptions()]) {
  // Fold each source line independently so a line's own quote depth governs
  // the prefix repeated on its continuations.
  final List<String> out = <String>[
    for (final String line in text.split('\n')) ..._foldLine(line, options.width),
  ];
  return out.join('\n');
}