hex_patch/app/widgets/
logo.rs

1use ratatui::{
2    style::{Color, Style},
3    widgets::Widget,
4};
5
6pub struct Logo {
7    colors: Vec<Style>,
8    matrix: Vec<Vec<usize>>,
9}
10
11impl Logo {
12    pub fn new() -> Self {
13        let c1 = Color::Rgb(231, 150, 86);
14        let c2 = Color::Rgb(144, 85, 38);
15        Self {
16            colors: vec![
17                Style::default(),
18                Style::default().bg(c1),
19                Style::default().bg(c2),
20                Style::default().fg(c1),
21            ],
22            matrix: vec![
23                vec![0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
24                vec![0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0],
25                vec![1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1],
26                vec![1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1],
27                vec![1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1],
28                vec![1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1],
29                vec![1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1],
30                vec![1, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1],
31                vec![0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0],
32                vec![0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
33            ],
34        }
35    }
36
37    pub fn get_size(&self) -> (u16, u16) {
38        (self.matrix[0].len() as u16, self.matrix.len() as u16 + 2)
39    }
40}
41
42impl Default for Logo {
43    fn default() -> Self {
44        Self::new()
45    }
46}
47
48impl Widget for Logo {
49    fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer)
50    where
51        Self: Sized,
52    {
53        for y in 0..self.matrix.len() {
54            for x in 0..self.matrix[y].len() {
55                let index = self.matrix[y][x];
56                if index != 0 && (x as u16) < area.width && (y as u16) < area.height {
57                    let style = self.colors[index];
58                    let x = x as u16;
59                    let y = y as u16;
60                    buf.set_string(x + area.x, y + area.y, " ", style);
61                }
62            }
63        }
64        let string = t!("hexpatch");
65        if (area.width < string.len() as u16) || (area.height < self.matrix.len() as u16 + 2) {
66            return;
67        }
68        buf.set_string(
69            self.matrix[0].len() as u16 / 2 - string.len() as u16 / 2 + area.x,
70            self.matrix.len() as u16 + 1 + area.y,
71            string,
72            self.colors[3],
73        )
74    }
75}