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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Tag input widget with autocomplete.
//!
//! This module provides a [`TagInput`] widget for entering tags with
//! autocomplete suggestions based on existing tags in the database.
//!
//! ## Features
//!
//! - Comma or Enter to add a tag
//! - Backspace on empty input removes the last tag
//! - Tab to accept autocomplete suggestion
//! - Shows existing tags as chips/badges
//!
//! ## Example
//!
//! ```rust,no_run
//! use ratado::ui::tag_input::TagInput;
//!
//! let mut input = TagInput::new();
//! input.add_tag("work".to_string());
//! input.add_tag("urgent".to_string());
//! assert_eq!(input.tags(), &["work", "urgent"]);
//! ```

use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Widget},
};

use crate::storage::Tag;
use crate::ui::input::TextInput;

/// Default placeholder text for tag input.
const DEFAULT_PLACEHOLDER: &str = "Type tag, press Enter to add";

/// A tag input widget with autocomplete support.
///
/// `TagInput` provides a multi-value input field for tags:
/// - Text input for typing new tags
/// - Autocomplete suggestions from existing tags
/// - Visual display of selected tags as chips
/// - Easy removal of tags via backspace
#[derive(Debug, Clone)]
pub struct TagInput {
    /// The text input for typing new tags
    input: TextInput,
    /// Currently selected tags
    tags: Vec<String>,
    /// Autocomplete suggestions based on current input
    suggestions: Vec<String>,
    /// Index of selected suggestion (if any)
    selected_suggestion: Option<usize>,
    /// Placeholder text shown when empty
    placeholder: String,
}

impl TagInput {
    /// Creates a new empty tag input.
    pub fn new() -> Self {
        Self {
            input: TextInput::new(),
            tags: Vec::new(),
            suggestions: Vec::new(),
            selected_suggestion: None,
            placeholder: DEFAULT_PLACEHOLDER.to_string(),
        }
    }

    /// Creates a tag input with initial tags.
    pub fn with_tags(tags: Vec<String>) -> Self {
        Self {
            input: TextInput::new(),
            tags,
            suggestions: Vec::new(),
            selected_suggestion: None,
            placeholder: DEFAULT_PLACEHOLDER.to_string(),
        }
    }

    /// Returns the current list of tags.
    pub fn tags(&self) -> &[String] {
        &self.tags
    }

    /// Returns the tags as a cloned vector.
    pub fn tags_vec(&self) -> Vec<String> {
        self.tags.clone()
    }

    /// Adds a tag if it's not already present.
    pub fn add_tag(&mut self, tag: String) {
        let tag = tag.trim().to_string();
        if !tag.is_empty() && !self.tags.iter().any(|t| t.eq_ignore_ascii_case(&tag)) {
            self.tags.push(tag);
        }
    }

    /// Removes the last tag.
    pub fn remove_last_tag(&mut self) {
        self.tags.pop();
    }

    /// Removes a tag by name.
    pub fn remove_tag(&mut self, tag: &str) {
        self.tags.retain(|t| t != tag);
    }

    /// Returns the current input value.
    pub fn input_value(&self) -> &str {
        self.input.value()
    }

    /// Returns the currently selected suggestion, if any.
    pub fn selected_suggestion(&self) -> Option<&String> {
        self.selected_suggestion
            .and_then(|idx| self.suggestions.get(idx))
    }

    /// Updates autocomplete suggestions based on current input and available tags.
    pub fn update_suggestions(&mut self, all_tags: &[Tag]) {
        let query = self.input.value().to_lowercase();

        if query.is_empty() {
            self.suggestions.clear();
            self.selected_suggestion = None;
            return;
        }

        self.suggestions = all_tags
            .iter()
            .filter(|t| t.name.to_lowercase().contains(&query))
            .filter(|t| !self.tags.iter().any(|existing| existing.eq_ignore_ascii_case(&t.name)))
            .map(|t| t.name.clone())
            .take(5)
            .collect();

        self.selected_suggestion = if self.suggestions.is_empty() {
            None
        } else {
            Some(0)
        };
    }

    /// Handles a key event and updates state accordingly.
    ///
    /// Returns `true` if the event was consumed.
    pub fn handle_key(&mut self, key: KeyEvent, all_tags: &[Tag]) -> bool {
        match key.code {
            // Comma or Enter adds the current input as a tag
            KeyCode::Char(',') | KeyCode::Enter => {
                let tag = self.input.value().trim().to_string();
                if !tag.is_empty() {
                    self.add_tag(tag);
                    self.input.clear();
                    self.suggestions.clear();
                    self.selected_suggestion = None;
                }
                true
            }

            // Backspace on empty input removes the last tag
            KeyCode::Backspace if self.input.value().is_empty() => {
                self.remove_last_tag();
                true
            }

            // Regular backspace
            KeyCode::Backspace => {
                self.input.delete_backward();
                self.update_suggestions(all_tags);
                true
            }

            // Tab accepts the selected suggestion
            KeyCode::Tab => {
                if let Some(suggestion) = self.selected_suggestion() {
                    let tag = suggestion.clone();
                    self.add_tag(tag);
                    self.input.clear();
                    self.suggestions.clear();
                    self.selected_suggestion = None;
                    return true;
                }
                false // Allow tab to move to next field if no suggestion
            }

            // Up/Down navigate suggestions
            KeyCode::Up => {
                if !self.suggestions.is_empty() {
                    self.selected_suggestion = Some(
                        self.selected_suggestion
                            .map(|i| if i == 0 { self.suggestions.len() - 1 } else { i - 1 })
                            .unwrap_or(0),
                    );
                    true
                } else {
                    false
                }
            }

            KeyCode::Down => {
                if !self.suggestions.is_empty() {
                    self.selected_suggestion = Some(
                        self.selected_suggestion
                            .map(|i| (i + 1) % self.suggestions.len())
                            .unwrap_or(0),
                    );
                    true
                } else {
                    false
                }
            }

            // Regular character input
            KeyCode::Char(c) => {
                self.input.insert(c);
                self.update_suggestions(all_tags);
                true
            }

            // Delete key
            KeyCode::Delete => {
                self.input.delete_forward();
                self.update_suggestions(all_tags);
                true
            }

            // Cursor movement
            KeyCode::Left => {
                self.input.move_left();
                true
            }
            KeyCode::Right => {
                self.input.move_right();
                true
            }
            KeyCode::Home => {
                self.input.move_home();
                true
            }
            KeyCode::End => {
                self.input.move_end();
                true
            }

            _ => false,
        }
    }

    /// Renders the tag input to a buffer.
    ///
    /// # Arguments
    ///
    /// * `area` - The area to render into
    /// * `buf` - The buffer to render to
    /// * `focused` - Whether this input is currently focused
    /// * `label` - Optional label to show in the border
    pub fn render_to_buffer(
        &self,
        area: Rect,
        buf: &mut Buffer,
        focused: bool,
        label: Option<&str>,
    ) {
        let border_style = if focused {
            Style::default().fg(Color::Yellow)
        } else {
            Style::default().fg(Color::DarkGray)
        };

        let mut block = Block::default()
            .borders(Borders::ALL)
            .border_style(border_style);

        if let Some(label) = label {
            block = block.title(format!(" {} ", label));
        }

        let inner = block.inner(area);
        block.render(area, buf);

        // Build the content line with tags and input
        let mut spans: Vec<Span> = Vec::new();

        // Render existing tags as chips
        for tag in &self.tags {
            spans.push(Span::styled(
                format!(" #{} ", tag),
                Style::default()
                    .fg(Color::White)
                    .bg(Color::Magenta)
                    .add_modifier(Modifier::BOLD),
            ));
            spans.push(Span::raw(" "));
        }

        // Render the input text or placeholder
        let input_text = if self.input.value().is_empty() && self.tags.is_empty() {
            Span::styled(&self.placeholder, Style::default().fg(Color::DarkGray))
        } else if self.input.value().is_empty() {
            Span::raw("")
        } else {
            Span::styled(
                self.input.value().to_string(),
                if focused {
                    Style::default().fg(Color::Yellow)
                } else {
                    Style::default().fg(Color::Gray)
                },
            )
        };
        spans.push(input_text);

        let line = Line::from(spans);
        let paragraph = Paragraph::new(line);
        paragraph.render(inner, buf);

        // Render cursor if focused and there's room
        if focused && inner.width > 0 {
            // Calculate cursor position (after all tags)
            let tags_width: usize = self.tags.iter().map(|t| t.len() + 4).sum(); // " #tag "
            let cursor_x = inner.x + tags_width as u16 + self.input.cursor() as u16;

            if cursor_x < inner.x + inner.width {
                let cursor_char = self.input.value().chars().nth(self.input.cursor()).unwrap_or(' ');
                buf[(cursor_x, inner.y)]
                    .set_char(cursor_char)
                    .set_style(Style::default().bg(Color::Yellow).fg(Color::Black));
            }
        }
    }

    /// Renders autocomplete suggestions below the input area.
    pub fn render_suggestions(&self, frame: &mut ratatui::Frame, area: Rect) {
        if self.suggestions.is_empty() {
            return;
        }

        let suggestion_height = self.suggestions.len() as u16 + 2; // +2 for borders
        let suggestion_area = Rect::new(
            area.x,
            area.y + area.height,
            area.width.min(30),
            suggestion_height.min(7),
        );

        // Check if we have room below
        let frame_area = frame.area();
        if suggestion_area.y + suggestion_area.height > frame_area.height {
            return;
        }

        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::Cyan))
            .style(Style::default().bg(Color::Black));

        let inner = block.inner(suggestion_area);

        // Render block background
        frame.render_widget(ratatui::widgets::Clear, suggestion_area);
        frame.render_widget(block, suggestion_area);

        // Render suggestions
        let mut y = inner.y;
        for (i, suggestion) in self.suggestions.iter().enumerate() {
            let style = if Some(i) == self.selected_suggestion {
                Style::default()
                    .fg(Color::Black)
                    .bg(Color::Cyan)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Gray)
            };

            let text = format!(" #{}", suggestion);
            let paragraph = Paragraph::new(text).style(style);
            let line_area = Rect::new(inner.x, y, inner.width, 1);
            frame.render_widget(paragraph, line_area);
            y += 1;
        }
    }
}

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

/// A renderable version of TagInput for use as a Widget.
pub struct TagInputWidget<'a> {
    input: &'a TagInput,
    focused: bool,
    label: Option<&'a str>,
}

impl<'a> TagInputWidget<'a> {
    /// Creates a new tag input widget.
    pub fn new(input: &'a TagInput) -> Self {
        Self {
            input,
            focused: false,
            label: None,
        }
    }

    /// Sets whether the input is focused.
    pub fn focused(mut self, focused: bool) -> Self {
        self.focused = focused;
        self
    }

    /// Sets the label shown in the border.
    pub fn label(mut self, label: &'a str) -> Self {
        self.label = Some(label);
        self
    }
}

impl Widget for TagInputWidget<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        self.input
            .render_to_buffer(area, buf, self.focused, self.label);
    }
}

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

    #[test]
    fn test_new_input() {
        let input = TagInput::new();
        assert!(input.tags().is_empty());
        assert!(input.input_value().is_empty());
    }

    #[test]
    fn test_add_tag() {
        let mut input = TagInput::new();
        input.add_tag("work".to_string());
        input.add_tag("urgent".to_string());
        assert_eq!(input.tags(), &["work", "urgent"]);
    }

    #[test]
    fn test_add_duplicate_tag() {
        let mut input = TagInput::new();
        input.add_tag("work".to_string());
        input.add_tag("Work".to_string()); // Same tag, different case
        assert_eq!(input.tags().len(), 1);
    }

    #[test]
    fn test_add_empty_tag() {
        let mut input = TagInput::new();
        input.add_tag("".to_string());
        input.add_tag("   ".to_string());
        assert!(input.tags().is_empty());
    }

    #[test]
    fn test_remove_last_tag() {
        let mut input = TagInput::with_tags(vec!["a".to_string(), "b".to_string()]);
        input.remove_last_tag();
        assert_eq!(input.tags(), &["a"]);
    }

    #[test]
    fn test_remove_tag_by_name() {
        let mut input = TagInput::with_tags(vec!["a".to_string(), "b".to_string(), "c".to_string()]);
        input.remove_tag("b");
        assert_eq!(input.tags(), &["a", "c"]);
    }

    #[test]
    fn test_update_suggestions() {
        let all_tags = vec![
            Tag { id: "1".to_string(), name: "work".to_string() },
            Tag { id: "2".to_string(), name: "workout".to_string() },
            Tag { id: "3".to_string(), name: "personal".to_string() },
        ];

        let mut input = TagInput::new();
        input.input.set_value("wor");
        input.update_suggestions(&all_tags);

        assert_eq!(input.suggestions.len(), 2);
        assert!(input.suggestions.contains(&"work".to_string()));
        assert!(input.suggestions.contains(&"workout".to_string()));
    }

    #[test]
    fn test_suggestions_exclude_already_selected() {
        let all_tags = vec![
            Tag { id: "1".to_string(), name: "work".to_string() },
            Tag { id: "2".to_string(), name: "workout".to_string() },
        ];

        let mut input = TagInput::with_tags(vec!["work".to_string()]);
        input.input.set_value("wor");
        input.update_suggestions(&all_tags);

        assert_eq!(input.suggestions.len(), 1);
        assert!(input.suggestions.contains(&"workout".to_string()));
    }
}