use ratatui::text::{Line, Span};
use super::{
pad_spans_line, place_on_row, slice_spans_by_bytes, soft_wrap_visible_ranges,
spans_display_width, wrap_line_at_whitespace_ranges, RowLink, TextLink,
};
pub(super) fn push_wrapped_prefixed(
lines: &mut Vec<Line<'static>>,
prefix: Vec<Span<'static>>,
wrappable: Vec<Span<'static>>,
width: usize,
continuation_prefix: impl Fn(usize) -> Vec<Span<'static>>,
) {
let mut placed = Vec::new();
let links = &[];
push_wrapped_prefixed_linked(
lines,
links,
&mut placed,
prefix,
wrappable,
width,
continuation_prefix,
);
}
pub(super) fn push_wrapped_prefixed_linked(
lines: &mut Vec<Line<'static>>,
links: &[TextLink],
placed: &mut Vec<RowLink>,
prefix: Vec<Span<'static>>,
wrappable: Vec<Span<'static>>,
width: usize,
continuation_prefix: impl Fn(usize) -> Vec<Span<'static>>,
) {
let first_row = lines.len();
let hang = spans_display_width(&prefix);
let text: String = wrappable.iter().map(|span| span.content.as_ref()).collect();
if hang >= width {
place_on_row(links, &text, 0..text.len(), 0, hang, placed);
let mut spans = prefix;
spans.extend(wrappable);
lines.push(pad_spans_line(spans, width));
return;
}
let content_width = (width - hang).max(1);
if text.is_empty() {
lines.push(pad_spans_line(prefix, width));
return;
}
let mut row_index = 0usize;
for logical_line in text.lines() {
let line_start = subslice_start(&text, logical_line);
for range in soft_wrap_visible_ranges(
logical_line,
wrap_line_at_whitespace_ranges(logical_line, content_width),
) {
let chunk_spans =
slice_spans_by_bytes(&wrappable, line_start + range.start, line_start + range.end);
let mut row = if row_index == 0 {
prefix.clone()
} else {
continuation_prefix(hang)
};
let row_bytes = line_start + range.start..line_start + range.end;
let row_column = spans_display_width(&row);
let row_number = lines.len() - first_row;
place_on_row(links, &text, row_bytes, row_number, row_column, placed);
row.extend(chunk_spans);
lines.push(pad_spans_line(row, width));
row_index += 1;
}
}
}
fn subslice_start(parent: &str, child: &str) -> usize {
let start = child.as_ptr() as usize - parent.as_ptr() as usize;
debug_assert!(parent.get(start..start + child.len()) == Some(child));
start
}