use compact_str::CompactString;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
use crate::icon::{Icon, Piece};
use crate::measure::Font;
use crate::paint::{Bounds, Paint, UNIT, units};
const BUTTON_HEIGHT: f32 = 0.8;
const LARGE_BUTTON_HEIGHT: f32 = BUTTON_HEIGHT;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Rgb(pub u8, pub u8, pub u8);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Style {
pub fg: Rgb,
pub bg: Rgb,
pub bold: bool,
pub italic: bool,
pub underline: bool,
pub painted: bool,
}
fn visible(text: &str) -> std::borrow::Cow<'_, str> {
let control = |c: char| c.is_control();
if !text.contains(control) {
return std::borrow::Cow::Borrowed(text);
}
let shown = text
.chars()
.map(|c| match c {
'\t' => ' ',
'\0'..='\u{1f}' => char::from_u32(0x2400 + c as u32).unwrap_or('\u{fffd}'),
'\u{7f}' => '\u{2421}',
c if c.is_control() => '\u{fffd}',
c => c,
})
.collect();
std::borrow::Cow::Owned(shown)
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Cell {
pub symbol: CompactString,
pub style: Style,
}
trait CopyCells {
fn copy_within_cells(&mut self, from: std::ops::Range<usize>, to: usize);
}
impl CopyCells for Vec<Cell> {
fn copy_within_cells(&mut self, from: std::ops::Range<usize>, to: usize) {
for (n, at) in from.enumerate() {
let cell = self[at].clone();
self[to + n] = cell;
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Rect {
pub x: u16,
pub y: u16,
pub w: u16,
pub h: u16,
}
impl Rect {
pub fn new(x: u16, y: u16, w: u16, h: u16) -> Self {
Self { x, y, w, h }
}
pub fn contains(&self, x: u16, y: u16) -> bool {
x >= self.x && x < self.x + self.w && y >= self.y && y < self.y + self.h
}
pub fn right(&self) -> u16 {
self.x + self.w
}
pub fn bottom(&self) -> u16 {
self.y + self.h
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Run {
pub x: u16,
pub y: u16,
pub len: u16,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Grid {
width: u16,
height: u16,
cells: Vec<Cell>,
pub cursor: Option<(u16, u16)>,
pub caret: Option<Bounds>,
pub paint: Vec<Paint>,
pub bands: Vec<u16>,
}
impl Grid {
pub fn scroll_rows(&mut self, top: u16, bottom: u16, by: i32) {
let width = self.width as usize;
let (top, bottom) = (
top as usize,
(bottom as usize).min(self.height as usize - 1),
);
if top > bottom || by == 0 {
return;
}
let rows = bottom + 1 - top;
let n = (by.unsigned_abs() as usize).min(rows);
let unknown = Cell {
symbol: CompactString::const_new("\u{fffe}"),
style: Style::default(),
};
let row = |y: usize| y * width..(y + 1) * width;
if by > 0 {
for y in top..bottom + 1 - n {
self.cells.copy_within_cells(row(y + n), row(y).start);
}
for y in bottom + 1 - n..=bottom {
self.cells[row(y)].fill(unknown.clone());
}
} else {
for y in (top + n..=bottom).rev() {
self.cells.copy_within_cells(row(y - n), row(y).start);
}
for y in top..top + n {
self.cells[row(y)].fill(unknown.clone());
}
}
}
pub fn new(width: u16, height: u16, style: Style) -> Self {
let blank = Cell {
symbol: " ".into(),
style,
};
Self {
width,
height,
cells: vec![blank; width as usize * height as usize],
cursor: None,
caret: None,
paint: Vec::new(),
bands: Vec::new(),
}
}
pub fn label(&mut self, x: u16, y: u16, text: &str, style: Style, max_x: u16) -> u16 {
self.label_at(x as f32, x, y, text, style, max_x, 100)
}
pub fn painted_so_far(&self) -> usize {
self.paint.len()
}
pub fn lift_since(&mut self, from: usize, rows: f32) {
let by = units(rows);
for paint in self.paint.iter_mut().skip(from) {
paint.lift(by);
}
}
#[allow(clippy::too_many_arguments)]
pub fn label_at(
&mut self,
x_px: f32,
x: u16,
y: u16,
text: &str,
style: Style,
max_x: u16,
size: u8,
) -> u16 {
let end = self.put_str(
x,
y,
text,
Style {
painted: true,
..style
},
max_x,
);
if y < self.height {
self.paint.push(Paint::Text {
x: units(x_px),
y: y as i32 * UNIT,
max_width: units(max_x.min(self.width) as f32 - x_px),
text: text.to_string(),
color: style.fg,
font: Font {
bold: style.bold,
size,
..Font::default()
},
align_right: false,
centred: false,
underline: false,
});
}
end
}
pub fn text_run(
&mut self,
(x, y): (f32, f32),
max_x: f32,
text: &str,
(color, underline): (Rgb, bool),
font: Font,
) {
self.paint.push(Paint::Text {
x: units(x),
y: units(y),
max_width: units(max_x - x),
text: text.to_string(),
color,
font,
align_right: false,
centred: false,
underline,
});
}
pub fn set_caret(&mut self, x: f32, y: u16, height: f32) {
self.cursor = Some((x as u16, y));
let top = y as f32 + (1.0 - height) / 2.0;
self.caret = Some(Bounds::cells(x, top, 0.0, height));
}
pub fn label_right(&mut self, right: u16, y: u16, text: &str, style: Style, size: u8) {
let cells = text.chars().count() as u16;
let x = right.saturating_sub(cells);
self.put_str(
x,
y,
text,
Style {
painted: true,
..style
},
right,
);
self.paint.push(Paint::Text {
x: units(right as f32),
y: y as i32 * UNIT,
max_width: units(right as f32),
text: text.to_string(),
color: style.fg,
font: Font {
bold: style.bold,
size,
..Font::default()
},
align_right: true,
centred: false,
underline: false,
});
}
pub fn field(&mut self, rect: Rect, behind: Rgb, fill: Rgb, border: Rgb) {
self.panel(Bounds::inset(rect, 0.0, 0.0), 0, Some(behind), None);
self.panel(
Bounds::inset(rect, 0.15, 0.11),
22,
Some(fill),
Some(border),
);
}
pub fn button(&mut self, x: u16, y: u16, cells: &str, label: &str, fill: Rgb, style: Style) {
self.button_of((BUTTON_HEIGHT, 0.0), (x, y), cells, label, fill, style);
}
pub fn large_button(
&mut self,
x: u16,
y: u16,
cells: &str,
label: &str,
fill: Rgb,
style: Style,
) {
self.button_of(
(LARGE_BUTTON_HEIGHT, 0.0),
(x, y),
cells,
label,
fill,
style,
);
}
pub fn large_button_between(
&mut self,
x: u16,
y: u16,
cells: &str,
label: &str,
fill: Rgb,
style: Style,
) {
self.button_of(
(LARGE_BUTTON_HEIGHT, 0.5),
(x, y),
cells,
label,
fill,
style,
);
}
fn button_of(
&mut self,
(height, drop): (f32, f32),
(x, y): (u16, u16),
cells: &str,
label: &str,
fill: Rgb,
style: Style,
) {
let width = cells.chars().count() as u16;
let (left, top, right, bottom) = button_span((height, drop), (x, y), width);
self.put_str(
x,
y,
cells,
Style {
painted: true,
..style
},
self.width,
);
let bounds = Bounds::cells(left, top, right - left, bottom - top);
self.panel(bounds, 22, Some(fill), None);
let middle = x as f32 + width as f32 / 2.0;
self.centred_text(middle, y as f32 + drop, label, style.fg, 90);
}
pub fn drop_last(&mut self, by: f32) {
match self.paint.last_mut() {
Some(Paint::Text { y, .. }) => *y += units(by),
Some(Paint::Icon { bounds, .. }) => bounds.y += units(by),
_ => {}
}
}
pub fn centred_text(&mut self, x: f32, y: f32, text: &str, color: Rgb, size: u8) {
self.paint.push(Paint::Text {
x: units(x),
y: units(y),
max_width: units(self.width as f32),
text: text.to_string(),
color,
font: Font {
size,
..Font::default()
},
align_right: false,
centred: true,
underline: false,
});
}
pub fn panel(&mut self, bounds: Bounds, radius: u8, fill: Option<Rgb>, border: Option<Rgb>) {
self.paint.push(Paint::Panel {
bounds,
radius,
fill,
border,
shadow: false,
dashed: false,
});
}
pub fn dashed_panel(&mut self, bounds: Bounds, radius: u8, fill: Option<Rgb>, border: Rgb) {
self.paint.push(Paint::Panel {
bounds,
radius,
fill,
border: Some(border),
shadow: false,
dashed: true,
});
}
pub fn popup(&mut self, bounds: Bounds, radius: u8, fill: Rgb, border: Rgb) {
self.paint.push(Paint::Panel {
bounds,
radius,
fill: Some(fill),
border: Some(border),
shadow: true,
dashed: false,
});
}
pub fn wash(
&mut self,
bounds: Bounds,
radius: u8,
(color, alpha): (Rgb, u8),
border: Option<Rgb>,
) {
self.paint.push(Paint::Wash {
bounds,
radius,
color,
alpha,
border,
});
}
pub fn hairline(&mut self, bounds: Bounds, color: Rgb) {
self.paint.push(Paint::Hairline { bounds, color });
}
pub fn glyph(&mut self, x: f32, y: u16, icon: Icon, color: Rgb, fallback: &str, style: Style) {
let cell = x.floor() as u16;
if cell < self.width && y < self.height {
self.put_str(
cell,
y,
fallback,
Style {
painted: true,
..style
},
self.width,
);
}
self.paint.push(Paint::Icon {
bounds: Bounds::cells(x, y as f32 + 0.14, 2.0, 0.72),
icon,
color,
});
}
pub fn corners(&mut self, bounds: Bounds, radius: u8, color: Rgb, which: u8) {
self.paint.push(Paint::Corners {
bounds,
radius,
color,
which,
});
}
pub fn paint_on_row(&self, y: u16) -> impl Iterator<Item = &Paint> {
let (top, bottom) = (y as i32 * UNIT, (y as i32 + 1) * UNIT);
self.paint.iter().filter(move |p| {
let (from, to) = p.span();
from < bottom && to > top
})
}
pub fn width(&self) -> u16 {
self.width
}
pub fn height(&self) -> u16 {
self.height
}
pub fn area(&self) -> Rect {
Rect::new(0, 0, self.width, self.height)
}
fn index(&self, x: u16, y: u16) -> usize {
y as usize * self.width as usize + x as usize
}
pub fn cell(&self, x: u16, y: u16) -> &Cell {
&self.cells[self.index(x, y)]
}
pub fn row(&self, y: u16) -> &[Cell] {
let start = self.index(0, y);
&self.cells[start..start + self.width as usize]
}
pub fn fill(&mut self, rect: Rect, style: Style) {
for y in rect.y..rect.bottom().min(self.height) {
for x in rect.x..rect.right().min(self.width) {
self.put_cell(x, y, " ", style);
}
}
}
fn put_cell(&mut self, x: u16, y: u16, symbol: &str, style: Style) {
let i = self.index(x, y);
if self.cells[i].symbol.is_empty() && x > 0 {
self.cells[i - 1].symbol = " ".into();
}
if x + 1 < self.width && self.cells[i + 1].symbol.is_empty() {
self.cells[i + 1].symbol = " ".into();
}
self.cells[i] = Cell {
symbol: symbol.into(),
style,
};
}
pub fn put_str(&mut self, x: u16, y: u16, text: &str, style: Style, max_x: u16) -> u16 {
if y >= self.height {
return x;
}
let max_x = max_x.min(self.width);
let mut at = x;
let text = visible(text);
for g in text.graphemes(true) {
if at >= max_x {
break;
}
let width = g.width() as u16;
if width == 0 {
continue;
}
if at + width > max_x {
self.put_cell(at, y, " ", style);
return at + 1;
}
self.put_cell(at, y, g, style);
if width == 2 {
self.put_cell(at + 1, y, "", style);
}
at += width;
}
at
}
pub fn put_icon(&mut self, x: u16, y: u16, icon: Icon, size: (u8, u8), style: Style) {
for row in 0..size.1 {
for column in 0..size.0 {
let (cx, cy) = (x + u16::from(column), y + u16::from(row));
if cx < self.width && cy < self.height {
let piece = Piece {
icon,
width: size.0,
height: size.1,
column,
row,
};
self.put_cell(cx, cy, piece.symbol().encode_utf8(&mut [0; 4]), style);
}
}
}
}
pub fn set_style(&mut self, rect: Rect, f: impl Fn(Style) -> Style) {
for y in rect.y..rect.bottom().min(self.height) {
for x in rect.x..rect.right().min(self.width) {
let i = self.index(x, y);
self.cells[i].style = f(self.cells[i].style);
}
}
}
pub fn diff(&self, prev: &Grid) -> Vec<Run> {
let same_size = self.width == prev.width && self.height == prev.height;
let mut runs = Vec::new();
for y in 0..self.height {
if same_size && self.row(y) == prev.row(y) {
continue;
}
let mut x = 0;
while x < self.width {
if same_size && self.cell(x, y) == prev.cell(x, y) {
x += 1;
continue;
}
let start = x;
while x < self.width && !(same_size && self.cell(x, y) == prev.cell(x, y)) {
x += 1;
}
let start = if self.cell(start, y).symbol.is_empty() && start > 0 {
start - 1
} else {
start
};
runs.push(Run {
x: start,
y,
len: x - start,
});
}
}
runs
}
pub fn row_text(&self, y: u16) -> String {
self.row(y).iter().map(|c| c.symbol.as_str()).collect()
}
pub fn text(&self) -> String {
(0..self.height)
.map(|y| self.row_text(y).trim_end().to_string())
.collect::<Vec<_>>()
.join("\n")
}
}
fn button_span((height, drop): (f32, f32), (x, y): (u16, u16), width: u16) -> (f32, f32, f32, f32) {
let top = y as f32 + drop + (1.0 - height) / 2.0;
(x as f32, top, (x + width) as f32, top + height)
}
pub fn on_large_button((x, y): (u16, u16), width: u16, at: (f32, f32)) -> bool {
let (left, top, right, bottom) = button_span((LARGE_BUTTON_HEIGHT, 0.0), (x, y), width);
at.0 >= left && at.0 < right && at.1 >= top && at.1 < bottom
}
#[cfg(test)]
mod tests {
#[test]
fn control_characters_never_reach_a_cell() {
let style = Style::default();
let mut grid = Grid::new(40, 2, style);
let text = "a\x1b]52;c;aGk=\x07b\r\n\x7f\u{9b}2Jc\td";
let end = grid.put_str(0, 0, text, style, 40);
let row: String = (0..end)
.map(|x| grid.cell(x, 0).symbol.to_string())
.collect();
assert_eq!(row, "a␛]52;c;aGk=␇b␍␊␡\u{fffd}2Jc d");
assert!(row.chars().all(|c| !c.is_control()), "{row:?}");
}
use super::*;
fn grid() -> Grid {
Grid::new(10, 3, Style::default())
}
#[test]
fn diff_emits_only_changed_cells() {
let before = grid();
let mut after = grid();
after.put_str(2, 1, "ab", Style::default(), 10);
after.put_str(7, 1, "c", Style::default(), 10);
assert_eq!(
after.diff(&before),
vec![Run { x: 2, y: 1, len: 2 }, Run { x: 7, y: 1, len: 1 }]
);
assert!(before.diff(&before.clone()).is_empty());
}
#[test]
fn resize_redraws_everything() {
let runs = grid().diff(&Grid::new(4, 4, Style::default()));
assert_eq!(runs.len(), 3);
assert!(runs.iter().all(|r| r.len == 10));
}
#[test]
fn wide_characters_take_two_cells() {
let mut g = grid();
let end = g.put_str(0, 0, "日本", Style::default(), 10);
assert_eq!(end, 4);
assert_eq!(g.cell(0, 0).symbol, "日");
assert_eq!(g.cell(1, 0).symbol, "");
assert_eq!(g.row_text(0).trim_end(), "日本");
}
#[test]
fn overwriting_half_a_wide_character_blanks_the_other_half() {
let mut g = grid();
g.put_str(0, 0, "日", Style::default(), 10);
g.put_str(1, 0, "x", Style::default(), 10);
assert_eq!(g.cell(0, 0).symbol, " ");
assert_eq!(g.cell(1, 0).symbol, "x");
}
#[test]
fn wide_character_at_the_edge_becomes_a_space() {
let mut g = grid();
g.put_str(9, 0, "日", Style::default(), 10);
assert_eq!(g.cell(9, 0).symbol, " ");
}
#[test]
fn combining_marks_stay_in_one_cell() {
let mut g = grid();
let end = g.put_str(0, 0, "e\u{301}x", Style::default(), 10);
assert_eq!(end, 2);
assert_eq!(g.cell(0, 0).symbol, "e\u{301}");
}
#[test]
fn diff_never_starts_on_a_continuation_cell() {
let mut before = grid();
before.put_str(0, 0, "日", Style::default(), 10);
let mut after = grid();
after.put_str(
0,
0,
"日",
Style {
bold: true,
..Style::default()
},
10,
);
let mut only_right = before.clone();
only_right.cells[1].style.bold = true;
assert_eq!(only_right.diff(&before)[0].x, 0);
assert_eq!(after.diff(&before)[0].x, 0);
}
}