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
76
77
78
79
80
81
82
83
84
85
86
87
use crate::rect_ext::RectExt;
use crate::{Component, Event};
use crossterm::event::KeyCode;
use tui::buffer::Buffer;
use tui::layout::{Alignment, Rect};
use tui::style::{Color, Style};
use tui::text::{Span, Spans};
use tui::widgets::{Block, Borders, Clear, Paragraph, Widget};

#[derive(Debug, Clone)]
pub struct Confirm {
    choice: bool,
    title: String,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConfirmResponse {
    Confirm(bool),
    Handled,
    None,
}

impl Confirm {
    pub fn new<T: Into<String>>(title: T) -> Self {
        Self {
            choice: false,
            title: title.into(),
        }
    }
}

impl Component for Confirm {
    type Response = ConfirmResponse;
    type DrawResponse = ();

    fn handle_event(&mut self, event: Event) -> Self::Response {
        if let Event::Key(key_event) = event {
            match key_event.code {
                KeyCode::Right => {
                    self.choice = false;
                    ConfirmResponse::Handled
                }
                KeyCode::Left => {
                    self.choice = true;
                    ConfirmResponse::Handled
                }
                KeyCode::Enter => ConfirmResponse::Confirm(self.choice),
                KeyCode::Backspace | KeyCode::Esc => ConfirmResponse::Confirm(false),
                _ => ConfirmResponse::None,
            }
        } else {
            ConfirmResponse::None
        }
    }

    fn draw(&mut self, rect: Rect, buf: &mut Buffer) {
        let block = Block::default()
            .title(Span::styled(&self.title, Style::default().fg(Color::White)))
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::Yellow));

        let text_styles = if self.choice {
            [Style::default().fg(Color::Green), Style::default()]
        } else {
            [Style::default(), Style::default().fg(Color::Green)]
        };
        let inside_text = Spans::from(vec![
            Span::styled("Yes", text_styles[0]),
            Span::raw(" / "),
            Span::styled("No", text_styles[1]),
        ]);
        let max_width = (inside_text.width() + 2).max(self.title.len() + 2);
        let p = Paragraph::new(inside_text).alignment(Alignment::Center);

        let block_area = rect.centered(Rect {
            x: 0,
            y: 0,
            width: max_width as u16,
            height: 3,
        });
        let block_inner = block.inner(block_area);

        Widget::render(Clear, block_area, buf);
        Widget::render(block, block_area, buf);
        Widget::render(p, block_inner, buf);
    }
}