use super::cell::{Cell, Link, Pen};
use std::fmt::Write as _;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Addressing {
Absolute,
Relative,
}
#[derive(Debug, Clone)]
pub(crate) struct Writer {
pub(crate) buf: String,
pub(crate) x: Option<usize>,
pub(crate) y: usize,
pub(crate) rows: usize,
pub(crate) width: usize,
pub(crate) addressing: Addressing,
pen: Pen,
link: Link,
}
impl Writer {
pub(crate) fn new(width: usize, rows: usize, addressing: Addressing) -> Self {
Self {
buf: String::new(),
x: Some(0),
y: 0,
rows: rows.max(1),
width,
addressing,
pen: Pen::DEFAULT,
link: None,
}
}
pub(crate) fn raw(&mut self, s: &str) {
self.buf.push_str(s);
}
pub(crate) fn move_to(&mut self, x: usize, y: usize) {
if self.x == Some(x) && self.y == y {
return;
}
if self.addressing == Addressing::Relative && y >= self.rows {
self.grow_to(y);
}
let relative = self.relative_move(x, y);
let text = match self.addressing {
Addressing::Absolute => shortest(relative, cup(x, y)),
Addressing::Relative => relative,
};
self.buf.push_str(&text);
self.x = Some(x);
self.y = y;
}
fn grow_to(&mut self, y: usize) {
let last = self.rows.max(1) - 1;
self.buf.push_str(&vertical(self.y, last));
for _ in last..y {
self.buf.push_str("\r\n");
}
self.x = Some(0);
self.y = y;
self.rows = y + 1;
}
fn relative_move(&self, x: usize, y: usize) -> String {
let mut best = vertical(self.y, y) + &horizontal(self.x, x);
if y > self.y && y < self.rows {
let feeds = "\r\n".repeat(y - self.y) + &horizontal(Some(0), x);
best = shortest(best, feeds);
}
best
}
pub(crate) fn put(&mut self, cell: &Cell) {
if cell.is_continuation() {
return;
}
self.style(&cell.pen, &cell.link);
cell.push_text(&mut self.buf);
self.advance(usize::from(cell.width));
}
pub(crate) fn advance(&mut self, n: usize) {
let width = self.width;
self.x = self.x.map(|x| x + n).filter(|x| *x < width);
}
pub(crate) fn style(&mut self, pen: &Pen, link: &Link) {
if self.link != *link {
let target = link.as_deref().unwrap_or(";");
let _ = write!(self.buf, "\x1b]8;{target}\x07");
self.link.clone_from(link);
}
let sgr = self.pen.transition(pen);
self.buf.push_str(&sgr);
self.pen = *pen;
}
pub(crate) fn plain(&mut self) {
self.style(&Pen::DEFAULT, &None);
}
pub(crate) fn erase_line_right(&mut self) {
self.plain();
self.buf.push_str("\x1b[K");
}
pub(crate) fn erase_below(&mut self) {
self.plain();
self.buf.push_str("\x1b[J");
}
}
fn shortest(a: String, b: String) -> String {
if b.len() < a.len() { b } else { a }
}
fn counted(n: usize, final_byte: char) -> String {
if n == 1 {
format!("\x1b[{final_byte}")
} else {
format!("\x1b[{n}{final_byte}")
}
}
fn vertical(from: usize, to: usize) -> String {
match to.cmp(&from) {
std::cmp::Ordering::Less => counted(from - to, 'A'),
std::cmp::Ordering::Greater => counted(to - from, 'B'),
std::cmp::Ordering::Equal => String::new(),
}
}
fn horizontal(from: Option<usize>, to: usize) -> String {
let home = if to == 0 {
"\r".to_string()
} else {
format!("\r{}", counted(to, 'C'))
};
let column = format!("\x1b[{}G", to + 1);
let step = match from {
Some(f) if f == to => return String::new(),
Some(f) if f < to => counted(to - f, 'C'),
Some(f) => counted(f - to, 'D'),
None => column.clone(),
};
shortest(shortest(step, column), home)
}
fn cup(x: usize, y: usize) -> String {
match (x, y) {
(0, 0) => "\x1b[H".to_string(),
(0, _) => format!("\x1b[{}H", y + 1),
_ => format!("\x1b[{};{}H", y + 1, x + 1),
}
}
#[cfg(test)]
mod tests;