checklist-tui 0.1.2

A TUI for keeping track of your tasks in slim terminal views
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
use std::cmp::Ordering;
use std::collections::HashSet;
use std::string::ToString;

use chrono::prelude::*;
use clap::ValueEnum;
use crossterm::style::Stylize;
use ratatui::widgets::ListState;
use rusqlite::{types::FromSql, types::ValueRef, ToSql};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Enum to help control what tasks are to be displayed
#[derive(Clone, Copy, Debug, ValueEnum, strum_macros::Display, Serialize, Deserialize)]
pub enum Display {
    All,
    Completed,
    NotCompleted,
}

impl Display {
    /// Will rotate through the different enum variants
    pub fn next(&mut self) {
        match self {
            Display::All => *self = Display::Completed,
            Display::Completed => *self = Display::NotCompleted,
            Display::NotCompleted => *self = Display::All,
        }
    }
}

/// Enum to handle the urgency of a `Task`
#[derive(
    Clone,
    Debug,
    Copy,
    PartialEq,
    Eq,
    PartialOrd,
    Ord,
    ValueEnum,
    strum_macros::Display,
    Default,
    Serialize,
    Deserialize,
)]
pub enum Urgency {
    #[default]
    Low,
    Medium,
    High,
    Critical,
}

impl Urgency {
    /// Will return a `StyledContent<String>` based on the Urgency
    pub fn to_colored_string(self) -> crossterm::style::StyledContent<String> {
        match self {
            Urgency::Low => String::from("Low").green(),
            Urgency::Medium => String::from("Medium").yellow(),
            Urgency::High => String::from("High").dark_yellow(),
            Urgency::Critical => String::from("Critical").red(),
        }
    }
}

impl From<&str> for Urgency {
    fn from(s: &str) -> Self {
        match s {
            "Low" => Urgency::Low,
            "Medium" => Urgency::Medium,
            "High" => Urgency::High,
            "Critical" => Urgency::Critical,
            _ => {
                println!("String received was not a valid Urgency");
                panic!()
            }
        }
    }
}

impl ToSql for Urgency {
    fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
        Ok(self.to_string().into())
    }
}

impl FromSql for Urgency {
    fn column_result(value: ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
        value.as_str().map(Into::into)
    }
}

/// Enum to handle the status of a `Task`
#[derive(Clone, Debug, Copy, ValueEnum, strum_macros::Display, PartialEq, Eq, Default)]
pub enum Status {
    #[default]
    Open,
    Working,
    Paused,
    Completed,
}

impl Status {
    /// Will return a `StyledContent<String>` based on the Status
    pub fn to_colored_string(self) -> crossterm::style::StyledContent<String> {
        match self {
            Status::Open => String::from("Open").cyan(),
            Status::Working => String::from("Working").dark_green(),
            Status::Paused => String::from("Paused").dark_yellow(),
            Status::Completed => String::from("Completed").green(),
        }
    }
}

impl From<&str> for Status {
    fn from(s: &str) -> Self {
        match s {
            "Open" => Status::Open,
            "Working" => Status::Working,
            "Paused" => Status::Paused,
            "Completed" => Status::Completed,
            _ => {
                println!("String received wasn not a valid Status");
                panic!()
            }
        }
    }
}

impl ToSql for Status {
    fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
        Ok(self.to_string().into())
    }
}

impl FromSql for Status {
    fn column_result(value: ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
        value.as_str().map(Into::into)
    }
}

/// Struct that holds the attributes to a Task
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Task {
    id: Uuid,
    pub name: String,
    pub description: Option<String>,
    pub latest: Option<String>,
    pub urgency: Urgency,
    pub status: Status,
    pub tags: Option<HashSet<String>>,
    pub date_added: DateTime<Local>,
    pub completed_on: Option<DateTime<Local>>,
}

impl Task {
    /// Creates a new `Task`, requiring only a `String` name.
    /// Everything else is optional.
    pub fn new(
        name: String,
        description: Option<String>,
        latest: Option<String>,
        urgency: Option<Urgency>,
        status: Option<Status>,
        tags: Option<HashSet<String>>,
    ) -> Self {
        let status_value = status.unwrap_or(Status::Open);
        Self {
            id: Uuid::new_v4(),
            name,
            description,
            latest,
            urgency: urgency.unwrap_or(Urgency::Low),
            status: status_value,
            tags,
            date_added: Local::now(),
            completed_on: if status_value == Status::Completed {
                Some(Local::now())
            } else {
                None
            },
        }
    }

    pub fn get_id(&self) -> Uuid {
        self.id
    }

    pub fn get_date_added(&self) -> DateTime<Local> {
        self.date_added
    }

    pub fn from_sql(
        id: Uuid,
        name: String,
        description: Option<String>,
        latest: Option<String>,
        urgency: Urgency,
        status: Status,
        tags: Option<HashSet<String>>,
        date_added: DateTime<Local>,
        completed_on: Option<DateTime<Local>>,
    ) -> Self {
        Self {
            id,
            name,
            description,
            latest,
            urgency,
            status,
            tags,
            date_added,
            completed_on,
        }
    }
}

fn urgency_desc(a: &Task, b: &Task) -> Ordering {
    if a.urgency < b.urgency {
        return Ordering::Greater;
    }
    if a.urgency == b.urgency {
        if a.date_added > b.date_added {
            return Ordering::Less;
        }
        return Ordering::Greater;
    }
    Ordering::Less
}

fn urgency_asc(a: &Task, b: &Task) -> Ordering {
    if a.urgency > b.urgency {
        return Ordering::Greater;
    }
    if a.urgency == b.urgency {
        if a.date_added > b.date_added {
            return Ordering::Greater;
        }
        return Ordering::Less;
    }
    Ordering::Less
}

/// Struct that holds a vector of `Task`, and
/// a ratatui's `ListState`.
///
/// Meant to be used within the TUI
#[derive(Clone, Debug)]
pub struct TaskList {
    pub tasks: Vec<Task>,
    pub state: ListState,
}

impl TaskList {
    /// Creates a new `TaskList` with an empty vector of `Task`s and a `ListState::default()`.
    pub fn new() -> Self {
        TaskList {
            tasks: vec![],
            state: ListState::default(),
        }
    }

    /// Creates a new `TaskList` given a vector of `Task`s. Will start with `ListState::default()`.
    pub fn from(tasks: Vec<Task>) -> Self {
        TaskList {
            tasks,
            state: ListState::default(),
        }
    }

    /// Sorts the `TaskList` based on the `Urgency` in the vector of `Task`s.
    /// If `descending` is true, sort will be done in a Critical > Low order.
    pub fn sort_by_urgency(&mut self, descending: bool) {
        if descending {
            self.tasks.sort_by(urgency_desc)
        } else {
            self.tasks.sort_by(urgency_asc)
        }
    }

    pub fn len(&self) -> usize {
        self.tasks.len()
    }

    /// Filters the `TaskList`, either on a `Display` given or by a tag `String`
    pub fn filter_tasks(&mut self, display_option: Option<Display>, tags_filter: String) {
        let mut tasks_to_keep = vec![];
        'task: for task in &mut self.tasks.iter() {
            // check if fits our display needs
            match display_option {
                Some(display) => match display {
                    Display::Completed => {
                        if task.status != Status::Completed {
                            continue 'task;
                        }
                    }
                    Display::NotCompleted => {
                        if task.status == Status::Completed {
                            continue 'task;
                        }
                    }
                    Display::All => {}
                },
                None => {
                    if task.status == Status::Completed {
                        continue 'task;
                    }
                }
            }

            // Check if our tag string is within any of our tags
            if !tags_filter.is_empty() {
                match task.tags.clone() {
                    Some(task_tags) => {
                        for tag in task_tags {
                            if tag.contains(&tags_filter) {
                                // on first match, we can add to our tasks
                                // to keep and move on
                                tasks_to_keep.push(task.clone());
                                continue 'task;
                            }
                        }
                        continue 'task;
                    }
                    None => continue 'task,
                }
            } else {
                // if tags_filter is empty, we just push everything
                tasks_to_keep.push(task.clone());
            }
        }
        self.tasks = tasks_to_keep;
    }
}

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

    #[test]
    fn test_urgency_ordering() {
        assert!(Urgency::Low < Urgency::Medium);
        assert!(Urgency::Medium < Urgency::High);
        assert!(Urgency::High < Urgency::Critical);
        assert!(Urgency::Low < Urgency::Critical);
        assert!(Urgency::Low == Urgency::Low);
    }

    #[test]
    fn test_sort_by_urgency() {
        let task1 = Task::new(
            String::from("Task1"),
            None,
            None,
            Some(Urgency::Low),
            None,
            None,
        );
        let task2 = Task::new(
            String::from("Task1"),
            None,
            None,
            Some(Urgency::High),
            None,
            None,
        );
        let task3 = Task::new(
            String::from("Task1"),
            None,
            None,
            Some(Urgency::Critical),
            None,
            None,
        );
        let task4 = Task::new(
            String::from("Task1"),
            None,
            None,
            Some(Urgency::Medium),
            None,
            None,
        );
        let task5 = Task::new(
            String::from("Task1"),
            None,
            None,
            Some(Urgency::Low),
            None,
            None,
        );

        let mut task_vec = TaskList::from(vec![task1, task2, task3, task4, task5]);

        // Descending sort
        task_vec.sort_by_urgency(true);
        assert_eq!(task_vec.tasks[0].urgency, Urgency::Critical);
        assert_eq!(task_vec.tasks[1].urgency, Urgency::High);
        assert_eq!(task_vec.tasks[2].urgency, Urgency::Medium);
        assert_eq!(task_vec.tasks[3].urgency, Urgency::Low);
        assert_eq!(task_vec.tasks[4].urgency, Urgency::Low);
        println!("{:?}", task_vec.tasks[3].date_added);
        println!("{:?}", task_vec.tasks[4].date_added);
        assert!(task_vec.tasks[3].date_added > task_vec.tasks[4].date_added);

        // Ascending sort
        task_vec.sort_by_urgency(false);
        assert_eq!(task_vec.tasks[0].urgency, Urgency::Low);
        assert_eq!(task_vec.tasks[1].urgency, Urgency::Low);
        assert_eq!(task_vec.tasks[2].urgency, Urgency::Medium);
        assert_eq!(task_vec.tasks[3].urgency, Urgency::High);
        assert_eq!(task_vec.tasks[4].urgency, Urgency::Critical);
        assert!(task_vec.tasks[0].date_added < task_vec.tasks[1].date_added);
    }
}