waxterm/widgets/
confirm.rs1use ratatui::layout::Rect;
2use ratatui::style::{Color, Modifier, Style};
3use ratatui::text::{Line, Span};
4use ratatui::widgets::Paragraph;
5use ratatui::Frame;
6
7use crate::input::KeyPress;
8
9use super::WidgetResult;
10
11pub struct ConfirmDialog {
13 label: String,
14 value: bool,
15}
16
17impl ConfirmDialog {
18 pub fn new(label: impl Into<String>) -> Self {
19 Self {
20 label: label.into(),
21 value: true,
22 }
23 }
24
25 pub fn with_default(mut self, default: bool) -> Self {
26 self.value = default;
27 self
28 }
29
30 pub fn value(&self) -> bool {
32 self.value
33 }
34
35 pub fn handle_key(&mut self, key: &KeyPress) -> WidgetResult<bool> {
36 match key {
37 KeyPress::ArrowLeft | KeyPress::ArrowRight => {
38 self.value = !self.value;
39 WidgetResult::Pending
40 }
41 KeyPress::Char(b'y') | KeyPress::Char(b'Y') => {
42 self.value = true;
43 WidgetResult::Confirmed(true)
44 }
45 KeyPress::Char(b'n') | KeyPress::Char(b'N') => {
46 self.value = false;
47 WidgetResult::Confirmed(false)
48 }
49 KeyPress::Enter => WidgetResult::Confirmed(self.value),
50 KeyPress::Escape => WidgetResult::Cancelled,
51 _ => WidgetResult::Pending,
52 }
53 }
54
55 pub fn render(&self, frame: &mut Frame, area: Rect) {
56 let yes_style = if self.value {
57 Style::default()
58 .fg(Color::Green)
59 .add_modifier(Modifier::BOLD)
60 } else {
61 Style::default().fg(Color::DarkGray)
62 };
63
64 let no_style = if !self.value {
65 Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)
66 } else {
67 Style::default().fg(Color::DarkGray)
68 };
69
70 let line = Line::from(vec![
71 Span::styled(&self.label, Style::default().fg(Color::Cyan)),
72 Span::raw(" "),
73 Span::styled(if self.value { "[Yes]" } else { " Yes " }, yes_style),
74 Span::raw(" / "),
75 Span::styled(if !self.value { "[No]" } else { " No " }, no_style),
76 ]);
77
78 let paragraph = Paragraph::new(line);
79 frame.render_widget(paragraph, area);
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn test_default_yes() {
89 let mut dialog = ConfirmDialog::new("Continue?");
90 assert!(dialog.value());
91 assert_eq!(
92 dialog.handle_key(&KeyPress::Enter),
93 WidgetResult::Confirmed(true)
94 );
95 }
96
97 #[test]
98 fn test_default_no() {
99 let mut dialog = ConfirmDialog::new("Delete?").with_default(false);
100 assert!(!dialog.value());
101 assert_eq!(
102 dialog.handle_key(&KeyPress::Enter),
103 WidgetResult::Confirmed(false)
104 );
105 }
106
107 #[test]
108 fn test_arrow_toggles() {
109 let mut dialog = ConfirmDialog::new("?");
110 assert!(dialog.value());
111 dialog.handle_key(&KeyPress::ArrowRight);
112 assert!(!dialog.value());
113 dialog.handle_key(&KeyPress::ArrowLeft);
114 assert!(dialog.value());
115 }
116
117 #[test]
118 fn test_y_n_shortcuts() {
119 let mut dialog = ConfirmDialog::new("?");
120 assert_eq!(
121 dialog.handle_key(&KeyPress::Char(b'n')),
122 WidgetResult::Confirmed(false)
123 );
124
125 let mut dialog2 = ConfirmDialog::new("?").with_default(false);
126 assert_eq!(
127 dialog2.handle_key(&KeyPress::Char(b'y')),
128 WidgetResult::Confirmed(true)
129 );
130 }
131
132 #[test]
133 fn test_escape() {
134 let mut dialog = ConfirmDialog::new("?");
135 assert_eq!(
136 dialog.handle_key(&KeyPress::Escape),
137 WidgetResult::Cancelled
138 );
139 }
140}