rustodo 2.26.0

A modern, powerful task manager built with Rust
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//! Application state for the TUI.

use crate::models::{Priority, Project, Recurrence, StatusFilter, Task};
use crate::storage::Storage;
use anyhow::Result;

// ── Mode ──────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq)]
pub enum Mode {
    Normal,
    ConfirmDelete,
    ConfirmClearAll,
    Search,
    EditForm,
    AddForm,
    Help,
}

// ── FocusedPanel ──────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Copy)]
pub enum FocusedPanel {
    Left,
    Right,
}

impl FocusedPanel {
    pub fn toggle(self) -> Self {
        match self {
            FocusedPanel::Left => FocusedPanel::Right,
            FocusedPanel::Right => FocusedPanel::Left,
        }
    }
}

// ── LeftPanel ─────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Copy)]
pub enum LeftPanel {
    Tasks,
    Projects,
    Tags,
}

impl LeftPanel {
    pub fn next(self) -> Self {
        match self {
            LeftPanel::Tasks => LeftPanel::Projects,
            LeftPanel::Projects => LeftPanel::Tags,
            LeftPanel::Tags => LeftPanel::Tasks,
        }
    }
    pub fn prev(self) -> Self {
        match self {
            LeftPanel::Tasks => LeftPanel::Tags,
            LeftPanel::Projects => LeftPanel::Tasks,
            LeftPanel::Tags => LeftPanel::Projects,
        }
    }
    pub fn label(self) -> &'static str {
        match self {
            LeftPanel::Tasks => "Tasks",
            LeftPanel::Projects => "Projects",
            LeftPanel::Tags => "Tags",
        }
    }
}

// ── RightPanel ────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Copy)]
pub enum RightPanel {
    Details,
    Stats,
    Deps,
}

impl RightPanel {
    pub fn next(self) -> Self {
        match self {
            RightPanel::Details => RightPanel::Stats,
            RightPanel::Stats => RightPanel::Deps,
            RightPanel::Deps => RightPanel::Details,
        }
    }
    pub fn prev(self) -> Self {
        match self {
            RightPanel::Details => RightPanel::Deps,
            RightPanel::Stats => RightPanel::Details,
            RightPanel::Deps => RightPanel::Stats,
        }
    }
    pub fn label(self) -> &'static str {
        match self {
            RightPanel::Details => "Details",
            RightPanel::Stats => "Stats",
            RightPanel::Deps => "Deps",
        }
    }
}

// ── EditField ─────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Copy)]
pub enum EditField {
    Text,
    Priority,
    Due,
    Recurrence,
    Project,
    Tags,
    Deps,
}

impl EditField {
    pub fn next(self) -> Self {
        match self {
            EditField::Text => EditField::Priority,
            EditField::Priority => EditField::Due,
            EditField::Due => EditField::Recurrence,
            EditField::Recurrence => EditField::Project,
            EditField::Project => EditField::Tags,
            EditField::Tags => EditField::Deps,
            EditField::Deps => EditField::Text,
        }
    }
    pub fn prev(self) -> Self {
        match self {
            EditField::Text => EditField::Deps,
            EditField::Priority => EditField::Text,
            EditField::Due => EditField::Priority,
            EditField::Recurrence => EditField::Due,
            EditField::Project => EditField::Recurrence,
            EditField::Tags => EditField::Project,
            EditField::Deps => EditField::Tags,
        }
    }
    pub fn label(self) -> &'static str {
        match self {
            EditField::Text => "Text",
            EditField::Priority => "Priority",
            EditField::Due => "Due",
            EditField::Recurrence => "Recurrence",
            EditField::Project => "Project",
            EditField::Tags => "Tags",
            EditField::Deps => "Deps (IDs)",
        }
    }
}

// ── EditFormState ─────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct EditFormState {
    pub focused: EditField,
    pub text: String,
    pub priority: Priority,
    pub due: String,
    pub recurrence: Option<Recurrence>,
    pub project: String,
    pub tags: String,
    pub deps: String,
}

impl EditFormState {
    pub fn blank() -> Self {
        Self {
            focused: EditField::Text,
            text: String::new(),
            priority: Priority::Medium,
            due: String::new(),
            recurrence: None,
            project: String::new(),
            tags: String::new(),
            deps: String::new(),
        }
    }

    pub fn from_task(task: &Task, all_tasks: &[Task], projects: &[Project]) -> Self {
        let visible: Vec<&Task> = all_tasks.iter().filter(|t| !t.is_deleted()).collect();
        let deps = task
            .depends_on
            .iter()
            .filter_map(|uuid| {
                let pos = visible.iter().position(|t| t.uuid == *uuid)?;
                Some((pos + 1).to_string())
            })
            .collect::<Vec<_>>()
            .join(", ");

        // Resolve project_id → project name for the form field
        let project_name = task
            .project_id
            .and_then(|pid| projects.iter().find(|p| p.uuid == pid && !p.is_deleted()))
            .map(|p| p.name.clone())
            .unwrap_or_default();

        Self {
            focused: EditField::Text,
            text: task.text.clone(),
            priority: task.priority,
            due: task
                .due_date
                .map(|d| d.format("%Y-%m-%d").to_string())
                .unwrap_or_default(),
            recurrence: task.recurrence,
            project: project_name,
            tags: task.tags.join(", "),
            deps,
        }
    }

    pub fn focused_buf_mut(&mut self) -> Option<&mut String> {
        match self.focused {
            EditField::Text => Some(&mut self.text),
            EditField::Due => Some(&mut self.due),
            EditField::Project => Some(&mut self.project),
            EditField::Tags => Some(&mut self.tags),
            EditField::Deps => Some(&mut self.deps),
            EditField::Priority | EditField::Recurrence => None,
        }
    }

    pub fn priority_prev(&mut self) {
        self.priority = match self.priority {
            Priority::High => Priority::Low,
            Priority::Medium => Priority::High,
            Priority::Low => Priority::Medium,
        };
    }
    pub fn priority_next(&mut self) {
        self.priority = match self.priority {
            Priority::High => Priority::Medium,
            Priority::Medium => Priority::Low,
            Priority::Low => Priority::High,
        };
    }
    pub fn recurrence_next(&mut self) {
        self.recurrence = match self.recurrence {
            None => Some(Recurrence::Daily),
            Some(Recurrence::Daily) => Some(Recurrence::Weekly),
            Some(Recurrence::Weekly) => Some(Recurrence::Monthly),
            Some(Recurrence::Monthly) => None,
        };
    }
    pub fn recurrence_prev(&mut self) {
        self.recurrence = match self.recurrence {
            None => Some(Recurrence::Monthly),
            Some(Recurrence::Daily) => None,
            Some(Recurrence::Weekly) => Some(Recurrence::Daily),
            Some(Recurrence::Monthly) => Some(Recurrence::Weekly),
        };
    }
    pub fn recurrence_label(&self) -> &'static str {
        match self.recurrence {
            None => "None",
            Some(Recurrence::Daily) => "Daily",
            Some(Recurrence::Weekly) => "Weekly",
            Some(Recurrence::Monthly) => "Monthly",
        }
    }
}

// ── ListFilter ────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Copy)]
pub enum ListFilter {
    Pending,
    Done,
    All,
}

impl ListFilter {
    pub fn next(self) -> Self {
        match self {
            ListFilter::Pending => ListFilter::Done,
            ListFilter::Done => ListFilter::All,
            ListFilter::All => ListFilter::Pending,
        }
    }
    pub fn label(self) -> &'static str {
        match self {
            ListFilter::Pending => "Pending",
            ListFilter::Done => "Done",
            ListFilter::All => "All",
        }
    }
    pub fn as_status_filter(self) -> StatusFilter {
        match self {
            ListFilter::Pending => StatusFilter::Pending,
            ListFilter::Done => StatusFilter::Done,
            ListFilter::All => StatusFilter::All,
        }
    }
}

// ── PriorityFilter ────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Copy)]
pub enum PriorityFilter {
    All,
    High,
    Medium,
    Low,
}

impl PriorityFilter {
    pub fn next(self) -> Self {
        match self {
            PriorityFilter::All => PriorityFilter::High,
            PriorityFilter::High => PriorityFilter::Medium,
            PriorityFilter::Medium => PriorityFilter::Low,
            PriorityFilter::Low => PriorityFilter::All,
        }
    }
    pub fn label(self) -> &'static str {
        match self {
            PriorityFilter::All => "All",
            PriorityFilter::High => "High",
            PriorityFilter::Medium => "Med",
            PriorityFilter::Low => "Low",
        }
    }
}

// ── TreeItem ──────────────────────────────────────────────────────────────────

/// A flat navigable item in the project tree view.
#[derive(Debug, Clone)]
pub enum TreeItem {
    /// A project header (expandable).
    Project {
        name: Option<String>,
        task_count: usize,
        expanded: bool,
    },
    /// A task row nested under its project.
    Task { task_idx: usize },
}

impl TreeItem {
    pub fn is_project(&self) -> bool {
        matches!(self, TreeItem::Project { .. })
    }
    pub fn is_task(&self) -> bool {
        matches!(self, TreeItem::Task { .. })
    }
    pub fn task_idx(&self) -> Option<usize> {
        match self {
            TreeItem::Task { task_idx } => Some(*task_idx),
            _ => None,
        }
    }
    pub fn expanded(&self) -> bool {
        match self {
            TreeItem::Project { expanded, .. } => *expanded,
            _ => false,
        }
    }
}

// ── App ───────────────────────────────────────────────────────────────────────

pub struct App {
    pub tasks: Vec<Task>,
    pub projects: Vec<Project>,
    pub filtered_indices: Vec<usize>,
    pub selected: usize,
    pub mode: Mode,
    pub status_msg: Option<String>,
    pub details_scroll: usize,
    pub list_filter: ListFilter,
    pub priority_filter: PriorityFilter,
    pub input: String,
    pub edit_form: Option<EditFormState>,
    pub help_selected: usize,
    pub right_panel: RightPanel,
    pub left_panel: LeftPanel,
    pub left_selected: usize,
    pub focused_panel: FocusedPanel,
    /// Flat navigable list for the project tree.
    pub project_tree: Vec<TreeItem>,
    /// Selected row index within project_tree.
    pub tree_selected: usize,
}

impl App {
    pub fn new(storage: &impl Storage) -> Result<Self> {
        let tasks = Self::load_visible(storage)?;
        let projects = storage.load_projects()?;
        let filtered_indices = tasks
            .iter()
            .enumerate()
            .filter(|(_, t)| !t.completed)
            .map(|(i, _)| i)
            .collect();
        let mut app = Self {
            tasks,
            projects,
            filtered_indices,
            selected: 0,
            mode: Mode::Normal,
            status_msg: None,
            details_scroll: 0,
            list_filter: ListFilter::Pending,
            priority_filter: PriorityFilter::All,
            input: String::new(),
            edit_form: None,
            help_selected: 0,
            right_panel: RightPanel::Details,
            left_panel: LeftPanel::Tasks,
            left_selected: 0,
            focused_panel: FocusedPanel::Left,
            project_tree: vec![],
            tree_selected: 0,
        };
        app.build_project_tree();
        Ok(app)
    }

    pub fn reload(&mut self, storage: &impl Storage) -> Result<()> {
        self.tasks = Self::load_visible(storage)?;
        self.projects = storage.load_projects()?;
        self.refilter();
        if self.selected >= self.filtered_indices.len() {
            self.selected = self.filtered_indices.len().saturating_sub(1);
        }
        self.build_project_tree();
        Ok(())
    }

    /// Resolve project_id → project name for display purposes.
    pub fn project_name_for<'a>(&'a self, task: &Task) -> Option<&'a str> {
        let pid = task.project_id?;
        self.projects
            .iter()
            .find(|p| p.uuid == pid && !p.is_deleted())
            .map(|p| p.name.as_str())
    }

    pub fn refilter(&mut self) {
        let raw = self.input.to_lowercase();
        let status = self.list_filter.as_status_filter();

        let mut project_filter: Option<String> = None;
        let mut tag_filters: Vec<String> = Vec::new();
        let mut text_tokens: Vec<String> = Vec::new();

        if self.mode == Mode::Search && !raw.is_empty() {
            for token in raw.split_whitespace() {
                if let Some(proj) = token.strip_prefix('@') {
                    project_filter = Some(proj.to_string());
                } else if let Some(tag) = token.strip_prefix('#') {
                    tag_filters.push(tag.to_string());
                } else {
                    text_tokens.push(token.to_string());
                }
            }
        }

        self.filtered_indices = self
            .tasks
            .iter()
            .enumerate()
            .filter(|(_, t)| t.matches_status(status))
            .filter(|(_, t)| match self.priority_filter {
                PriorityFilter::All => true,
                PriorityFilter::High => t.priority == Priority::High,
                PriorityFilter::Medium => t.priority == Priority::Medium,
                PriorityFilter::Low => t.priority == Priority::Low,
            })
            .filter(|(_, t)| {
                if self.mode != Mode::Search || raw.is_empty() {
                    return true;
                }
                if let Some(ref pf) = project_filter {
                    // Resolve project name via project_id for search filtering
                    let proj_name = t
                        .project_id
                        .and_then(|pid| {
                            self.projects
                                .iter()
                                .find(|p| p.uuid == pid && !p.is_deleted())
                        })
                        .map(|p| p.name.to_lowercase());
                    match proj_name {
                        Some(ref name) if name.contains(pf.as_str()) => {}
                        _ => return false,
                    }
                }
                for tf in &tag_filters {
                    if !t
                        .tags
                        .iter()
                        .any(|tag| tag.to_lowercase().contains(tf.as_str()))
                    {
                        return false;
                    }
                }
                for token in &text_tokens {
                    if !t.text.to_lowercase().contains(token.as_str()) {
                        return false;
                    }
                }
                true
            })
            .map(|(i, _)| i)
            .collect();
    }

    // ── project tree ──────────────────────────────────────────────────────────

    /// Build (or rebuild) the flat navigable project tree.
    /// Preserves expanded/collapsed state across rebuilds.
    pub fn build_project_tree(&mut self) {
        // Preserve existing expanded states
        let prev_expanded: std::collections::HashMap<String, bool> = self
            .project_tree
            .iter()
            .filter_map(|item| match item {
                TreeItem::Project { name, expanded, .. } => {
                    Some((name.clone().unwrap_or_default(), *expanded))
                }
                _ => None,
            })
            .collect();

        // Collect project names from loaded projects (non-deleted), sorted
        let mut project_names: Vec<Option<String>> = self
            .projects
            .iter()
            .filter(|p| !p.is_deleted())
            .map(|p| Some(p.name.clone()))
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();
        project_names.sort();

        let mut tree: Vec<TreeItem> = Vec::new();
        for proj_name in &project_names {
            let key = proj_name.clone().unwrap_or_default();
            let expanded = prev_expanded.get(&key).copied().unwrap_or(true);

            // Count tasks that belong to this project
            let task_count = self
                .tasks
                .iter()
                .filter(|t| {
                    let name = t
                        .project_id
                        .and_then(|pid| {
                            self.projects
                                .iter()
                                .find(|p| p.uuid == pid && !p.is_deleted())
                        })
                        .map(|p| p.name.as_str());
                    name == proj_name.as_deref()
                })
                .count();

            tree.push(TreeItem::Project {
                name: proj_name.clone(),
                task_count,
                expanded,
            });

            if expanded {
                for (idx, task) in self.tasks.iter().enumerate() {
                    let name = task
                        .project_id
                        .and_then(|pid| {
                            self.projects
                                .iter()
                                .find(|p| p.uuid == pid && !p.is_deleted())
                        })
                        .map(|p| p.name.as_str());
                    if name == proj_name.as_deref() {
                        tree.push(TreeItem::Task { task_idx: idx });
                    }
                }
            }
        }

        self.project_tree = tree;
        self.tree_selected = self
            .tree_selected
            .min(self.project_tree.len().saturating_sub(1));
    }

    /// Toggle expand/collapse on the currently selected project header.
    pub fn tree_toggle_expand(&mut self) {
        if let Some(TreeItem::Project { expanded, .. }) =
            self.project_tree.get_mut(self.tree_selected)
        {
            *expanded = !*expanded;
            // Rebuild in place to add/remove child rows
            self.build_project_tree();
        }
    }

    pub fn tree_move_down(&mut self) {
        if !self.project_tree.is_empty() {
            self.tree_selected = (self.tree_selected + 1).min(self.project_tree.len() - 1);
        }
        self.details_scroll = 0;
    }

    pub fn tree_move_up(&mut self) {
        self.tree_selected = self.tree_selected.saturating_sub(1);
        self.details_scroll = 0;
    }

    /// The task currently selected in the tree (if a Task row is selected).
    pub fn tree_selected_task(&self) -> Option<&Task> {
        match self.project_tree.get(self.tree_selected)? {
            TreeItem::Task { task_idx } => self.tasks.get(*task_idx),
            _ => None,
        }
    }

    /// Visible 1-based ID of the tree-selected task.
    pub fn tree_selected_task_visible_id(&self) -> Option<usize> {
        let task_idx = match self.project_tree.get(self.tree_selected)? {
            TreeItem::Task { task_idx } => *task_idx,
            _ => return None,
        };
        let visible: Vec<&Task> = self.tasks.iter().filter(|t| !t.is_deleted()).collect();
        visible
            .iter()
            .position(|t| std::ptr::eq(*t, &self.tasks[task_idx]))
            .map(|p| p + 1)
    }

    // ── form helpers ──────────────────────────────────────────────────────────

    pub fn open_edit_form(&mut self) {
        if let Some(real) = self.selected_real_index() {
            let task = self.tasks[real].clone();
            let all_tasks = self.tasks.clone();
            let projects = self.projects.clone();
            self.edit_form = Some(EditFormState::from_task(&task, &all_tasks, &projects));
            self.mode = Mode::EditForm;
            self.status_msg = None;
        }
    }

    pub fn open_add_form(&mut self) {
        self.edit_form = Some(EditFormState::blank());
        self.mode = Mode::AddForm;
        self.status_msg = None;
    }

    // ── selection helpers ─────────────────────────────────────────────────────

    pub fn selected_task(&self) -> Option<&Task> {
        let real = *self.filtered_indices.get(self.selected)?;
        self.tasks.get(real)
    }

    pub fn selected_real_index(&self) -> Option<usize> {
        self.filtered_indices.get(self.selected).copied()
    }

    pub fn selected_visible_id(&self) -> Option<usize> {
        let real = self.selected_real_index()?;
        Some(real + 1)
    }

    // ── navigation ────────────────────────────────────────────────────────────

    pub fn move_down(&mut self) {
        if !self.filtered_indices.is_empty() {
            self.selected = (self.selected + 1).min(self.filtered_indices.len() - 1);
            self.details_scroll = 0;
        }
    }

    pub fn move_up(&mut self) {
        self.selected = self.selected.saturating_sub(1);
        self.details_scroll = 0;
    }

    pub fn scroll_details_down(&mut self) {
        self.details_scroll = self.details_scroll.saturating_add(1);
    }
    pub fn scroll_details_up(&mut self) {
        self.details_scroll = self.details_scroll.saturating_sub(1);
    }

    pub fn cycle_status_filter(&mut self) {
        self.list_filter = self.list_filter.next();
        self.selected = 0;
        self.details_scroll = 0;
        self.refilter();
    }

    pub fn cycle_priority_filter(&mut self) {
        self.priority_filter = self.priority_filter.next();
        self.selected = 0;
        self.details_scroll = 0;
        self.refilter();
    }

    pub fn move_left_down(&mut self) {
        let len = self.left_list_len();
        if len > 0 {
            self.left_selected = (self.left_selected + 1).min(len - 1);
        }
    }

    pub fn move_left_up(&mut self) {
        self.left_selected = self.left_selected.saturating_sub(1);
    }

    // ── lists ─────────────────────────────────────────────────────────────────

    pub fn projects_list(&self) -> Vec<String> {
        let mut names: Vec<String> = self
            .projects
            .iter()
            .filter(|p| !p.is_deleted())
            .map(|p| p.name.clone())
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();
        names.sort();
        names
    }

    pub fn tags_list(&self) -> Vec<String> {
        let mut tags: Vec<String> = self
            .tasks
            .iter()
            .flat_map(|t| t.tags.clone())
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();
        tags.sort();
        tags
    }

    pub fn tasks_for_selected_project(&self) -> Vec<&Task> {
        let projects = self.projects_list();
        if let Some(proj_name) = projects.get(self.left_selected) {
            let proj_uuid = self
                .projects
                .iter()
                .find(|p| &p.name == proj_name && !p.is_deleted())
                .map(|p| p.uuid);
            self.tasks
                .iter()
                .filter(|t| proj_uuid.is_some() && t.project_id == proj_uuid)
                .collect()
        } else {
            vec![]
        }
    }

    pub fn tasks_for_selected_tag(&self) -> Vec<&Task> {
        let tags = self.tags_list();
        if let Some(tag) = tags.get(self.left_selected) {
            self.tasks.iter().filter(|t| t.tags.contains(tag)).collect()
        } else {
            vec![]
        }
    }

    pub fn left_list_len(&self) -> usize {
        match self.left_panel {
            LeftPanel::Tasks => self.filtered_indices.len(),
            LeftPanel::Projects => self.project_tree.len(),
            LeftPanel::Tags => self.tags_list().len(),
        }
    }

    pub fn pending_count(&self) -> usize {
        self.tasks.iter().filter(|t| !t.completed).count()
    }
    pub fn total_count(&self) -> usize {
        self.tasks.len()
    }

    fn load_visible(storage: &impl Storage) -> Result<Vec<Task>> {
        Ok(storage
            .load()?
            .into_iter()
            .filter(|t| !t.is_deleted())
            .collect())
    }
}