tui_components/components/
confirm.rs

1use crate::rect_ext::RectExt;
2use crate::{Component, Event};
3use crossterm::event::KeyCode;
4use tui::buffer::Buffer;
5use tui::layout::{Alignment, Rect};
6use tui::style::{Color, Style};
7use tui::text::{Span, Spans};
8use tui::widgets::{Block, Borders, Clear, Paragraph, Widget};
9
10#[derive(Debug, Clone)]
11pub struct Confirm {
12    choice: bool,
13    title: String,
14}
15
16#[derive(Debug, Clone, Copy, PartialEq)]
17pub enum ConfirmResponse {
18    Confirm(bool),
19    Handled,
20    None,
21}
22
23impl Confirm {
24    pub fn new<T: Into<String>>(title: T) -> Self {
25        Self {
26            choice: false,
27            title: title.into(),
28        }
29    }
30}
31
32impl Component for Confirm {
33    type Response = ConfirmResponse;
34    type DrawResponse = ();
35
36    fn handle_event(&mut self, event: Event) -> Self::Response {
37        if let Event::Key(key_event) = event {
38            match key_event.code {
39                KeyCode::Right => {
40                    self.choice = false;
41                    ConfirmResponse::Handled
42                }
43                KeyCode::Left => {
44                    self.choice = true;
45                    ConfirmResponse::Handled
46                }
47                KeyCode::Enter => ConfirmResponse::Confirm(self.choice),
48                KeyCode::Backspace | KeyCode::Esc => ConfirmResponse::Confirm(false),
49                _ => ConfirmResponse::None,
50            }
51        } else {
52            ConfirmResponse::None
53        }
54    }
55
56    fn draw(&mut self, rect: Rect, buf: &mut Buffer) {
57        let block = Block::default()
58            .title(Span::styled(&self.title, Style::default().fg(Color::White)))
59            .borders(Borders::ALL)
60            .border_style(Style::default().fg(Color::Yellow));
61
62        let text_styles = if self.choice {
63            [Style::default().fg(Color::Green), Style::default()]
64        } else {
65            [Style::default(), Style::default().fg(Color::Green)]
66        };
67        let inside_text = Spans::from(vec![
68            Span::styled("Yes", text_styles[0]),
69            Span::raw(" / "),
70            Span::styled("No", text_styles[1]),
71        ]);
72        let max_width = (inside_text.width() + 2).max(self.title.len() + 2);
73        let p = Paragraph::new(inside_text).alignment(Alignment::Center);
74
75        let block_area = rect.centered(Rect {
76            x: 0,
77            y: 0,
78            width: max_width as u16,
79            height: 3,
80        });
81        let block_inner = block.inner(block_area);
82
83        Widget::render(Clear, block_area, buf);
84        Widget::render(block, block_area, buf);
85        Widget::render(p, block_inner, buf);
86    }
87}