use super::{linebreak, styled::Span};
#[derive(Debug, Clone)]
pub struct PageRow {
pub text: String,
pub spans: Vec<Span>, pub char_offset: usize, }
#[derive(Debug, Clone)]
pub struct Page {
pub rows: Vec<PageRow>,
}
pub fn paginate(spans: &[Span], width: u16, height: u16) -> Vec<Page> {
let height = height as usize;
if spans.is_empty() {
return vec![Page { rows: vec![] }];
}
let mut text = String::new();
for s in spans {
text.push_str(&s.text);
}
let lines = linebreak::wrap(&text, width);
let mut rows: Vec<PageRow> = Vec::with_capacity(lines.len());
let mut cursor = 0usize;
for l in &lines {
if l.is_empty() {
rows.push(PageRow {
text: String::new(),
spans: vec![],
char_offset: cursor,
});
continue;
}
let off = text[cursor..]
.find(l.as_str())
.map(|b| cursor + b)
.unwrap_or(cursor);
let char_off = text[..off].chars().count();
rows.push(PageRow {
text: l.clone(),
spans: vec![],
char_offset: char_off,
});
cursor = off + l.len();
}
rows.chunks(height)
.map(|c| Page { rows: c.to_vec() })
.collect()
}