aboutsummaryrefslogtreecommitdiff
path: root/src/gui/message.rs
blob: 01d124e0b994564f031fed1490e3110c182a8225 (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
37
38
39
40
41
use crate::gui::util;
use crate::util::event::{Event, Events};
use anyhow::Result;
use termion::event::Key;
use tui::{
    backend::Backend,
    layout::{Alignment, Constraint, Direction, Layout},
    widgets::Paragraph,
    Terminal,
};

pub fn show<B: Backend>(
    terminal: &mut Terminal<B>,
    events: &Events,
    title: &String,
    message: &String,
) -> Result<()> {
    loop {
        terminal.draw(|f| {
            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .margin(2)
                .constraints([Constraint::Length(1), Constraint::Percentage(50)].as_ref())
                .split(f.size());

            let d1 = util::title(title);
            f.render_widget(d1, chunks[0]);

            let message = Paragraph::new(util::center_vertically(chunks[1], &message))
                .alignment(Alignment::Center);
            f.render_widget(message, chunks[1]);
        })?;

        if let Event::Input(key) = events.next()? {
            match key {
                Key::Char('q') => return Ok(()),
                _ => (),
            }
        }
    }
}