use super::cell::{Cell, Link, Pen};
use crate::ansi::{Token, tokens};
use crate::unicode::WidthMethod;
use std::hash::{Hash, Hasher};
use std::rc::Rc;
const TAB: usize = 8;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Row {
pub(crate) cells: Vec<Cell>,
pub(crate) hash: u64,
}
impl Row {
pub(crate) fn blank(width: usize) -> Self {
Self::new(vec![Cell::BLANK; width])
}
pub(crate) fn new(cells: Vec<Cell>) -> Self {
let mut hasher = Fnv::default();
cells.hash(&mut hasher);
Self {
hash: hasher.finish(),
cells,
}
}
pub(crate) fn content_end(&self) -> usize {
self.cells
.iter()
.rposition(|c| !c.is_blank())
.map_or(0, |i| i + 1)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct Frame {
pub(crate) width: usize,
pub(crate) rows: Vec<Row>,
}
#[derive(Debug, Default)]
pub(crate) struct Pens {
pen: Pen,
link: Link,
}
impl Pens {
fn apply(&mut self, token: Token<'_>) {
match token {
Token::Csi {
params,
final_byte: b'm',
} if params
.bytes()
.all(|b| b.is_ascii_digit() || b == b';' || b == b':') =>
{
self.pen.apply_sgr(params);
}
Token::Osc(data) => {
if let Some(link) = data.strip_prefix("8;") {
self.set_link(link);
}
}
_ => {}
}
}
fn set_link(&mut self, link: &str) {
let url = link.split_once(';').map_or("", |(_, url)| url);
self.link = if url.is_empty() {
None
} else {
Some(Rc::from(link))
};
}
}
struct Line {
cells: Vec<Cell>,
limit: usize,
x: usize,
last: Option<usize>,
full: bool,
}
impl Line {
fn new(limit: usize) -> Self {
Self {
cells: Vec::new(),
limit,
x: 0,
last: None,
full: false,
}
}
fn push(&mut self, text: &str, width: usize, pens: &Pens) {
if self.full || self.x + width > self.limit {
self.full = true;
return;
}
let (x, end) = (self.x, self.x + width);
if self.cells.len() < end {
self.cells.resize(end, Cell::BLANK);
}
self.unsplit(x, end);
let w = u8::try_from(width).unwrap_or(u8::MAX);
self.cells[x] = Cell::new(text, w, pens.pen, pens.link.clone());
self.cover(x + 1, end, pens);
self.last = Some(x);
self.x = end;
}
fn cover(&mut self, from: usize, to: usize, pens: &Pens) {
for cell in &mut self.cells[from..to] {
*cell = Cell::continuation(pens.pen, pens.link.clone());
}
}
fn unsplit(&mut self, from: usize, to: usize) {
let mut i = from;
while i > 0 && self.cells[i].is_continuation() {
i -= 1;
self.cells[i] = Cell::BLANK;
}
let mut j = to;
while self.cells.get(j).is_some_and(Cell::is_continuation) {
self.cells[j] = Cell::BLANK;
j += 1;
}
}
fn mark(&mut self, text: &str) {
if let Some(cell) = self.last.and_then(|i| self.cells.get_mut(i)) {
cell.append(text);
}
}
fn tab(&mut self, pens: &Pens) {
let to = (self.x / TAB + 1) * TAB;
while self.x < to && !self.full {
self.push(" ", 1, pens);
}
}
fn read(&mut self, token: Token<'_>, pens: &mut Pens) {
match token {
Token::Text(text, 0) => self.mark(text),
Token::Text(text, width) => self.push(text, width, pens),
Token::Control(b'\t') => self.tab(pens),
Token::Control(b'\r') => {
self.x = 0;
self.full = false;
}
Token::Control(_) => {}
other => pens.apply(other),
}
}
}
pub(crate) fn line_cells(
text: &str,
limit: usize,
method: WidthMethod,
pens: &mut Pens,
) -> Vec<Cell> {
let mut line = Line::new(limit);
for token in tokens(text, method) {
line.read(token, pens);
}
line.cells
}
pub(crate) fn lines(content: &str) -> Vec<&str> {
if content.is_empty() {
return Vec::new();
}
content
.split('\n')
.map(|l| l.strip_suffix('\r').unwrap_or(l))
.collect()
}
pub(crate) fn layout(
content: &str,
width: usize,
rows: Option<usize>,
method: WidthMethod,
) -> Frame {
let mut pens = Pens::default();
let mut out: Vec<Row> = lines(content)
.into_iter()
.take(rows.unwrap_or(usize::MAX))
.map(|line| {
let mut cells = line_cells(line, width, method, &mut pens);
cells.resize(width, Cell::BLANK);
Row::new(cells)
})
.collect();
if let Some(rows) = rows {
out.resize_with(rows, || Row::blank(width));
}
Frame { width, rows: out }
}
struct Fnv(u64);
impl Default for Fnv {
fn default() -> Self {
Self(0xcbf2_9ce4_8422_2325)
}
}
impl Hasher for Fnv {
fn finish(&self) -> u64 {
self.0
}
fn write(&mut self, bytes: &[u8]) {
for b in bytes {
self.0 = (self.0 ^ u64::from(*b)).wrapping_mul(0x0100_0000_01b3);
}
}
}
#[cfg(test)]
mod tests;