Skip to main content

comb/draw/
border.rs

1//! Box-drawing borders and framed blocks, added as methods on [`Buffer`].
2
3use crate::core::buffer::Buffer;
4use crate::core::geom::Rect;
5use crate::core::style::Style;
6use crate::core::text::{Line, Span};
7
8#[derive(Clone, Copy, PartialEq, Eq, Debug)]
9pub enum Border {
10    Plain,
11    Rounded,
12    Double,
13    Thick,
14}
15
16struct Glyphs {
17    tl: char,
18    tr: char,
19    bl: char,
20    br: char,
21    h: char,
22    v: char,
23}
24
25fn glyphs(b: Border) -> Glyphs {
26    match b {
27        Border::Plain => Glyphs {
28            tl: '┌',
29            tr: '┐',
30            bl: '└',
31            br: '┘',
32            h: '─',
33            v: '│',
34        },
35        Border::Rounded => Glyphs {
36            tl: '╭',
37            tr: '╮',
38            bl: '╰',
39            br: '╯',
40            h: '─',
41            v: '│',
42        },
43        Border::Double => Glyphs {
44            tl: '╔',
45            tr: '╗',
46            bl: '╚',
47            br: '╝',
48            h: '═',
49            v: '║',
50        },
51        Border::Thick => Glyphs {
52            tl: '┏',
53            tr: '┓',
54            bl: '┗',
55            br: '┛',
56            h: '━',
57            v: '┃',
58        },
59    }
60}
61
62impl Buffer {
63    /// Draw a border around `area` (needs at least 2×2).
64    pub fn border(&mut self, area: Rect, b: Border, style: Style) {
65        if area.width < 2 || area.height < 2 {
66            return;
67        }
68        let g = glyphs(b);
69        let (x0, y0) = (area.x, area.y);
70        let (x1, y1) = (area.right() - 1, area.bottom() - 1);
71        self.set(x0, y0, g.tl, style);
72        self.set(x1, y0, g.tr, style);
73        self.set(x0, y1, g.bl, style);
74        self.set(x1, y1, g.br, style);
75        for x in (x0 + 1)..x1 {
76            self.set(x, y0, g.h, style);
77            self.set(x, y1, g.h, style);
78        }
79        for y in (y0 + 1)..y1 {
80            self.set(x0, y, g.v, style);
81            self.set(x1, y, g.v, style);
82        }
83    }
84
85    /// Optionally fill `area`, then draw a border — a framed panel in one call.
86    pub fn block(&mut self, area: Rect, b: Border, border_style: Style, fill: Option<Style>) {
87        if let Some(f) = fill {
88            self.paint(area, f);
89        }
90        self.border(area, b, border_style);
91    }
92
93    /// Draw a border with a title inlaid on the top edge (` title `), padded so
94    /// it sits off the corner.
95    pub fn border_title(&mut self, area: Rect, b: Border, style: Style, title: &Line) {
96        self.border(area, b, style);
97        if area.width < 6 {
98            return;
99        }
100        let mut t = Line::new();
101        t.push(Span::styled(" ", style));
102        for s in &title.spans {
103            t.push(s.clone());
104        }
105        t.push(Span::styled(" ", style));
106        self.set_line(area.x + 2, area.y, &t, area.width.saturating_sub(4));
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113    use crate::core::geom::Size;
114    use crate::core::style::Style;
115
116    #[test]
117    fn rounded_border_corners() {
118        let mut b = Buffer::blank(Size::new(5, 3));
119        b.border(Rect::new(0, 0, 5, 3), Border::Rounded, Style::new());
120        assert_eq!(b.get(0, 0).unwrap().ch, '╭');
121        assert_eq!(b.get(4, 0).unwrap().ch, '╮');
122        assert_eq!(b.get(0, 2).unwrap().ch, '╰');
123        assert_eq!(b.get(4, 2).unwrap().ch, '╯');
124        assert_eq!(b.get(2, 0).unwrap().ch, '─');
125        assert_eq!(b.get(0, 1).unwrap().ch, '│');
126    }
127}