Skip to main content

retroglyph_ui/draw/
primitives.rs

1//! [`fill_rect`]. The box-drawing codepoints previously here moved to
2//! [`retroglyph_core::symbols::border`], reachable by any crate rather than just this one.
3
4use retroglyph_core::color::Style;
5use retroglyph_core::grid::Rect;
6
7use crate::Surface;
8
9/// Fill `rect` with `ch` in the given `style`.
10///
11/// The entire rectangle including corners is overwritten. Kept as a plain
12/// function rather than a widget: there's no configuration to build up
13/// beyond the two arguments already here, and it's a building block other
14/// widgets (`Panel`, `Table`, `Scrollbar`) call directly.
15pub fn fill_rect(surface: &mut Surface<'_>, rect: Rect, ch: char, style: Style) {
16    surface.fill_rect(rect, ch, style);
17}
18
19#[cfg(test)]
20mod tests {
21    use retroglyph_core::grid::{Grid, Pos};
22
23    use super::*;
24
25    #[test]
26    fn fill_rect_overwrites_the_whole_rectangle() {
27        let area = Rect::new(0, 0, 6, 4);
28        let rect = Rect::new(1, 1, 4, 2);
29        let mut grid = Grid::new(6, 4);
30        fill_rect(
31            &mut Surface::new(&mut grid, area, 0),
32            rect,
33            '#',
34            Style::new(),
35        );
36
37        for y in 1..3 {
38            for x in 1..5 {
39                assert_eq!(grid[Pos::new(x, y)].glyph(), '#');
40            }
41        }
42        // Untouched outside the rect.
43        assert_eq!(grid[Pos::new(0, 0)].glyph(), ' ');
44    }
45}