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
//! Date picker calendar widget.
//!
//! A visual calendar widget for selecting dates using keyboard navigation.

use chrono::{Datelike, Duration, NaiveDate, Utc};
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    layout::{Alignment, Constraint, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Paragraph},
    Frame,
};

/// A calendar-based date picker widget.
#[derive(Debug, Clone)]
pub struct DatePicker {
    /// Currently selected date
    selected: NaiveDate,
    /// Currently viewed month (for navigation)
    view_month: NaiveDate,
}

impl DatePicker {
    /// Creates a new date picker with today's date selected.
    pub fn new() -> Self {
        let today = Utc::now().date_naive();
        Self {
            selected: today,
            view_month: today,
        }
    }

    /// Creates a date picker with a specific date selected.
    pub fn with_date(date: NaiveDate) -> Self {
        Self {
            selected: date,
            view_month: date,
        }
    }

    /// Returns the currently selected date.
    pub fn selected(&self) -> NaiveDate {
        self.selected
    }

    /// Sets the selected date.
    pub fn set_date(&mut self, date: NaiveDate) {
        self.selected = date;
        self.view_month = date;
    }

    /// Handles a key event and returns true if the picker should close.
    pub fn handle_key(&mut self, key: KeyEvent) -> DatePickerAction {
        match key.code {
            // Navigation
            KeyCode::Left | KeyCode::Char('h') => {
                self.selected -= Duration::days(1);
                self.ensure_view_contains_selected();
                DatePickerAction::None
            }
            KeyCode::Right | KeyCode::Char('l') => {
                self.selected += Duration::days(1);
                self.ensure_view_contains_selected();
                DatePickerAction::None
            }
            KeyCode::Up | KeyCode::Char('k') => {
                self.selected -= Duration::days(7);
                self.ensure_view_contains_selected();
                DatePickerAction::None
            }
            KeyCode::Down | KeyCode::Char('j') => {
                self.selected += Duration::days(7);
                self.ensure_view_contains_selected();
                DatePickerAction::None
            }

            // Month navigation
            KeyCode::PageUp | KeyCode::Char('H') => {
                self.prev_month();
                DatePickerAction::None
            }
            KeyCode::PageDown | KeyCode::Char('L') => {
                self.next_month();
                DatePickerAction::None
            }

            // Quick jumps
            KeyCode::Char('t') => {
                self.selected = Utc::now().date_naive();
                self.ensure_view_contains_selected();
                DatePickerAction::None
            }

            // Confirm/Cancel
            KeyCode::Enter => DatePickerAction::Select,
            KeyCode::Esc | KeyCode::Char('q') => DatePickerAction::Cancel,

            _ => DatePickerAction::None,
        }
    }

    /// Move view to previous month.
    fn prev_month(&mut self) {
        let year = self.view_month.year();
        let month = self.view_month.month();
        self.view_month = if month == 1 {
            NaiveDate::from_ymd_opt(year - 1, 12, 1).unwrap()
        } else {
            NaiveDate::from_ymd_opt(year, month - 1, 1).unwrap()
        };
        // Also move selected to be in the visible month
        self.selected = NaiveDate::from_ymd_opt(
            self.view_month.year(),
            self.view_month.month(),
            self.selected.day().min(days_in_month(self.view_month)),
        )
        .unwrap();
    }

    /// Move view to next month.
    fn next_month(&mut self) {
        let year = self.view_month.year();
        let month = self.view_month.month();
        self.view_month = if month == 12 {
            NaiveDate::from_ymd_opt(year + 1, 1, 1).unwrap()
        } else {
            NaiveDate::from_ymd_opt(year, month + 1, 1).unwrap()
        };
        // Also move selected to be in the visible month
        self.selected = NaiveDate::from_ymd_opt(
            self.view_month.year(),
            self.view_month.month(),
            self.selected.day().min(days_in_month(self.view_month)),
        )
        .unwrap();
    }

    /// Ensure the view month contains the selected date.
    fn ensure_view_contains_selected(&mut self) {
        if self.selected.year() != self.view_month.year()
            || self.selected.month() != self.view_month.month()
        {
            self.view_month = NaiveDate::from_ymd_opt(
                self.selected.year(),
                self.selected.month(),
                1,
            )
            .unwrap();
        }
    }

    /// Renders the date picker as a popup.
    pub fn render(&self, frame: &mut Frame) {
        let area = frame.area();

        // Calculate popup size and position (bigger for better readability)
        let popup_width = 32;
        let popup_height = 15;
        let popup_area = centered_rect(popup_width, popup_height, area);

        // Clear background
        frame.render_widget(Clear, area);
        let dim = Block::default().style(Style::default().bg(Color::Black));
        frame.render_widget(dim, area);

        // Draw popup border
        let block = Block::default()
            .title(" Select Date ")
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::Cyan))
            .style(Style::default().bg(Color::Black));

        let inner = block.inner(popup_area);
        frame.render_widget(block, popup_area);

        // Layout
        let chunks = Layout::vertical([
            Constraint::Length(2), // Month/Year header with spacing
            Constraint::Length(1), // Day names
            Constraint::Length(1), // Spacer
            Constraint::Min(6),    // Calendar grid
            Constraint::Length(1), // Spacer
            Constraint::Length(1), // Help text
        ])
        .split(inner);

        // Month/Year header with navigation hints
        let month_name = month_name(self.view_month.month());
        let header = format!(
            "{} {}",
            month_name,
            self.view_month.year()
        );
        let header_para = Paragraph::new(header)
            .style(Style::default().fg(Color::White).add_modifier(Modifier::BOLD))
            .alignment(Alignment::Center);
        frame.render_widget(header_para, chunks[0]);

        // Day names (Monday to Sunday)
        let day_names = " Mo  Tu  We  Th  Fr  Sa  Su";
        let day_names_para = Paragraph::new(day_names)
            .style(Style::default().fg(Color::DarkGray).add_modifier(Modifier::BOLD))
            .alignment(Alignment::Center);
        frame.render_widget(day_names_para, chunks[1]);

        // Calendar grid
        let grid = self.build_calendar_grid();
        let grid_para = Paragraph::new(grid).alignment(Alignment::Center);
        frame.render_widget(grid_para, chunks[3]);

        // Help text
        let help = "←↓↑→:nav  PgUp/Dn:month  t:today  Enter:ok";
        let help_para = Paragraph::new(help)
            .style(Style::default().fg(Color::DarkGray))
            .alignment(Alignment::Center);
        frame.render_widget(help_para, chunks[5]);
    }

    /// Builds the calendar grid as styled lines (Monday to Sunday).
    fn build_calendar_grid(&self) -> Vec<Line<'static>> {
        let today = Utc::now().date_naive();
        let first_of_month = NaiveDate::from_ymd_opt(
            self.view_month.year(),
            self.view_month.month(),
            1,
        )
        .unwrap();
        let days_in_month = days_in_month(first_of_month);
        // Use Monday as first day of week (0 = Monday, 6 = Sunday)
        let start_weekday = first_of_month.weekday().num_days_from_monday() as usize;

        let mut lines = Vec::new();
        let mut current_line: Vec<Span> = Vec::new();

        // Add empty spaces for days before the 1st
        for _ in 0..start_weekday {
            current_line.push(Span::raw("    "));
        }

        for day in 1..=days_in_month {
            let date = NaiveDate::from_ymd_opt(
                self.view_month.year(),
                self.view_month.month(),
                day,
            )
            .unwrap();

            let is_selected = date == self.selected;
            let is_today = date == today;
            // Weekend check: Saturday = 5, Sunday = 6 (from Monday)
            let is_weekend = date.weekday().num_days_from_monday() >= 5;

            let style = if is_selected {
                Style::default()
                    .fg(Color::Black)
                    .bg(Color::Yellow)
                    .add_modifier(Modifier::BOLD)
            } else if is_today {
                Style::default()
                    .fg(Color::Cyan)
                    .add_modifier(Modifier::BOLD)
            } else if is_weekend {
                Style::default().fg(Color::DarkGray)
            } else {
                Style::default().fg(Color::White)
            };

            current_line.push(Span::styled(format!(" {:2} ", day), style));

            // If we've reached Sunday (7 items), start a new line
            if current_line.len() == 7 {
                lines.push(Line::from(current_line));
                current_line = Vec::new();
            }
        }

        // Add remaining days if any
        if !current_line.is_empty() {
            while current_line.len() < 7 {
                current_line.push(Span::raw("    "));
            }
            lines.push(Line::from(current_line));
        }

        // Pad to 6 lines for consistent height
        while lines.len() < 6 {
            lines.push(Line::from(""));
        }

        lines
    }
}

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

/// Result of handling a key in the date picker.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DatePickerAction {
    /// No action, continue showing picker
    None,
    /// User selected a date
    Select,
    /// User cancelled
    Cancel,
}

/// Returns the number of days in the month containing the given date.
fn days_in_month(date: NaiveDate) -> u32 {
    let year = date.year();
    let month = date.month();
    if month == 12 {
        NaiveDate::from_ymd_opt(year + 1, 1, 1).unwrap()
    } else {
        NaiveDate::from_ymd_opt(year, month + 1, 1).unwrap()
    }
    .signed_duration_since(NaiveDate::from_ymd_opt(year, month, 1).unwrap())
    .num_days() as u32
}

/// Returns the name of a month (1-12).
fn month_name(month: u32) -> &'static str {
    match month {
        1 => "January",
        2 => "February",
        3 => "March",
        4 => "April",
        5 => "May",
        6 => "June",
        7 => "July",
        8 => "August",
        9 => "September",
        10 => "October",
        11 => "November",
        12 => "December",
        _ => "Unknown",
    }
}

/// Centers a rect within another rect.
fn centered_rect(width: u16, height: u16, area: Rect) -> Rect {
    let x = area.x + (area.width.saturating_sub(width)) / 2;
    let y = area.y + (area.height.saturating_sub(height)) / 2;
    Rect::new(x, y, width.min(area.width), height.min(area.height))
}

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

    #[test]
    fn test_new_picker() {
        let picker = DatePicker::new();
        assert_eq!(picker.selected(), Utc::now().date_naive());
    }

    #[test]
    fn test_navigation_right() {
        let mut picker = DatePicker::with_date(NaiveDate::from_ymd_opt(2025, 1, 15).unwrap());
        picker.handle_key(KeyEvent::new(KeyCode::Right, KeyModifiers::NONE));
        assert_eq!(picker.selected(), NaiveDate::from_ymd_opt(2025, 1, 16).unwrap());
    }

    #[test]
    fn test_navigation_down() {
        let mut picker = DatePicker::with_date(NaiveDate::from_ymd_opt(2025, 1, 15).unwrap());
        picker.handle_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
        assert_eq!(picker.selected(), NaiveDate::from_ymd_opt(2025, 1, 22).unwrap());
    }

    #[test]
    fn test_month_navigation() {
        let mut picker = DatePicker::with_date(NaiveDate::from_ymd_opt(2025, 1, 15).unwrap());
        picker.handle_key(KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE));
        assert_eq!(picker.view_month.month(), 2);
    }

    #[test]
    fn test_enter_selects() {
        let mut picker = DatePicker::new();
        let action = picker.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
        assert_eq!(action, DatePickerAction::Select);
    }

    #[test]
    fn test_escape_cancels() {
        let mut picker = DatePicker::new();
        let action = picker.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
        assert_eq!(action, DatePickerAction::Cancel);
    }

    #[test]
    fn test_days_in_month() {
        assert_eq!(days_in_month(NaiveDate::from_ymd_opt(2025, 1, 1).unwrap()), 31);
        assert_eq!(days_in_month(NaiveDate::from_ymd_opt(2025, 2, 1).unwrap()), 28);
        assert_eq!(days_in_month(NaiveDate::from_ymd_opt(2024, 2, 1).unwrap()), 29); // Leap year
        assert_eq!(days_in_month(NaiveDate::from_ymd_opt(2025, 4, 1).unwrap()), 30);
    }
}