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.88;
const LARGE_BUTTON_HEIGHT: f32 = BUTTON_HEIGHT;
const ROW_BUTTON_RADIUS: u8 = 17;
const EXACT_FILL: u64 = u64::MAX - 1;
pub const WORDS_BASELINE: f32 = 0.026;
pub const GLYPH: f32 = 0.86;
pub const SMALL_GLYPH: f32 = 0.70;
pub const FIELD_AIR: f32 = 0.0625;
pub const GLYPH_LEAD: f32 = 0.15;
#[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,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum CursorShape {
#[default]
Bar,
Block,
Underline,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TermCursor {
pub at: (u16, u16),
pub on: Cell,
pub off: Cell,
pub shape: CursorShape,
pub blinks: bool,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
struct Blinked {
caret: Option<Bounds>,
cursor: Option<(u16, u16)>,
cells: Vec<(u16, u16, Cell)>,
}
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>,
pub edge: Option<Rgb>,
pub roomy: bool,
pub row_air: u16,
pub foot: u16,
pub row_bands: Option<(u16, Vec<u16>)>,
pub turn: Option<u8>,
pub spinning: bool,
turning: Vec<(u16, u16)>,
kept: Vec<((usize, usize), (u16, u16))>,
pub cursor_shape: CursorShape,
pub cursor_blinks: bool,
pub term_cursors: Vec<TermCursor>,
blinking: Vec<(u16, u16, Cell, Cell)>,
blinked: Option<Blinked>,
}
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(),
edge: None,
roomy: false,
row_air: 100,
foot: 1,
row_bands: None,
turn: Some(0),
spinning: false,
turning: Vec::new(),
kept: Vec::new(),
cursor_shape: CursorShape::Bar,
cursor_blinks: false,
term_cursors: Vec::new(),
blinking: Vec::new(),
blinked: None,
}
}
pub fn blink_cell(&mut self, x: u16, y: u16, off: Cell) {
if x < self.width && y < self.height {
let on = self.cell(x, y).clone();
self.blinking.push((x, y, off, on));
}
}
pub fn blinks(&self) -> bool {
self.cursor_blinks
&& (self.caret.is_some()
|| self.cursor.is_some()
|| self.blinked.is_some()
|| !self.blinking.is_empty())
}
pub fn caret_shown(&self) -> bool {
self.blinked.is_none()
}
pub fn blink_to(&mut self, shown: bool) {
if shown == self.caret_shown() || (!shown && !self.blinks()) {
return;
}
if shown {
let blinked = self.blinked.take().unwrap_or_default();
self.caret = blinked.caret;
self.cursor = blinked.cursor;
for (x, y, on) in blinked.cells {
let at = self.index(x, y);
self.cells[at] = on;
}
return;
}
let mut cells = Vec::new();
for (x, y, off, on) in &self.blinking {
let at = self.index(*x, *y);
if self.cells[at] == *on {
self.cells[at] = off.clone();
cells.push((*x, *y, on.clone()));
}
}
self.blinked = Some(Blinked {
caret: self.caret.take(),
cursor: self.cursor.take(),
cells,
});
}
pub fn spinner(&mut self, x: f32, y: u16, size: f32, color: Rgb, style: Style) {
self.spinner_in((x, x.floor() as u16), y, size, color, style);
}
pub fn spinner_in(
&mut self,
(x, cell): (f32, u16),
y: u16,
size: f32,
color: Rgb,
style: Style,
) {
let step = self.turn;
self.spinning |= step.is_some();
let letter = step.map_or(crate::spinner::STILL, |s| {
crate::spinner::FRAMES[s as usize % crate::spinner::FRAMES.len()]
});
self.mark((x, cell), y, size, color, style, (step, letter));
}
pub fn turn_to(&mut self, step: u8) {
if self.turn.is_none() {
return;
}
self.turn = Some(step);
for paint in &mut self.paint {
if let Paint::Spinner { step: Some(at), .. } = paint {
*at = step;
}
}
let letter = crate::spinner::FRAMES[step as usize % crate::spinner::FRAMES.len()];
for &(x, y) in &self.turning {
let at = y as usize * self.width as usize + x as usize;
let cell = self.cells.get_mut(at).filter(|_| x < self.width);
if let Some(cell) = cell.filter(|c| crate::spinner::FRAMES.contains(&c.symbol.as_str()))
{
cell.symbol = letter.into();
}
}
}
pub fn dot(&mut self, x: f32, y: u16, size: f32, color: Rgb, style: Style) {
self.dot_in((x, x.floor() as u16), y, size, color, style);
}
pub fn dot_in(&mut self, (x, cell): (f32, u16), y: u16, size: f32, color: Rgb, style: Style) {
let still = (None, crate::spinner::STILL);
self.mark((x, cell), y, size, color, style, still);
}
fn mark(
&mut self,
(x, cell): (f32, u16),
y: u16,
size: f32,
color: Rgb,
style: Style,
(step, letter): (Option<u8>, &str),
) {
if step.is_some() {
self.turning.push((cell, y));
}
if cell < self.width && y < self.height {
let shown = Style {
fg: color,
painted: true,
..style
};
self.put_str(cell, y, letter, shown, cell + 1);
}
self.paint.push(Paint::Spinner {
bounds: Bounds::cells(x, y as f32 + (1.0 - size) / 2.0, 2.0, size),
color,
step,
});
}
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 clear_paint_in(&mut self, area: Rect) {
let (left, right) = (i32::from(area.x) * UNIT, i32::from(area.right()) * UNIT);
let (top, bottom) = (i32::from(area.y) * UNIT, i32::from(area.bottom()) * UNIT);
self.paint.retain(|paint| {
let (from, _) = paint.reach();
let start = paint.top();
!(start >= top && start < bottom && from >= left && from < right)
});
}
pub fn pictures_again_in(&mut self, before: usize, area: Rect) {
let clip_to = |clip: Bounds, (left, top, right, bottom): (i32, i32, i32, i32)| {
let (x, y) = (clip.x.max(left), clip.y.max(top));
let (end_x, end_y) = ((clip.x + clip.w).min(right), (clip.y + clip.h).min(bottom));
Bounds {
x,
y,
w: end_x - x,
h: end_y - y,
}
};
let (left, right) = (i32::from(area.x) * UNIT, i32::from(area.right()) * UNIT);
let (top, bottom) = (i32::from(area.y) * UNIT, i32::from(area.bottom()) * UNIT);
let mut again = Vec::new();
for (index, paint) in self.paint.iter().enumerate().take(before) {
let Paint::Image {
bounds,
clip,
bitmap,
color,
} = paint
else {
continue;
};
if bitmap.id == EXACT_FILL {
continue;
}
let mut clip = clip_to(*clip, (left, top, right, bottom));
for ((from, to), (first, end)) in &self.kept {
if (*from..*to).contains(&index) {
let rows = (i32::from(*first) * UNIT, i32::from(*end) * UNIT);
clip = clip_to(clip, (left, rows.0, right, rows.1));
}
}
let shown = clip_to(
clip,
(bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h),
);
if shown.w > 0 && shown.h > 0 {
again.push(Paint::Image {
bounds: *bounds,
clip,
bitmap: bitmap.clone(),
color: *color,
});
}
}
self.paint.extend(again);
}
pub fn painted_so_far(&self) -> usize {
self.paint.len()
}
pub fn keep_to_rows(&mut self, from: usize, top: u16, bottom: u16) {
if from < self.paint.len() {
self.kept.push(((from, self.paint.len()), (top, bottom)));
}
}
pub fn insert_paint(&mut self, index: usize, paint: Paint) {
let index = index.min(self.paint.len());
self.paint.insert(index, paint);
for ((from, to), _) in &mut self.kept {
if index < *from {
*from += 1;
}
if index < *to {
*to += 1;
}
}
}
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);
}
}
pub fn nudge_since(&mut self, from: usize, columns: f32) {
let by = units(columns);
for paint in self.paint.iter_mut().skip(from) {
match paint {
Paint::Text { x, .. } => *x += by,
Paint::Panel { bounds, .. }
| Paint::Wash { bounds, .. }
| Paint::Hairline { bounds, .. }
| Paint::Icon { bounds, .. }
| Paint::Spinner { bounds, .. }
| Paint::Corners { bounds, .. }
| Paint::Strips { bounds, .. } => bounds.x += by,
Paint::Image { bounds, clip, .. } => {
bounds.x += by;
clip.x += 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,
span_centred: 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,
span_centred: false,
});
}
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) {
self.label_right_at(right as f32, right, y, text, style, size);
}
pub fn label_right_at(
&mut self,
right: f32,
cell: u16,
y: u16,
text: &str,
style: Style,
size: u8,
) {
let cells = text.chars().count() as u16;
let x = cell.saturating_sub(cells);
self.put_str(
x,
y,
text,
Style {
painted: true,
..style
},
cell,
);
self.paint.push(Paint::Text {
x: units(right),
y: y as i32 * UNIT,
max_width: units(right),
text: text.to_string(),
color: style.fg,
font: Font {
bold: style.bold,
size,
..Font::default()
},
align_right: true,
centred: false,
underline: false,
span_centred: 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, FIELD_AIR),
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 row_button(
&mut self,
(x, y): (u16, u16),
cells: &str,
label: &str,
fill: Rgb,
style: Style,
) {
let from = self.paint.len();
self.button(x, y, cells, label, fill, style);
if !self.roomy {
return;
}
for paint in &mut self.paint[from..] {
if let Paint::Panel { inset, radius, .. } = paint {
*inset = true;
*radius = ROW_BUTTON_RADIUS;
}
}
self.beside_words(from);
}
pub fn row_chip(&mut self, (left, right): (f32, f32), y: u16, fill: Rgb) {
let (_, top, _, bottom) = button_span((BUTTON_HEIGHT, 0.0), (0, y), 1);
let (radius, inset) = if self.roomy {
(ROW_BUTTON_RADIUS, true)
} else {
(22, false)
};
self.paint.push(Paint::Panel {
bounds: Bounds::cells(left, top, right - left, bottom - top),
radius,
fill: Some(fill),
border: None,
shadow: false,
dashed: false,
inset,
});
}
pub fn beside_words(&mut self, from: usize) {
if self.roomy {
let line = 100.0 / f32::from(self.row_air.max(1));
self.lift_since(from, -WORDS_BASELINE * line);
}
}
pub fn exact_fill(&mut self, bounds: Bounds, color: Rgb) {
let bitmap = crate::paint::Bitmap {
id: EXACT_FILL,
width: 1,
height: 1,
kind: crate::paint::Pixels::Coverage,
pixels: std::sync::Arc::new(vec![255]),
};
self.paint.push(Paint::Image {
bounds,
clip: bounds,
bitmap,
color,
});
}
pub fn exact_fill_under(&mut self, index: usize, bounds: Bounds, color: Rgb) {
self.exact_fill(bounds, color);
if let Some(fill) = self.paint.pop() {
self.insert_paint(index, fill);
}
}
pub fn chip(&mut self, x: f32, y: u16, wide: f32, fill: Rgb) {
let top = y as f32 + (1.0 - BUTTON_HEIGHT) / 2.0;
let bounds = Bounds::cells(x, top, wide, BUTTON_HEIGHT);
self.panel(bounds, 22, Some(fill), None);
}
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.button_label(middle, y as f32 + drop, label, style.fg, 90);
}
pub fn button_words(&mut self, middle: f32, y: u16, label: &str, color: Rgb) {
self.button_label(middle, y as f32, label, color, 90);
}
pub fn button_label(&mut self, x: f32, y: f32, text: &str, color: Rgb, size: u8) {
self.centred_text(x, y, text, color, size);
self.centre_span_since(self.paint.len() - 1);
}
pub fn centre_span_since(&mut self, from: usize) {
for paint in self.paint.iter_mut().skip(from) {
if let Paint::Text { span_centred, .. } = paint {
*span_centred = true;
}
}
}
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,
span_centred: 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,
inset: 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,
inset: false,
});
}
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,
inset: false,
});
}
pub fn exact_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,
inset: true,
});
}
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) {
self.glyph_in((x, x.floor() as u16), y, (icon, color), fallback, style);
}
pub fn glyph_in(
&mut self,
(x, cell): (f32, u16),
y: u16,
(icon, color): (Icon, Rgb),
fallback: &str,
style: Style,
) {
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 - GLYPH_LEAD, y as f32 + (1.0 - GLYPH) / 2.0, 2.0, GLYPH),
icon,
color,
});
}
#[allow(clippy::too_many_arguments)]
pub fn small_glyph(
&mut self,
x: f32,
y: u16,
icon: Icon,
color: Rgb,
fallback: &str,
style: Style,
) {
self.glyph(x, y, icon, color, fallback, style);
if let Some(Paint::Icon { bounds, .. }) = self.paint.last_mut() {
let top = y as f32 + (1.0 - SMALL_GLYPH) / 2.0;
*bounds = Bounds::cells(x, top, 2.0, SMALL_GLYPH);
}
}
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);
let kept_off = move |index: usize| {
self.kept.iter().any(|((from, to), (first, end))| {
(*from..*to).contains(&index) && !(*first..*end).contains(&y)
})
};
self.paint
.iter()
.enumerate()
.filter(move |(index, p)| {
let (from, to) = p.span();
from < bottom && to > top && !kept_off(*index)
})
.map(|(_, paint)| paint)
}
pub fn spans(&self, y: u16) -> Vec<(u16, u16)> {
let width = self.width;
let own = self.row_bands.as_ref().filter(|(row, _)| *row == y);
let bands = match own {
Some((_, bands)) => bands,
None if y + 1 == self.height => return vec![(0, width)],
None => &self.bands,
};
let inside = bands.iter().copied().filter(|x| *x > 0 && *x < width);
let edges: Vec<u16> = [0].into_iter().chain(inside).chain([width]).collect();
edges.windows(2).map(|edge| (edge[0], edge[1])).collect()
}
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 air_under_button(row: f32, line: f32) -> f32 {
row - button_in_row(row, line).1
}
pub fn row_button_in_row(row: f32, line: f32) -> (f32, f32) {
let (_, top, _, bottom) = button_span((BUTTON_HEIGHT, 0.0), (0, 0), 1);
let drop = units(WORDS_BASELINE * line / row) as f32 * row / UNIT as f32;
let (top, tall) = crate::raster::inset(units(top), units(bottom - top), row, line);
(top + drop, top + drop + tall)
}
pub fn button_in_row(row: f32, line: f32) -> (f32, f32) {
let (_, top, _, bottom) = button_span((BUTTON_HEIGHT, 0.0), (0, 0), 1);
let bounds = Bounds::cells(0.0, top, 1.0, bottom - top);
let (y, h) = crate::raster::stood(bounds.y, bounds.h, row, line);
(y, y + h)
}
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 paint_kept_to_its_rows_is_on_no_other() {
let style = Style::default();
let mut grid = Grid::new(10, 6, style);
let panel = |grid: &mut Grid, y: f32, h: f32| {
grid.panel(Bounds::cells(0.0, y, 10.0, h), 0, Some(Rgb(1, 2, 3)), None);
};
panel(&mut grid, 0.0, 1.0);
let from = grid.painted_so_far();
panel(&mut grid, 1.0, 4.5);
grid.keep_to_rows(from, 2, 4);
panel(&mut grid, 5.0, 1.0);
let on = |grid: &Grid, y: u16| grid.paint_on_row(y).count();
let rows: Vec<usize> = (0..6).map(|y| on(&grid, y)).collect();
assert_eq!(rows, [1, 0, 1, 1, 0, 1]);
grid.insert_paint(
0,
Paint::Hairline {
bounds: Bounds::cells(0.0, 1.0, 10.0, 0.0),
color: Rgb(9, 9, 9),
},
);
let rows: Vec<usize> = (0..6).map(|y| on(&grid, y)).collect();
assert_eq!(rows, [1, 1, 1, 1, 0, 1]);
}
#[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);
}
#[test]
fn a_step_on_the_drawn_grid_is_the_grid_drawn_at_that_step() {
let draw = |step: u8| {
let mut grid = Grid::new(30, 4, Style::default());
grid.turn = Some(step);
grid.put_str(0, 1, "working", Style::default(), 30);
grid.spinner(9.0, 1, 0.65, Rgb(1, 2, 3), Style::default());
grid.dot(12.0, 1, 0.65, Rgb(4, 5, 6), Style::default());
grid.spinner(3.0, 3, 0.65, Rgb(1, 2, 3), Style::default());
grid
};
let mut stepped = draw(0);
for step in [1, 7, 0, 9] {
stepped.turn_to(step);
assert_eq!(stepped, draw(step), "step {step}");
}
assert_eq!(
stepped.cell(12, 1).symbol,
crate::spinner::STILL,
"a dot stays"
);
let mut covered = draw(0);
covered.put_str(8, 1, "menu", Style::default(), 30);
covered.turn_to(4);
assert_eq!(covered.row_text(1).trim_end(), "working menu●");
let mut still = Grid::new(30, 4, Style::default());
still.turn = None;
still.spinner(9.0, 1, 0.65, Rgb(1, 2, 3), Style::default());
let before = still.clone();
still.turn_to(3);
assert_eq!(still, before);
}
}