1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crossterm::style::ContentStyle;

use crate::{
    canvas::CanvasLike,
    layout::{Dims, Pos},
};

use super::{styled::Stylable, Drawable};

#[derive(Clone, Copy)]
pub struct Dbox {
    pub size: Dims,
}

impl Dbox {
    pub fn new(size: impl Into<Dims>) -> Self {
        Self { size: size.into() }
    }
}

impl Drawable for Dbox {
    type X = i32;
    type Y = i32;

    fn draw(&self, pos: impl Into<Dims>, frame: &mut impl CanvasLike) {
        let pos = pos.into();
        self.styled(ContentStyle::default()).draw(pos, frame);
    }
}

impl Drawable for (ContentStyle, &Dbox) {
    type X = i32;
    type Y = i32;

    fn draw(&self, pos: impl Into<Dims>, frame: &mut impl CanvasLike) {
        let (style, dbox) = self;
        let Dbox {
            size: Pos { x: w, y: h },
        } = **dbox;
        let pos @ Pos { x, y } = pos.into().clone();

        format!("╭{}╮", "─".repeat(w as usize - 2))
            .styled(*style)
            .draw(pos, frame);

        for y in y + 1..y + h - 1 {
            // frame.draw_on((x, y), '│'.styled(*style));
            '│'.styled(*style).draw((x, y), frame);
            // frame.draw_on((x + w - 1, y), '│'.styled(*style));
            '│'.styled(*style).draw((x + w - 1, y), frame);
        }

        // frame.draw(
        //     (x, y + h - 1),
        //     format!("╰{}╯", "─".repeat(w as usize - 2)).styled(*style),
        // );
        format!("╰{}╯", "─".repeat(w as usize - 2))
            .styled(*style)
            .draw((x, y + h - 1), frame);
    }
}