use std::borrow::Cow;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use crate::document::{Line, Span};
use crate::sanitize::printed;
const TAB_STOP: usize = 4;
const WIDEST_GRAPHEME: usize = 2;
pub fn expand_tabs(text: &str, column: usize) -> Cow<'_, str> {
if !text.contains('\t') {
return Cow::Borrowed(text);
}
let mut expanded = String::with_capacity(text.len() + TAB_STOP);
let mut column = column;
for (index, part) in text.split('\t').enumerate() {
if index > 0 {
let spaces = tab_width(column);
expanded.extend(std::iter::repeat_n(' ', spaces));
column += spaces;
}
expanded.push_str(part);
column = match part.rfind('\n') {
Some(newline) => display_width(&part[newline + 1..]),
None => column + display_width(part),
};
}
Cow::Owned(expanded)
}
pub fn rewritten<'a>(text: &'a str, starts: &[char], step: impl Fn(&'a str) -> (Cow<'a, str>, usize)) -> Cow<'a, str> {
if !text.contains(starts) {
return Cow::Borrowed(text);
}
let mut written = String::with_capacity(text.len());
let mut rest = text;
while let Some(at) = rest.find(starts) {
written.push_str(&rest[..at]);
let (replacement, length) = step(&rest[at..]);
written.push_str(&replacement);
rest = &rest[at + length..];
}
written.push_str(rest);
Cow::Owned(written)
}
pub fn display_width(text: &str) -> usize {
if is_printable_ascii(text) {
return text.len();
}
if text.contains('\t') {
return display_width(&expand_tabs(text, 0));
}
printed_graphemes(text).map(|(_, width)| width).sum()
}
fn is_printable_ascii(text: &str) -> bool {
text.bytes().all(|byte| byte == b' ' || byte.is_ascii_graphic())
}
fn printed_graphemes(text: &str) -> Box<dyn Iterator<Item = (usize, usize)> + '_> {
if is_printable_ascii(text) {
return Box::new((0..text.len()).map(|index| (index, 1)));
}
let cells = |grapheme: &str| grapheme.width().min(WIDEST_GRAPHEME);
if text.chars().all(|character| printed(character) == Some(character)) {
return Box::new(text.grapheme_indices(true).map(move |(index, grapheme)| (index, cells(grapheme))));
}
let kept: Vec<(usize, char)> = text.char_indices().filter_map(|(index, character)| Some((index, printed(character)?))).collect();
let shown: String = kept.iter().map(|&(_, character)| character).collect();
let graphemes: Vec<(usize, usize)> = shown
.graphemes(true)
.scan(0, |first_char, grapheme| {
let (origin, _) = kept[*first_char];
*first_char += grapheme.chars().count();
Some((origin, cells(grapheme)))
})
.collect();
Box::new(graphemes.into_iter())
}
#[cfg(feature = "cli")]
pub fn grapheme_bounds(text: &str) -> Vec<bool> {
let inside = |grapheme: &str| std::iter::once(true).chain(std::iter::repeat_n(false, grapheme.chars().count() - 1));
text.graphemes(true).flat_map(inside).chain(std::iter::once(true)).collect()
}
pub fn cut(text: &str, width: usize) -> (&str, &str) {
let mut used = 0;
let end = printed_graphemes(text).find_map(|(index, width_of)| {
used += width_of;
(used > width).then_some(index)
});
text.split_at(end.unwrap_or(text.len()))
}
const MIN_SPLIT_CELLS: usize = 4;
pub fn wrap(spans: &[Span], width: usize, indent: &[Span]) -> Vec<Line> {
let tokens = spans.iter().flat_map(|span| span_tokens(span, &span.text, row_tokens));
place(tokens, width, indent)
}
pub fn wrap_literal(spans: &[Span], width: usize, indent: &[Span]) -> Vec<Line> {
let texts = expanded_texts(spans);
let tokens = spans.iter().zip(&texts).flat_map(|(span, text)| span_tokens(span, text, literal_row_tokens));
place(tokens, width, indent)
}
fn place<'a>(tokens: impl Iterator<Item = Token<'a>>, width: usize, indent: &'a [Span]) -> Vec<Line> {
let tokens = tokens.fold(Vec::new(), join_words);
tokens.into_iter().fold(LineBuilder::new(width, indent), LineBuilder::place).finish()
}
fn expanded_texts(spans: &[Span]) -> Vec<Cow<'_, str>> {
let texts = spans.iter().scan(0, |column, span| {
let text = expand_tabs(&span.text, *column);
*column += display_width(&text);
Some(text)
});
texts.collect()
}
pub fn hard_wrap(spans: &[Span], width: usize, indent: &[Span]) -> Vec<Line> {
let indent_width = spans_width(indent);
let width = width.max(indent_width + WIDEST_GRAPHEME);
let fits = spans_width(spans) <= width && !spans.iter().any(|span| span.text.contains('\t'));
if fits {
return vec![Line::joined(pieces_of(spans).map(|piece| (piece.text, piece.span)))];
}
let texts = expanded_texts(spans);
let pieces = spans.iter().zip(&texts).map(|(span, text)| Piece { span, text });
let rows = cut_rows(pieces, width, width - indent_width);
let lines = rows.into_iter().enumerate().map(|(index, row)| if index == 0 { row } else { pieces_of(indent).chain(row).collect() });
lines.map(line_of).collect()
}
pub(crate) fn fit(spans: &[Span], width: usize) -> Line {
let width = width.max(WIDEST_GRAPHEME);
let texts = expanded_texts(spans);
let pieces: Vec<Piece> = spans.iter().zip(&texts).map(|(span, text)| Piece { span, text }).collect();
if pieces_width(&pieces) <= width {
return line_of(pieces);
}
let mut rows = cut_rows(pieces, width - display_width(ELLIPSIS), width).into_iter();
let kept = rows.next().unwrap_or_default();
let ellipsis = rows.next().and_then(|row| row.first().map(|piece| Span::new(ELLIPSIS, piece.span.style))).unwrap_or_default();
Line::joined(kept.into_iter().map(|piece| (piece.text, piece.span)).chain([(ELLIPSIS, &ellipsis)]))
}
const ELLIPSIS: &str = "…";
#[derive(Clone, Copy, Debug, PartialEq)]
struct Piece<'a> {
span: &'a Span,
text: &'a str,
}
impl Piece<'_> {
fn width(self) -> usize {
display_width(self.text)
}
fn part(self, start: usize, end: usize) -> Self {
Self { text: &self.text[start..end], ..self }
}
}
fn pieces_of(spans: &[Span]) -> impl Iterator<Item = Piece<'_>> {
spans.iter().map(|span| Piece { span, text: &span.text })
}
fn pieces_width(pieces: &[Piece]) -> usize {
pieces.iter().map(|piece| piece.width()).sum()
}
fn line_of(pieces: Vec<Piece>) -> Line {
Line::joined(pieces.into_iter().map(|piece| (piece.text, piece.span)))
}
fn cut_rows<'a>(pieces: impl IntoIterator<Item = Piece<'a>>, first: usize, rest: usize) -> Vec<Vec<Piece<'a>>> {
let mut rows = Vec::new();
let mut row = Vec::new();
let mut free = first;
for piece in pieces {
let mut start = 0;
for (index, width) in printed_graphemes(piece.text) {
if width > free {
row.extend((index > start).then(|| piece.part(start, index)));
rows.push(std::mem::take(&mut row));
(start, free) = (index, rest);
}
free -= width;
}
row.extend((start < piece.text.len()).then(|| piece.part(start, piece.text.len())));
}
rows.push(row);
rows
}
#[derive(Debug, PartialEq)]
enum Token<'a> {
Word(Vec<Piece<'a>>),
Space(Piece<'a>),
Break,
}
type RowTokens = for<'a> fn(&'a Span, &'a str) -> Vec<Token<'a>>;
fn span_tokens<'a>(span: &'a Span, text: &'a str, row_tokens: RowTokens) -> impl Iterator<Item = Token<'a>> {
let rows = text.split('\n').enumerate();
rows.flat_map(move |(index, row)| (index > 0).then_some(Token::Break).into_iter().chain(row_tokens(span, row)))
}
fn row_tokens<'a>(span: &'a Span, row: &'a str) -> Vec<Token<'a>> {
let breaks: &[char] = if span.style.bg.is_some() { &[] } else { &[' ', '\t'] };
let tokens = row.split(breaks).enumerate().flat_map(move |(index, word)| {
let space = (index > 0).then_some(Token::Space(Piece { span, text: SPACE }));
space.into_iter().chain((!word.is_empty()).then(|| Token::Word(word_pieces(span, word))))
});
tokens.collect()
}
fn literal_row_tokens<'a>(span: &'a Span, row: &'a str) -> Vec<Token<'a>> {
let runs = space_runs(row).into_iter().enumerate();
let token = |(index, text): (usize, &'a str)| {
let piece = Piece { span, text };
if index > 0 && text.starts_with(' ') { Token::Space(piece) } else { Token::Word(vec![piece]) }
};
runs.map(token).collect()
}
fn space_runs(row: &str) -> Vec<&str> {
let mut runs = Vec::new();
let mut rest = row;
while let Some(first) = rest.chars().next() {
let is_space = first == ' ';
let end = rest.find(|character: char| (character == ' ') != is_space).unwrap_or(rest.len());
let (run, tail) = rest.split_at(end);
runs.push(run);
rest = tail;
}
runs
}
fn word_pieces<'a>(span: &'a Span, word: &'a str) -> Vec<Piece<'a>> {
let parts = word
.split('\t')
.enumerate()
.flat_map(|(index, text)| (index > 0).then_some(TAB).into_iter().chain((!text.is_empty()).then_some(text)));
parts.map(|text| Piece { span, text }).collect()
}
const TAB: &str = "\t";
const TAB_SPACES: &str = " ";
const _: () = assert!(TAB_SPACES.len() == TAB_STOP);
fn tab_width(column: usize) -> usize {
TAB_STOP - column % TAB_STOP
}
fn width_at(word: &[Piece], column: usize) -> usize {
word.iter().fold(0, |width, piece| width + if piece.text == TAB { tab_width(column + width) } else { piece.width() })
}
fn at_column(word: Vec<Piece<'_>>, column: usize) -> Vec<Piece<'_>> {
if !word.iter().any(|piece| piece.text == TAB) {
return word;
}
let placed = word.into_iter().scan(column, |column, piece| {
let text = if piece.text == TAB { &TAB_SPACES[..tab_width(*column)] } else { piece.text };
*column += display_width(text);
Some(Piece { text, ..piece })
});
placed.collect()
}
fn join_words<'a>(mut tokens: Vec<Token<'a>>, token: Token<'a>) -> Vec<Token<'a>> {
match (tokens.last_mut(), token) {
(Some(Token::Word(word)), Token::Word(pieces)) => word.extend(pieces),
(_, token) => tokens.push(token),
}
tokens
}
pub(crate) fn spans_width(spans: &[Span]) -> usize {
spans.iter().map(|span| display_width(&span.text)).sum()
}
pub(crate) fn trimmed_width(spans: &[Span]) -> usize {
display_width(joined(spans).trim_end())
}
pub(crate) fn first_word_width(spans: &[Span]) -> Option<usize> {
joined(spans).split_whitespace().next().map(display_width)
}
pub(crate) fn joined(spans: &[Span]) -> String {
spans.iter().map(|span| span.text.as_str()).collect()
}
const SPACE: &str = " ";
struct LineBuilder<'a> {
width: usize,
indent: &'a [Span],
indent_width: usize,
lines: Vec<Line>,
current: Vec<Piece<'a>>,
used: usize,
start: usize,
space: Option<Piece<'a>>,
}
impl<'a> LineBuilder<'a> {
fn new(width: usize, indent: &'a [Span]) -> Self {
let indent_width = spans_width(indent);
let width = width.max(indent_width + WIDEST_GRAPHEME);
Self { width, indent, indent_width, lines: Vec::new(), current: Vec::new(), used: 0, start: 0, space: None }
}
fn place(self, token: Token<'a>) -> Self {
match token {
Token::Break => self.close(),
Token::Space(space) => self.hold_space(space),
Token::Word(word) => self.place_word(word),
}
}
fn hold_space(self, space: Piece<'a>) -> Self {
if self.has_content() { Self { space: Some(space), ..self } } else { self }
}
fn place_word(self, word: Vec<Piece<'a>>) -> Self {
let column = self.column();
let width = width_at(&word, column);
if column + width <= self.width {
return self.push_spaced(word, width);
}
let width = width_at(&word, self.indent_width);
if self.has_content() && width <= self.room() { self.close().push_spaced(word, width) } else { self.split_word(word) }
}
fn push_spaced(self, word: Vec<Piece<'a>>, width: usize) -> Self {
let column = self.column();
let word = at_column(word, column);
let used = column + width;
let Self { mut current, space, .. } = self;
current.extend(space);
current.extend(word);
Self { current, used, space: None, ..self }
}
fn split_word(self, word: Vec<Piece<'a>>) -> Self {
let builder = if self.has_content() && self.free() < MIN_SPLIT_CELLS { self.close() } else { self };
let word = at_column(word, builder.column());
let mut rows = cut_rows(word, builder.free(), builder.room()).into_iter();
let first = rows.next().unwrap_or_default();
let push_row = |builder: Self, row: Vec<Piece<'a>>| {
let width = pieces_width(&row);
builder.push_spaced(row, width)
};
rows.fold(push_row(builder, first), |builder, row| push_row(builder.close(), row))
}
fn column(&self) -> usize {
self.used + self.space_width()
}
fn close(self) -> Self {
let Self { mut lines, current, .. } = self;
lines.push(line_of(current));
Self { lines, current: pieces_of(self.indent).collect(), used: self.indent_width, start: self.indent_width, space: None, ..self }
}
fn room(&self) -> usize {
self.width - self.indent_width
}
fn free(&self) -> usize {
self.width.saturating_sub(self.column())
}
fn space_width(&self) -> usize {
self.space.map_or(0, Piece::width)
}
fn has_content(&self) -> bool {
self.used > self.start
}
fn finish(self) -> Vec<Line> {
let Self { mut lines, current, .. } = self;
lines.push(line_of(current));
lines
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::document::{Rgb, Style};
fn plain_lines(lines: &[Line]) -> Vec<String> {
lines.iter().map(Line::plain).collect()
}
#[test]
fn breaks_at_spaces_and_drops_the_space_at_the_break() {
let lines = wrap(&[Span::plain("the quick brown fox jumps")], 10, &[]);
assert_eq!(plain_lines(&lines), ["the quick", "brown fox", "jumps"]);
}
#[test]
fn cuts_a_word_longer_than_the_line() {
let lines = wrap(&[Span::plain("a supercalifragilistic word")], 8, &[]);
assert_eq!(plain_lines(&lines), ["a superc", "alifragi", "listic", "word"]);
}
#[test]
fn a_long_word_near_the_end_of_a_line_starts_the_next() {
let lines = wrap(&[Span::plain("abcdef supercalifragilistic")], 8, &[]);
assert_eq!(plain_lines(&lines), ["abcdef", "supercal", "ifragili", "stic"]);
}
#[test]
fn hanging_indent_starts_every_line_after_the_first() {
let lines = wrap(&[Span::plain("one two three four")], 9, &[Span::plain(" ")]);
assert_eq!(plain_lines(&lines), ["one two", " three", " four"]);
}
#[test]
fn counts_wide_characters_as_two_cells() {
let lines = wrap(&[Span::plain("漢字漢字漢字")], 5, &[]);
assert_eq!(plain_lines(&lines), ["漢字", "漢字", "漢字"]);
}
const MERGING_ONCE_SANITIZED: [&str; 4] = ["❤\u{200b}\u{fe0f}", "1\u{200b}\u{fe0f}\u{20e3}", "❤\x1b\u{fe0f}", "❤\u{2063}\u{fe0f}"];
#[test]
fn a_text_is_as_wide_as_what_sanitize_prints() {
for raw in MERGING_ONCE_SANITIZED {
assert_eq!((display_width(raw), display_width(&crate::sanitize::text(raw))), (2, 2), "{raw:?}");
}
}
#[test]
fn cut_keeps_together_graphemes_that_merge_once_sanitized() {
for raw in MERGING_ONCE_SANITIZED {
let text = format!("{raw}a");
assert_eq!(cut(&text, 1), ("", text.as_str()), "{raw:?}");
assert_eq!(cut(&text, 2), (raw, "a"), "{raw:?}");
}
}
#[test]
fn a_removed_character_opening_a_text_stays_with_the_first_grapheme() {
assert_eq!(cut("\x1bab", 1), ("\x1ba", "b"));
}
#[test]
fn fit_keeps_spans_that_fit() {
let spans = [Span::plain("ab"), Span::new("cd", Style::default().bold())];
assert_eq!(fit(&spans, 4).spans, spans);
}
#[test]
fn fit_cuts_a_wider_line_and_ends_it_with_an_ellipsis_in_the_cut_style() {
let bold = Style::default().bold();
let line = fit(&[Span::plain("ab"), Span::new("cd漢", bold)], 4);
assert_eq!(line.spans, [Span::plain("ab"), Span::new("c…", bold)]);
}
#[test]
fn keeps_styles_across_the_break() {
let bold = Style::default().bold();
let lines = wrap(&[Span::plain("plain "), Span::new("bold words", bold)], 10, &[]);
assert_eq!(lines[1].spans, [Span::new("words", bold)]);
}
#[test]
fn a_word_spanning_two_styles_stays_whole() {
let lines = wrap(&[Span::plain("aaaa bb"), Span::new("cc", Style::default().bold())], 6, &[]);
assert_eq!(plain_lines(&lines), ["aaaa", "bbcc"]);
}
#[test]
fn newline_forces_a_break_and_never_reaches_a_span() {
let lines = wrap(&[Span::plain("one\ntwo")], 80, &[]);
assert_eq!(plain_lines(&lines), ["one", "two"]);
}
#[test]
fn spaces_on_a_background_hold_together() {
let code = Style::default().on(Rgb(0, 0, 0));
let lines = wrap(&[Span::plain("see "), Span::new(" a b ", code)], 8, &[]);
assert_eq!(plain_lines(&lines), ["see", " a b "]);
}
#[test]
fn empty_input_is_one_empty_line() {
assert_eq!(wrap(&[], 10, &[]), [Line::blank()]);
assert_eq!(hard_wrap(&[], 10, &[Span::plain("> ")]), [Line::blank()]);
}
#[test]
fn an_emoji_with_its_presentation_selector_is_two_cells() {
assert_eq!(display_width("❤️"), 2);
assert_eq!(display_width("👩💻"), 2);
}
#[test]
fn what_sanitize_removes_takes_no_cell_and_a_tab_reaches_the_next_stop() {
assert_eq!(display_width("a\x1b[31mb\u{9b}\u{202e}c\r\n"), 7);
assert_eq!(display_width("a\tb"), 5);
assert_eq!(display_width("abcd\tb"), 9);
}
#[test]
fn a_grapheme_is_as_wide_as_what_sanitize_prints_of_it() {
let scotland = "\u{1f3f4}\u{e0067}\u{e0062}\u{e0073}\u{e0063}\u{e0074}\u{e007f}";
assert_eq!(display_width("a\u{e0041}bc"), 3);
assert_eq!(display_width(scotland), 2);
assert_eq!(display_width("a\u{2028}b"), 3);
}
#[test]
fn literal_text_keeps_its_runs_of_spaces_and_breaks_at_one() {
let lines = wrap_literal(&[Span::plain(" <td>a b</td> <td>c</td>")], 18, &[]);
assert_eq!(plain_lines(&lines), [" <td>a b</td>", "<td>c</td>"]);
assert_eq!(
plain_lines(&wrap_literal(&[Span::plain("key:\tvalue averyveryverylongword")], 13, &[])),
["key: value", "averyveryvery", "longword"]
);
}
#[test]
fn tabs_stop_every_four_columns_from_the_start_of_the_line() {
assert_eq!(expand_tabs("ab\tc", 0), "ab c");
assert_eq!(expand_tabs("\tc", 2), " c");
assert_eq!(expand_tabs("a\nb\tc", 0), "a\nb c");
assert!(matches!(expand_tabs("no tab", 0), Cow::Borrowed(_)));
}
#[test]
fn each_start_is_rewritten_as_its_step_says_and_the_rest_kept() {
let step = |text: &str| if text.starts_with("<x>") { (Cow::Borrowed("y"), 3) } else { (Cow::Borrowed("<"), 1) };
assert_eq!(rewritten("a<x>b<c<x>", &['<'], step), "ayb<cy");
assert!(matches!(rewritten("none", &['<'], step), Cow::Borrowed(_)));
}
#[test]
fn hard_wrap_counts_tab_stops_across_spans() {
let lines = hard_wrap(&[Span::plain("ab"), Span::new("\tc", Style::default().bold())], 20, &[]);
assert_eq!(plain_lines(&lines), ["ab c"]);
}
#[test]
fn cut_keeps_whole_graphemes_that_fit() {
assert_eq!(cut("❤️❤️❤️", 5), ("❤️❤️", "❤️"));
assert_eq!(cut("e\u{301}xyz", 2), ("e\u{301}x", "yz"));
assert_eq!(cut("漢字", 1), ("", "漢字"));
assert_eq!(cut("abc", 10), ("abc", ""));
}
#[test]
#[cfg(feature = "cli")]
fn grapheme_bounds_mark_the_offsets_between_graphemes_and_the_end() {
assert_eq!(grapheme_bounds("e\u{301}x"), [true, false, true, true]);
assert_eq!(grapheme_bounds("👩\u{200d}👧"), [true, false, false, true]);
assert_eq!(grapheme_bounds(""), [true]);
}
#[test]
fn emoji_presentation_sequences_never_overflow_the_line() {
let lines = wrap(&[Span::plain("❤️".repeat(30))], 20, &[]);
assert_eq!(lines.iter().map(Line::width).collect::<Vec<_>>(), [20, 20, 20]);
}
#[test]
fn a_width_too_narrow_for_the_indent_and_a_wide_grapheme_widens_to_fit_them() {
assert_eq!(plain_lines(&wrap(&[Span::plain("漢字")], 1, &[])), ["漢", "字"]);
assert_eq!(plain_lines(&hard_wrap(&[Span::plain("漢字")], 1, &[])), ["漢", "字"]);
assert_eq!(plain_lines(&wrap(&[Span::plain("ab cd")], 1, &[Span::plain(" ")])), ["ab", " cd"]);
}
#[test]
fn a_tab_is_four_spaces_and_breaks_like_one() {
let code = Style::default().on(Rgb(0, 0, 0));
assert_eq!(plain_lines(&wrap(&[Span::plain("one\ttwo")], 6, &[])), ["one", "two"]);
assert_eq!(plain_lines(&wrap(&[Span::new("\tx", code)], 20, &[])), [" x"]);
assert_eq!(plain_lines(&hard_wrap(&[Span::plain("\tabc")], 6, &[])), [" ab", "c"]);
}
#[test]
fn a_tab_on_a_background_stops_at_the_column_of_the_line_it_lands_on() {
let code = Style::default().on(Rgb(0, 0, 0));
assert_eq!(plain_lines(&wrap(&[Span::plain("ab "), Span::new("\tx", code)], 20, &[])), ["ab x"]);
assert_eq!(plain_lines(&wrap(&[Span::plain("abcdef "), Span::new("\tx", code)], 8, &[Span::plain(" ")])), ["abcdef", " x"]);
}
#[test]
fn hard_wrap_fills_each_line_and_indents_the_rest() {
let bold = Style::default().bold();
let lines = hard_wrap(&[Span::plain("let a = "), Span::new("12345", bold)], 6, &[Span::plain("> ")]);
assert_eq!(plain_lines(&lines), ["let a ", "> = 12", "> 345"]);
assert_eq!(lines[1].spans, [Span::plain("> = "), Span::new("12", bold)]);
}
#[test]
fn trimmed_width_ends_at_the_last_word_across_spans() {
let spans = [Span::plain("界 a"), Span::plain("b "), Span::plain(" ")];
assert_eq!(trimmed_width(&spans), 5);
assert_eq!(trimmed_width(&[Span::plain(" ")]), 0);
}
#[test]
fn the_first_word_is_measured_whole_across_spans() {
let spans = [Span::plain(" 界"), Span::plain("ab c")];
assert_eq!(first_word_width(&spans), Some(4));
assert_eq!(first_word_width(&[Span::plain(" ")]), None);
}
#[test]
fn no_line_exceeds_the_width() {
let text = "Lorem ipsum dolor sit amet, consectetur \u{200b} adipiscing elit, sed doeiusmodtemporincididunt ut labore 漢字漢字 et\tdolore.";
for width in 3..40 {
let wrapped = wrap(&[Span::plain(text)], width, &[Span::plain(" ")]);
let cut = hard_wrap(&[Span::plain(text)], width, &[Span::plain(" ")]);
assert!(wrapped.iter().chain(&cut).all(|line| line.width() <= width), "width {width}");
}
}
}