ratado 0.1.0

A fast, keyboard-driven terminal task manager built with Rust and Ratatui
Documentation
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Settings dialog for application configuration.
//!
//! This dialog provides options for managing application data:
//! - Reset database (delete all data)
//! - Delete all completed tasks

use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    layout::{Alignment, Constraint, Layout},
    style::{Modifier, Style},
    text::{Line, Span},
    widgets::{Clear, Paragraph},
    Frame,
};

use super::{
    button_danger_style, button_focused_style, centered_rect, dialog_block, hint_style,
    DialogAction,
};
use crate::ui::theme;

/// Available settings options.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SettingsOption {
    /// Delete all completed tasks from all projects
    DeleteCompletedTasks,
    /// Reset entire database to default state
    ResetDatabase,
}

impl SettingsOption {
    /// Returns the display label for this option.
    fn label(&self) -> &'static str {
        match self {
            SettingsOption::DeleteCompletedTasks => "Delete all completed tasks",
            SettingsOption::ResetDatabase => "Reset database (delete everything)",
        }
    }

    /// Returns whether this option is destructive.
    fn is_destructive(&self) -> bool {
        true // Both options are destructive
    }
}

/// The settings dialog state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DialogState {
    /// Selecting an option from the menu
    Menu,
    /// Confirming a destructive action
    Confirming,
}

/// Settings dialog for application configuration.
#[derive(Debug)]
pub struct SettingsDialog {
    /// Currently selected option index
    selected_index: usize,
    /// Available options
    options: Vec<SettingsOption>,
    /// Current dialog state
    state: DialogState,
    /// The option being confirmed (if in confirming state)
    confirming_option: Option<SettingsOption>,
    /// Whether "Yes" is selected in confirmation
    confirm_selected_yes: bool,
}

impl Default for SettingsDialog {
    fn default() -> Self {
        Self::new()
    }
}

impl SettingsDialog {
    /// Creates a new settings dialog.
    pub fn new() -> Self {
        Self {
            selected_index: 0,
            options: vec![
                SettingsOption::DeleteCompletedTasks,
                SettingsOption::ResetDatabase,
            ],
            state: DialogState::Menu,
            confirming_option: None,
            confirm_selected_yes: false,
        }
    }

    /// Returns the currently selected option.
    pub fn selected_option(&self) -> Option<SettingsOption> {
        self.options.get(self.selected_index).copied()
    }

    /// Returns the confirmed option (after user confirms).
    pub fn confirmed_option(&self) -> Option<SettingsOption> {
        self.confirming_option
    }

    /// Handles a key event and returns the resulting action.
    pub fn handle_key(&mut self, key: KeyEvent) -> DialogAction {
        match self.state {
            DialogState::Menu => self.handle_menu_key(key),
            DialogState::Confirming => self.handle_confirm_key(key),
        }
    }

    /// Handles key events in menu state.
    fn handle_menu_key(&mut self, key: KeyEvent) -> DialogAction {
        match key.code {
            // Cancel
            KeyCode::Esc => DialogAction::Cancel,

            // Navigation
            KeyCode::Up | KeyCode::Char('k') => {
                if self.selected_index > 0 {
                    self.selected_index -= 1;
                }
                DialogAction::None
            }
            KeyCode::Down | KeyCode::Char('j') => {
                if self.selected_index < self.options.len() - 1 {
                    self.selected_index += 1;
                }
                DialogAction::None
            }

            // Select option
            KeyCode::Enter => {
                if let Some(option) = self.selected_option() {
                    if option.is_destructive() {
                        // Show confirmation for destructive actions
                        self.state = DialogState::Confirming;
                        self.confirming_option = Some(option);
                        self.confirm_selected_yes = false;
                    } else {
                        return DialogAction::Submit;
                    }
                }
                DialogAction::None
            }

            _ => DialogAction::None,
        }
    }

    /// Handles key events in confirmation state.
    fn handle_confirm_key(&mut self, key: KeyEvent) -> DialogAction {
        match key.code {
            // Direct yes/no with y/n keys
            KeyCode::Char('y') | KeyCode::Char('Y') => DialogAction::Submit,
            KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => {
                // Go back to menu
                self.state = DialogState::Menu;
                self.confirming_option = None;
                DialogAction::None
            }

            // Toggle selection
            KeyCode::Left | KeyCode::Right | KeyCode::Tab | KeyCode::Char('h') | KeyCode::Char('l') => {
                self.confirm_selected_yes = !self.confirm_selected_yes;
                DialogAction::None
            }

            // Confirm selection
            KeyCode::Enter => {
                if self.confirm_selected_yes {
                    DialogAction::Submit
                } else {
                    // Go back to menu
                    self.state = DialogState::Menu;
                    self.confirming_option = None;
                    DialogAction::None
                }
            }

            _ => DialogAction::None,
        }
    }

    /// Renders the dialog to the frame.
    pub fn render(&self, frame: &mut Frame) {
        match self.state {
            DialogState::Menu => self.render_menu(frame),
            DialogState::Confirming => self.render_confirmation(frame),
        }
    }

    /// Renders the menu state.
    fn render_menu(&self, frame: &mut Frame) {
        let area = frame.area();

        // Dialog dimensions - fixed size for consistency
        let dialog_width = 50.min(area.width.saturating_sub(4));
        let dialog_height = 9.min(area.height.saturating_sub(4));
        let dialog_area = centered_rect(dialog_width, dialog_height, area);

        // Render dimmed background
        frame.render_widget(Clear, area);
        frame.render_widget(
            Paragraph::new("").style(Style::default().bg(theme::BG_DARK)),
            area,
        );

        // Dialog box with themed styling
        let block = dialog_block("Settings", false);
        let inner = block.inner(dialog_area);
        frame.render_widget(block, dialog_area);

        // Layout - simpler structure
        let chunks = Layout::vertical([
            Constraint::Length(1), // Title/spacer
            Constraint::Length(1), // Option 1
            Constraint::Length(1), // Option 2
            Constraint::Length(1), // Spacer
            Constraint::Length(1), // Hint
        ])
        .split(inner);

        // Section title
        let title = Paragraph::new("Data Management")
            .style(Style::default().fg(theme::WARNING).add_modifier(Modifier::BOLD))
            .alignment(Alignment::Center);
        frame.render_widget(title, chunks[0]);

        // Render options
        for (i, option) in self.options.iter().enumerate() {
            let is_selected = i == self.selected_index;
            let prefix = if is_selected { "" } else { "   " };

            let style = if is_selected {
                Style::default().fg(theme::ERROR).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(theme::TEXT_PRIMARY)
            };

            let line = Paragraph::new(Line::from(vec![
                Span::styled(prefix, style),
                Span::styled(option.label(), style),
            ]));
            frame.render_widget(line, chunks[1 + i]);
        }

        // Hint
        let hint = Paragraph::new("↑/↓ navigate • Enter select • Esc close")
            .style(hint_style())
            .alignment(Alignment::Center);
        frame.render_widget(hint, chunks[4]);
    }

    /// Renders the confirmation state.
    fn render_confirmation(&self, frame: &mut Frame) {
        let area = frame.area();

        // Dialog dimensions - taller to fit multi-line messages
        let dialog_width = 55.min(area.width.saturating_sub(4));
        let dialog_height = 11.min(area.height.saturating_sub(4));
        let dialog_area = centered_rect(dialog_width, dialog_height, area);

        // Render dimmed background
        frame.render_widget(Clear, area);
        frame.render_widget(
            Paragraph::new("").style(Style::default().bg(theme::BG_DARK)),
            area,
        );

        // Dialog box with destructive styling
        let block = dialog_block("⚠ Confirm", true);
        let inner = block.inner(dialog_area);
        frame.render_widget(block, dialog_area);

        // Layout with fixed heights
        let chunks = Layout::vertical([
            Constraint::Length(1), // Question
            Constraint::Length(1), // Spacer
            Constraint::Length(3), // Warning message
            Constraint::Length(1), // Spacer
            Constraint::Length(1), // Buttons
            Constraint::Length(1), // Hint
        ])
        .split(inner);

        // Question (first line, bold)
        let question = match self.confirming_option {
            Some(SettingsOption::DeleteCompletedTasks) => "Delete all completed tasks?",
            Some(SettingsOption::ResetDatabase) => "Reset entire database?",
            None => "",
        };
        let question_paragraph = Paragraph::new(question)
            .style(Style::default().fg(theme::TEXT_PRIMARY).add_modifier(Modifier::BOLD))
            .alignment(Alignment::Center);
        frame.render_widget(question_paragraph, chunks[0]);

        // Warning details
        let warning = match self.confirming_option {
            Some(SettingsOption::DeleteCompletedTasks) => {
                vec![
                    Line::from(Span::styled("All completed tasks will be permanently removed.", Style::default().fg(theme::WARNING))),
                    Line::from(""),
                    Line::from(Span::styled("This action cannot be undone.", Style::default().fg(theme::ERROR))),
                ]
            }
            Some(SettingsOption::ResetDatabase) => {
                vec![
                    Line::from(Span::styled("All tasks, projects, and tags will be deleted.", Style::default().fg(theme::WARNING))),
                    Line::from(Span::styled("Only the Inbox project will remain.", Style::default().fg(theme::WARNING))),
                    Line::from(Span::styled("This action cannot be undone.", Style::default().fg(theme::ERROR))),
                ]
            }
            None => vec![],
        };
        let warning_paragraph = Paragraph::new(warning).alignment(Alignment::Center);
        frame.render_widget(warning_paragraph, chunks[2]);

        // Buttons
        let yes_style = if self.confirm_selected_yes {
            button_danger_style()
        } else {
            hint_style()
        };

        let no_style = if !self.confirm_selected_yes {
            button_focused_style()
        } else {
            hint_style()
        };

        let buttons = Line::from(vec![
            Span::styled(" Delete ", yes_style),
            Span::raw("    "),
            Span::styled(" Cancel ", no_style),
        ]);

        let button_paragraph = Paragraph::new(buttons).alignment(Alignment::Center);
        frame.render_widget(button_paragraph, chunks[4]);

        // Hint
        let hint = Paragraph::new("y/n or ←/→ select • Enter confirm")
            .style(hint_style())
            .alignment(Alignment::Center);
        frame.render_widget(hint, chunks[5]);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::KeyModifiers;

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    #[test]
    fn test_new_dialog() {
        let dialog = SettingsDialog::new();
        assert_eq!(dialog.selected_index, 0);
        assert_eq!(dialog.state, DialogState::Menu);
    }

    #[test]
    fn test_navigation() {
        let mut dialog = SettingsDialog::new();
        assert_eq!(dialog.selected_index, 0);

        dialog.handle_key(key(KeyCode::Down));
        assert_eq!(dialog.selected_index, 1);

        dialog.handle_key(key(KeyCode::Up));
        assert_eq!(dialog.selected_index, 0);
    }

    #[test]
    fn test_enter_confirmation() {
        let mut dialog = SettingsDialog::new();

        // Press enter on first option
        let action = dialog.handle_key(key(KeyCode::Enter));
        assert_eq!(action, DialogAction::None);
        assert_eq!(dialog.state, DialogState::Confirming);
    }

    #[test]
    fn test_cancel_confirmation() {
        let mut dialog = SettingsDialog::new();

        // Enter confirmation
        dialog.handle_key(key(KeyCode::Enter));
        assert_eq!(dialog.state, DialogState::Confirming);

        // Press 'n' to cancel
        let action = dialog.handle_key(key(KeyCode::Char('n')));
        assert_eq!(action, DialogAction::None);
        assert_eq!(dialog.state, DialogState::Menu);
    }

    #[test]
    fn test_confirm_with_y() {
        let mut dialog = SettingsDialog::new();

        // Enter confirmation
        dialog.handle_key(key(KeyCode::Enter));

        // Press 'y' to confirm
        let action = dialog.handle_key(key(KeyCode::Char('y')));
        assert_eq!(action, DialogAction::Submit);
    }

    #[test]
    fn test_escape_closes() {
        let mut dialog = SettingsDialog::new();
        let action = dialog.handle_key(key(KeyCode::Esc));
        assert_eq!(action, DialogAction::Cancel);
    }
}