hex_patch/app/widgets/
logo.rs

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
68
69
70
71
72
73
74
75
use ratatui::{
    style::{Color, Style},
    widgets::Widget,
};

pub struct Logo {
    colors: Vec<Style>,
    matrix: Vec<Vec<usize>>,
}

impl Logo {
    pub fn new() -> Self {
        let c1 = Color::Rgb(231, 150, 86);
        let c2 = Color::Rgb(144, 85, 38);
        Self {
            colors: vec![
                Style::default(),
                Style::default().bg(c1),
                Style::default().bg(c2),
                Style::default().fg(c1),
            ],
            matrix: vec![
                vec![0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
                vec![0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0],
                vec![1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1],
                vec![1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1],
                vec![1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1],
                vec![1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1],
                vec![1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1],
                vec![1, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1],
                vec![0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0],
                vec![0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
            ],
        }
    }

    pub fn get_size(&self) -> (u16, u16) {
        (self.matrix[0].len() as u16, self.matrix.len() as u16 + 2)
    }
}

impl Default for Logo {
    fn default() -> Self {
        Self::new()
    }
}

impl Widget for Logo {
    fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
    where
        Self: Sized,
    {
        for y in 0..self.matrix.len() {
            for x in 0..self.matrix[y].len() {
                let index = self.matrix[y][x];
                if index != 0 && (x as u16) < area.width && (y as u16) < area.height {
                    let style = self.colors[index];
                    let x = x as u16;
                    let y = y as u16;
                    buf.set_string(x + area.x, y + area.y, " ", style);
                }
            }
        }
        let string = "HexPatch";
        if (area.width < string.len() as u16) || (area.height < self.matrix.len() as u16 + 2) {
            return;
        }
        buf.set_string(
            self.matrix[0].len() as u16 / 2 - string.len() as u16 / 2 + area.x,
            self.matrix.len() as u16 + 1 + area.y,
            "HexPatch",
            self.colors[3],
        )
    }
}