use crate::renderer::cell::Cell;
#[derive(Debug, Clone)]
pub(super) struct Grid {
rows: Vec<Vec<Cell>>,
columns: usize,
}
impl Grid {
pub(super) fn new(columns: u16, rows: u16, blank: &Cell) -> Self {
let row = vec![blank.clone(); usize::from(columns)];
Self {
rows: vec![row; usize::from(rows)],
columns: usize::from(columns),
}
}
pub(super) fn cell(&self, x: u16, y: u16) -> &Cell {
&self.rows[usize::from(y)][usize::from(x)]
}
pub(super) fn cell_mut(&mut self, x: u16, y: u16) -> &mut Cell {
&mut self.rows[usize::from(y)][usize::from(x)]
}
pub(super) fn row_text(&self, y: u16) -> String {
text_of(&self.rows[usize::from(y)])
}
pub(super) fn resize(&mut self, columns: u16, rows: u16) {
let width = usize::from(columns);
for row in &mut self.rows {
row.resize(width, Cell::BLANK);
}
self.rows
.resize(usize::from(rows), vec![Cell::BLANK; width]);
self.columns = width;
for y in 0..rows {
self.normalise(y, &Cell::BLANK);
}
}
pub(super) fn fill(&mut self, y: u16, from: u16, to: u16, blank: &Cell) {
let to = usize::from(to).min(self.columns);
let from = usize::from(from).min(to);
self.rows[usize::from(y)][from..to].fill(blank.clone());
self.normalise(y, blank);
}
pub(super) fn fill_rows(&mut self, from: u16, to: u16, blank: &Cell) {
for y in from..to {
self.fill(y, 0, u16::MAX, blank);
}
}
pub(super) fn insert_cells(&mut self, x: u16, y: u16, n: u16, blank: &Cell) {
let x = usize::from(x);
let n = usize::from(n).min(self.columns - x);
let row = &mut self.rows[usize::from(y)];
for _ in 0..n {
row.insert(x, blank.clone());
}
row.truncate(self.columns);
self.normalise(y, blank);
}
pub(super) fn delete_cells(&mut self, x: u16, y: u16, n: u16, blank: &Cell) {
let x = usize::from(x);
let n = usize::from(n).min(self.columns - x);
let row = &mut self.rows[usize::from(y)];
drop(row.drain(x..x + n));
row.resize(self.columns, blank.clone());
self.normalise(y, blank);
}
pub(super) fn scroll_up(&mut self, top: u16, bottom: u16, n: u16, blank: &Cell) -> Vec<String> {
let (top, end) = (usize::from(top), usize::from(bottom) + 1);
let n = usize::from(n).min(end - top);
let lost: Vec<String> = self.rows.drain(top..top + n).map(|r| text_of(&r)).collect();
for _ in 0..n {
self.rows.insert(end - n, vec![blank.clone(); self.columns]);
}
lost
}
pub(super) fn scroll_down(&mut self, top: u16, bottom: u16, n: u16, blank: &Cell) {
let (top, end) = (usize::from(top), usize::from(bottom) + 1);
let n = usize::from(n).min(end - top);
drop(self.rows.drain(end - n..end));
for _ in 0..n {
self.rows.insert(top, vec![blank.clone(); self.columns]);
}
}
pub(super) fn normalise(&mut self, y: u16, blank: &Cell) {
let row = &mut self.rows[usize::from(y)];
let mut x = 0;
while x < row.len() {
let width = usize::from(row[x].width).max(1);
if row[x].is_continuation() || !covered(row, x, width) {
row[x] = blank.clone();
x += 1;
} else {
x += width;
}
}
}
}
fn covered(row: &[Cell], x: usize, width: usize) -> bool {
x + width <= row.len() && row[x + 1..x + width].iter().all(Cell::is_continuation)
}
fn text_of(row: &[Cell]) -> String {
let mut out = String::new();
for cell in row {
cell.push_text(&mut out);
}
out.truncate(out.trim_end_matches(' ').len());
out
}