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
62
63
64
65
66
67
use crossterm::style::ContentStyle;

use super::{Cell, Frame, Pos};

pub trait Drawable {
    fn draw(&self, pos: Pos, frame: &mut Frame);
    fn draw_with_style(&self, pos: Pos, frame: &mut Frame, style: ContentStyle);
}

impl Drawable for char {
    fn draw(&self, pos: Pos, frame: &mut Frame) {
        frame.put_char(pos, *self);
    }

    fn draw_with_style(&self, pos: Pos, frame: &mut Frame, style: ContentStyle) {
        frame.put_char_styled(pos, *self, style);
    }
}

impl Drawable for String {
    fn draw(&self, pos: Pos, frame: &mut Frame) {
        self.as_str().draw(pos, frame);
    }

    fn draw_with_style(&self, pos: Pos, frame: &mut Frame, style: ContentStyle) {
        self.as_str().draw_with_style(pos, frame, style);
    }
}

impl<'a> Drawable for &'a str {
    fn draw(&self, pos: Pos, frame: &mut Frame) {
        for (i, character) in self.chars().enumerate() {
            frame.put_char((pos.0 + i as u16, pos.1), character);
        }
    }

    fn draw_with_style(&self, pos: Pos, frame: &mut Frame, style: ContentStyle) {
        for (i, character) in self.chars().enumerate() {
            frame.put_char_styled((pos.0 + i as u16, pos.1), character, style);
        }
    }
}

impl Drawable for Cell {
    fn draw(&self, pos: Pos, frame: &mut Frame) {
        frame.set(pos, *self);
    }

    fn draw_with_style(&self, pos: Pos, frame: &mut Frame, style: ContentStyle) {
        let mut cell = *self;
        if let Cell::Content(content) = &mut cell {
            content.style = style;
        }

        frame.set(pos, cell);
    }
}

impl<D: Drawable> Drawable for (D, ContentStyle) {
    fn draw(&self, pos: Pos, frame: &mut Frame) {
        self.0.draw_with_style(pos, frame, self.1);
    }

    fn draw_with_style(&self, pos: Pos, frame: &mut Frame, style: ContentStyle) {
        self.0.draw_with_style(pos, frame, style);
    }
}