summaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/tui_menu.rs
blob: 66fa38d87650ca950f400457064ef229230deae2 (plain)
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
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::{Color, Style};
use tui::widgets::{Block, Borders, Widget};

pub struct TuiMenu<'a> {
    options: &'a [&'a str],
}

impl<'a> TuiMenu<'a> {
    pub fn new(options: &'a [&'a str]) -> Self {
        Self { options }
    }

    pub fn len(&self) -> usize {
        self.options.len()
    }
}

impl<'a> Widget for TuiMenu<'a> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let style = Style::default().fg(Color::Reset).bg(Color::Reset);

        Block::default()
            .style(style)
            .borders(Borders::TOP)
            .render(area, buf);

        let text_iter = self.options.iter().chain(&[" "]);
        let area_x = area.x + 1;

        for (y, text) in (1..area.height).zip(text_iter) {
            buf.set_string(area_x, y, text, style);
        }
    }
}