use ratatui::style::Style;
use ratatui::text::{Line, Span};
use unicode_width::UnicodeWidthChar;
#[derive(Debug)]
pub(crate) struct Row {
pub(crate) line: Line<'static>,
pub(crate) cells: Vec<Option<usize>>,
}
#[derive(Clone, Copy)]
struct Cell {
ch: char,
width: usize,
offset: Option<usize>,
style: Style,
}
fn cells_of(line: &Line<'_>, offsets: &[Option<usize>]) -> Vec<Cell> {
let mut offsets = offsets.iter();
line.spans
.iter()
.flat_map(|span| span.content.chars().map(move |ch| (ch, span.style)))
.map(|(ch, style)| Cell {
ch,
width: ch.width().unwrap_or(0),
offset: offsets.next().copied().flatten(),
style,
})
.collect()
}
fn finish_row(mut cells: Vec<Cell>, line_style: Style) -> Row {
while cells.last().is_some_and(|c| c.ch.is_whitespace()) {
cells.pop();
}
let mut spans: Vec<Span<'static>> = Vec::new();
let mut columns: Vec<Option<usize>> = Vec::new();
for cell in &cells {
match spans.last_mut() {
Some(last) if last.style == cell.style => last.content.to_mut().push(cell.ch),
_ => spans.push(Span::styled(cell.ch.to_string(), cell.style)),
}
columns.extend(std::iter::repeat_n(cell.offset, cell.width));
}
Row { line: Line::from(spans).style(line_style), cells: columns }
}
pub(crate) fn wrap_line(line: &Line<'_>, offsets: &[Option<usize>], width: usize) -> Vec<Row> {
let width = width.max(1);
let cells = cells_of(line, offsets);
let mut rows: Vec<Row> = Vec::new();
let mut current: Vec<Cell> = Vec::new();
let mut current_width = 0usize;
let mut rest = cells.as_slice();
while let Some(first) = rest.first() {
let is_space = first.ch.is_whitespace();
let len = rest.iter().take_while(|c| c.ch.is_whitespace() == is_space).count();
let (token, tail) = rest.split_at(len);
rest = tail;
let token_width: usize = token.iter().map(|c| c.width).sum();
if is_space && current.is_empty() && !rows.is_empty() {
continue;
}
if current_width + token_width <= width {
current.extend_from_slice(token);
current_width += token_width;
continue;
}
if !current.is_empty() {
rows.push(finish_row(std::mem::take(&mut current), line.style));
current_width = 0;
if is_space {
continue;
}
}
if token_width <= width {
current.extend_from_slice(token);
current_width = token_width;
} else {
for cell in token {
if current_width + cell.width > width && !current.is_empty() {
rows.push(finish_row(std::mem::take(&mut current), line.style));
current_width = 0;
}
current.push(*cell);
current_width += cell.width;
}
}
}
if !current.is_empty() || rows.is_empty() {
rows.push(finish_row(current, line.style));
}
rows
}
pub(crate) fn clip_line(line: &Line<'_>, offsets: &[Option<usize>], width: usize) -> Row {
let mut used = 0usize;
let kept = cells_of(line, offsets)
.into_iter()
.take_while(|cell| {
let fits = used + cell.width <= width;
if fits {
used += cell.width;
}
fits
})
.collect();
finish_row(kept, line.style)
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::indexing_slicing, reason = "tests assert by panicking")]
mod tests {
use super::*;
use ratatui::style::{Modifier, Stylize};
fn plain(rows: &[Row]) -> Vec<String> {
rows.iter().map(|r| r.line.to_string()).collect()
}
fn identity(n: usize) -> Vec<Option<usize>> {
(0..n).map(Some).collect()
}
#[test]
fn wraps_on_word_boundaries_and_keeps_offsets() {
let line = Line::from("the quick brown fox jumps");
let rows = wrap_line(&line, &identity(25), 10);
assert_eq!(plain(&rows), ["the quick", "brown fox", "jumps"]);
assert_eq!(rows.first().map(|r| r.cells.clone()), Some(identity(9)));
assert_eq!(rows.get(1).and_then(|r| r.cells.first().copied()), Some(Some(10)));
assert_eq!(rows.get(2).and_then(|r| r.cells.first().copied()), Some(Some(20)));
}
#[test]
fn styles_survive_wrapping_at_span_and_line_level() {
let line = Line::from(vec!["plain ".into(), "bold text here".bold()]).style(Style::new().italic());
let rows = wrap_line(&line, &identity(20), 11);
assert_eq!(plain(&rows), ["plain bold", "text here"]);
let second = rows.get(1).expect("two rows");
assert!(second.line.style.add_modifier.contains(Modifier::ITALIC));
assert!(second.line.spans.first().is_some_and(|s| s.style.add_modifier.contains(Modifier::BOLD)));
}
#[test]
fn hard_splits_long_tokens_and_wide_chars_take_two_cells() {
assert_eq!(
plain(&wrap_line(&Line::from("abcdefghijkl"), &identity(12), 5)),
["abcde", "fghij", "kl"]
);
let row = clip_line(&Line::from("日本"), &identity(2), 10);
assert_eq!(row.cells, [Some(0), Some(0), Some(1), Some(1)]);
}
#[test]
fn empty_line_is_one_row() {
assert_eq!(wrap_line(&Line::from(""), &[], 20).len(), 1);
}
}