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
/// Functionality for creating todo list using terminal user interface.

pub mod config;
pub mod logger;
mod todo;
mod tui;

use dirs::home_dir;
use log::{info, warn};
use std::cell::RefCell;
use std::env::Args;
use std::fs::{create_dir, metadata, File};
use std::io::{self, Read};
use std::path::PathBuf;
use std::rc::{Rc, Weak};
use std::str::Lines;
use termion::event::Key;
use todo::{Priority, ToDo};
use tui::Window;

/// Check if save file exists.
pub fn look_for_save(mut args: Args) -> Result<PathBuf, ()> {
    args.next();

    match args.next() {
        Some(arg) => {
            let filename = PathBuf::from(&arg);
            match metadata(&filename) {
                Ok(_) => return Ok(filename),
                Err(err) => {
                    warn!("Provided save file does not exist: {}", err);
                    Err(())
                }
            }
        }
        None => {
            let mut filename = match home_dir() {
                Some(dir) => dir,
                None => {
                    warn!("Unable to find home directory.");
                    return Err(());
                }
            };
            filename.push(".todo");

            match metadata(&filename) {
                Ok(_) => {
                    filename.push("save.txt");
                    match metadata(&filename) {
                        Ok(_) => {
                            info!("Found save file.");
                            Ok(filename.to_path_buf())
                        }
                        Err(err) => {
                            warn!("$HOME/.todo/save.txt does not exist: {}", err);
                            Err(())
                        }
                    }
                }
                Err(_) => {
                    create_dir(filename).unwrap_or_else(|err| {
                        warn!("Unable to create directory ~/.todo: {}", err);
                    });
                    info!("Created $HOME/.todo directory.");
                    Err(())
                }
            }
        }
    }
}

/// Wrapper around the terminal user interface (Window) and the todo list
/// tree structure (ToDo).
pub struct View<'a> {
    window: Window<'a>,
    current_task: Rc<RefCell<ToDo>>,
    selection: Option<usize>,
    root: bool,
    quit: bool,
    save_file: Option<PathBuf>,
}

impl<'a> View<'a> {
    /// Create view of a new todo list.
    pub fn new(config: config::Config<'a>) -> Result<View<'a>, ()> {
        let root = ToDo::new("", Weak::new());
        let stdin = io::stdin();
        let stdout = io::stdout();
        let mut window = Window::new(stdin, stdout, config)?;
        window.colour_off();

        info!("Created new View.");
        Ok(View {
            window,
            current_task: Rc::new(RefCell::new(root)),
            selection: None,
            root: true,
            quit: false,
            save_file: None,
        })
    }

    /// Create view of a todo list loaded from save file.
    pub fn new_from_save(filename: PathBuf, config: config::Config<'a>) -> Result<View<'a>, ()> {
        let root = ToDo::new("", Weak::new());
        let stdin = io::stdin();
        let stdout = io::stdout();
        let mut window = Window::new(stdin, stdout, config)?;
        window.colour_off();

        let mut view = View {
            window,
            current_task: Rc::new(RefCell::new(root)),
            selection: None,
            root: true,
            quit: false,
            save_file: Some(filename.clone()),
        };

        let proot = Rc::clone(&view.current_task);
        if let Ok(buf) = Self::load(filename) {
            match view.fill_children(&mut buf.lines(), 0) {
                Ok(()) => {
                    view.current_task = proot;
                }
                Err(err) => {
                    warn!("Unable to parse save file: {}", err);
                    let new_root = ToDo::new("", Weak::new());
                    view.current_task = Rc::new(RefCell::new(new_root));
                }
            }
        };

        info!("Created new View from save file.");
        Ok(view)
    }

    /// Load save file into string buffer.
    fn load(filename: PathBuf) -> Result<String, ()> {
        let mut file = match File::open(filename) {
            Ok(f) => f,
            Err(_) => {
                warn!("Unable to load file.");
                return Err(());
            }
        };

        let mut buffer = String::new();
        match file.read_to_string(&mut buffer) {
            Ok(_) => Ok(buffer),
            Err(_) => {
                warn!("Unable to read from save file.");
                return Err(());
            }
        }
    }

    /// Parse save file and load into todo list tree structure.
    fn fill_children(&mut self, buf: &mut Lines, tabs: usize) -> Result<(), &'static str> {
        // Parse save file line by line
        if let Some(line) = buf.next() {
            // Use indentation to determine where to insert each task. If
            // indentation is the same as the previous line then we continue
            // adding sub-tasks to the current line.
            let num_tabs = tab_num(&line);
            let current = Rc::clone(&self.current_task);
            if num_tabs == tabs + 1 {
                // If indentation is increased compared to the previous line,
                // then the previously added sub-task is the new current task
                let n = self.current_task.borrow().sub_tasks.len();
                if n == 0 {
                    return Err("Can't have child without parent.");
                }
                let new_current = &current.borrow().sub_tasks[n - 1];
                self.current_task = Rc::clone(&new_current);
            } else if num_tabs < tabs {
                // If indentation is decreased compared to the previous line,
                // then the parent (or an even earlier ancestor) of the
                // previous task is the new current task
                self.ancestor(tabs - num_tabs);
            } else if num_tabs > tabs + 1 {
                return Err("Too much indentation.");
            }

            self.add_task_from_string(line.trim_start());

            // Continue onto next line
            self.fill_children(buf, num_tabs)?;
        }
        Ok(())
    }

    /// Move current task to parent task, if it exists.
    fn ancestor(&mut self, level: usize) {
        let current = Rc::clone(&self.current_task);
        let pparent = &current.borrow().parent;
        if level > 0 {
            if let Some(parent) = pparent.upgrade() {
                self.current_task = Rc::clone(&parent);
                self.ancestor(level - 1);
            }
        }
    }

    /// Game loop for user interaction and display.
    pub fn run(&mut self) {
        loop {
            self.list_tasks();
            match self.window.getch() {
                Some(key) if key == self.window.config.quit => {
                    self.quit = true;
                }
                Some(key) if key == self.window.config.back => match self.root {
                    true => (),
                    false => break,
                },
                Some(key) if key == self.window.config.save => self.save(),
                Some(key) if key == self.window.config.add => self.add_task_from_input(),
                Some(key) if key == self.window.config.edit => self.edit_task(),
                Some(key) if key == self.window.config.delete => self.remove_task(),
                Some(key) if key == self.window.config.task_up => self.move_task(true),
                Some(key) if key == self.window.config.task_down => self.move_task(false),
                Some(key) if key == self.window.config.focus => self.new_focus(),
                Some(key) if key == self.window.config.complete => self.complete_task(),
                Some(key) if key == self.window.config.up => self.move_selection(true),
                Some(key) if key == self.window.config.down => self.move_selection(false),
                Some(key) if key == self.window.config.increase => self.increase_priority(),
                Some(key) if key == self.window.config.decrease => self.decrease_priority(),
                Some(_) => (),
                None => (),
            }
            if self.quit {
                self.window.endwin();
                break;
            }
        }
    }

    /// Create a diaglogue for user input with specified prompt.
    fn input_dialogue(&mut self, prompt: &str) -> String {
        self.dialogue(prompt, "")
    }

    /// Create an editing dialogue.
    fn edit_dialogue(&mut self, prompt: &str, index: usize) -> String {
        let mut original = String::new();
        {
            let sub_tasks = &self.current_task.borrow().sub_tasks;
            original.push_str(&sub_tasks[index].borrow().task);
        }
        self.dialogue(prompt, &original)
    }

    /// A dialogue box for user interaction.
    fn dialogue(&mut self, prompt: &str, text: &str) -> String {
        let (ymax, xmax) = self.window.get_max_yx();
        self.window.border((ymax - 1, 0), (3, xmax));
        self.window
            .rectangle(&(' '.to_string())[..], (ymax - 1, 1), (2, xmax - 2));
        self.window.colour_on(0, 7);
        self.window.mvprintw(ymax - 2, 2, prompt);
        self.window.colour_off();
        self.window.mvprintw(ymax - 2, 3 + prompt.len(), text);
        self.window.refresh();
        self.window.show_cursor();

        let mut entry = String::from(text);
        let mut index = entry.len();
        loop {
            // Print entry
            self.window.mvprintw(ymax - 2, 3 + prompt.len(), &entry);
            self.window
                .mv(ymax - 2, 3 + (prompt.len() + &entry[0..index].len()));
            self.window.refresh();

            // User input
            match self.window.getch() {
                Some(Key::Char('\n')) => break,
                Some(Key::Char(ch)) => {
                    if index >= entry.len() {
                        entry.push(ch);
                    } else {
                        entry.insert(index, ch);
                    }
                    index += 1;
                    ()
                }
                Some(Key::Backspace) => {
                    if entry.len() > 0 {
                        self.window
                            .mvprintw(ymax - 2, 3 + (prompt.len() + entry.len() - 1), " ");
                        entry.remove(index - 1);
                        index -= 1;
                    }
                    ()
                }
                Some(Key::Delete) => {
                    if entry.len() > 0 && index < entry.len() {
                        self.window
                            .mvprintw(ymax - 2, 3 + (prompt.len() + entry.len() - 1), " ");
                        entry.remove(index);
                    }
                    ()
                }
                Some(Key::Left) => {
                    if index > 0 {
                        index -= 1;
                    }
                    ()
                }
                Some(Key::Right) => {
                    if index < entry.len() {
                        index += 1;
                    }
                    ()
                }
                _ => (),
            }
        }
        entry
    }

    /// Display a list of the sub-tasks of the current task.
    fn list_tasks(&mut self) {
        self.window.clear();
        self.window.hide_cursor();

        let (ymax, xmax) = self.window.get_max_yx();

        // Panels
        let mut path = self.current_task.borrow().task.clone();
        self.current_task.borrow().task_path(&mut path);
        self.window.mvprintw(1, 1, &path);
        self.window.border((2, 0), (3, xmax));
        self.window.border((ymax - 4, 0), (ymax - 6, xmax / 2));
        self.window
            .border((ymax - 4, xmax / 2), (ymax - 6, xmax / 2));
        self.window.border((ymax - 1, 0), (3, xmax));

        self.window.colour_on(4, 8);
        self.window.mvprintw(0, 2, "Parent");
        self.window.mvprintw(3, 2, "Tasks");
        self.window.mvprintw(3, xmax / 2 + 2, "Sub-tasks");
        self.window.mvprintw(ymax - 3, 2, "Selection");
        self.window.colour_off();

        self.window.colour_on(6, 8);
        match self.selection {
            Some(index) => {
                if index > self.current_task.borrow().sub_tasks.len() - 1 {
                    warn!("Index larger than it should be.");
                    self.selection = None;
                } else {
                    self.window.mvprintw(4 + index, 1, ">");
                    self.window.mvprintw(
                        ymax - 2,
                        2,
                        &self.current_task.borrow().sub_tasks[index].borrow().task,
                    );
                }
            }
            None => (),
        };
        self.window.colour_off();

        let sub_tasks = &self.current_task.borrow().sub_tasks;
        let mut y = 4;
        for (i, elem) in sub_tasks.iter().enumerate() {
            if elem.borrow().complete {
                self.window.mvprintw(y, 3, "[");
                self.window.colour_on(4, 8);
                self.window.mvprintw(y, 4, "X");
                self.window.colour_off();
                self.window.mvprintw(y, 5, "]");
            } else {
                self.window.mvprintw(y, 3, "[ ]");
            }
            match elem.borrow().priority {
                Some(Priority::Low) => {
                    self.window.colour_on(2, 8);
                }
                Some(Priority::Medium) => {
                    self.window.colour_on(3, 8);
                }
                Some(Priority::High) => {
                    self.window.colour_on(1, 8);
                }
                _ => (),
            };
            self.window
                .wrap_print(y, 7, xmax / 2 - 8, &format!("{}", elem.borrow().task));
            self.window.colour_off();
            y += 1;

            if let Some(index) = self.selection {
                if index == i {
                    let mut yy = 4;
                    for sub_elem in elem.borrow().sub_tasks.iter() {
                        if sub_elem.borrow().complete {
                            self.window.mvprintw(yy, xmax / 2 + 3, "[");
                            self.window.colour_on(4, 8);
                            self.window.mvprintw(yy, xmax / 2 + 4, "X");
                            self.window.colour_off();
                            self.window.mvprintw(yy, xmax / 2 + 5, "]");
                        } else {
                            self.window.mvprintw(yy, xmax / 2 + 3, "[ ]");
                        }
                        match sub_elem.borrow().priority {
                            Some(Priority::Low) => {
                                self.window.colour_on(2, 8);
                            }
                            Some(Priority::Medium) => {
                                self.window.colour_on(3, 8);
                            }
                            Some(Priority::High) => {
                                self.window.colour_on(1, 8);
                            }
                            _ => (),
                        };
                        self.window.wrap_print(
                            yy,
                            xmax / 2 + 7,
                            xmax / 2 - 8,
                            &format!("{}", sub_elem.borrow().task),
                        );
                        self.window.colour_off();
                        yy += 1;
                    }
                }
            };
        }
        self.window.refresh();
    }

    /// Increase the priority of the currently selected task.
    fn increase_priority(&mut self) {
        match self.selection {
            Some(index) => {
                let current = self.current_task.borrow();
                let mut sub_task = current.sub_tasks[index].borrow_mut();
                sub_task.priority = match sub_task.priority {
                    None => Some(Priority::Low),
                    Some(Priority::Low) => Some(Priority::Medium),
                    Some(Priority::Medium) => Some(Priority::High),
                    Some(Priority::High) => Some(Priority::High),
                };
            }
            None => (),
        }
    }

    /// Decrease the priority of the currently selected task.
    fn decrease_priority(&mut self) {
        match self.selection {
            Some(index) => {
                let current = self.current_task.borrow();
                let mut sub_task = current.sub_tasks[index].borrow_mut();
                sub_task.priority = match sub_task.priority {
                    None => None,
                    Some(Priority::Low) => None,
                    Some(Priority::Medium) => Some(Priority::Low),
                    Some(Priority::High) => Some(Priority::Medium),
                };
            }
            None => (),
        }
    }

    /// Add new task from user input.
    fn add_task_from_input(&mut self) {
        let task = self.input_dialogue("New Task:");
        let parent = Rc::downgrade(&self.current_task);
        let todo = ToDo::new(&task, parent);
        let sub_tasks = &mut self.current_task.borrow_mut().sub_tasks;
        sub_tasks.push(Rc::new(RefCell::new(todo)));
        self.selection = Some(sub_tasks.len() - 1);
    }

    /// Add new task from string buffer.
    fn add_task_from_string(&mut self, input: &str) {
        let parent = Rc::downgrade(&self.current_task);
        let todo = ToDo::from_string(input, parent);
        let sub_tasks = &mut self.current_task.borrow_mut().sub_tasks;
        sub_tasks.push(Rc::new(RefCell::new(todo)));
        self.selection = Some(sub_tasks.len() - 1);
    }

    /// Mark task as completed.
    fn complete_task(&mut self) {
        let sub_tasks = &mut self.current_task.borrow_mut().sub_tasks;
        match self.selection {
            Some(index) => {
                let mut sub_task = sub_tasks[index].borrow_mut();
                sub_task.complete = !sub_task.complete;
            }
            None => (),
        }
    }

    /// Change ordering of sub-tasks for current task.
    fn move_task(&mut self, up: bool) {
        let sub_tasks = &mut self.current_task.borrow_mut().sub_tasks;
        if let Some(index) = self.selection {
            if up {
                let new_index = if index == 0 {
                    sub_tasks.len() - 1
                } else {
                    index - 1
                };
                sub_tasks.swap(new_index, index);
                self.selection = Some(new_index);
            } else {
                let new_index = if index == sub_tasks.len() - 1 {
                    0
                } else {
                    index + 1
                };
                sub_tasks.swap(new_index, index);
                self.selection = Some(new_index);
            }
        }
    }

    /// Focus on currently selected sub-task.
    fn new_focus(&mut self) {
        let previous_root = self.root.clone();
        let previous_selection = self.selection.clone();
        let psub_tasks = Rc::clone(&self.current_task);
        let sub_tasks = &psub_tasks.borrow().sub_tasks;
        match self.selection {
            Some(index) => {
                // Focus on sub-task
                let sub_task = &sub_tasks[index];
                self.current_task = Rc::clone(sub_task);
                self.root = false;
                self.selection = if self.current_task.borrow().sub_tasks.len() > 0 {
                    Some(0)
                } else {
                    None
                };
                self.run();

                // Return to parent task (unwrap cannot panic here)
                self.current_task = sub_task.borrow().parent.upgrade().unwrap();
                self.root = previous_root;
                self.selection = previous_selection;
            }
            None => (),
        }
    }

    /// Edited currently selected sub-task.
    fn edit_task(&mut self) {
        match self.selection {
            Some(index) => {
                let task = self.edit_dialogue("Edit Task:", index);
                let current_task = self.current_task.borrow_mut();
                let mut sub_task = current_task.sub_tasks[index].borrow_mut();
                sub_task.task = task.to_string();
            }
            None => (),
        }
    }

    /// Move selection cursor.
    fn move_selection(&mut self, ifup: bool) {
        self.selection = match self.selection {
            Some(index) => {
                if ifup {
                    self.up(index)
                } else {
                    self.down(index)
                }
            }
            None => match self.current_task.borrow().sub_tasks.len() {
                0 => None,
                _ => Some(0),
            },
        };
    }

    /// Change index (wrapping below).
    fn up(&self, index: usize) -> Option<usize> {
        let ntasks = self.current_task.borrow().sub_tasks.len();
        if index as isize - 1 < 0 {
            Some(index + ntasks - 1)
        } else {
            Some(index - 1)
        }
    }

    /// Change index (wrapping above).
    fn down(&self, index: usize) -> Option<usize> {
        let ntasks = self.current_task.borrow().sub_tasks.len();
        if index + 1 >= ntasks {
            Some(index + 1 - ntasks)
        } else {
            Some(index + 1)
        }
    }

    /// Create a pop-up diaglogue with user choice.
    fn popup(&mut self, prompt: &str) -> bool {
        let (ymax, xmax) = self.window.get_max_yx();
        self.window.border((ymax - 1, 0), (3, xmax));
        self.window
            .rectangle(&(' '.to_string())[..], (ymax - 1, 1), (2, xmax - 2));
        self.window.colour_on(1, 7);
        self.window.mvprintw(ymax - 2, 2, prompt);
        self.window.colour_off();
        self.window.refresh();

        let mut choice = false;
        loop {
            match self.window.getch() {
                Some(Key::Char('y')) => {
                    choice = true;
                    break;
                }
                Some(Key::Char('n')) => break,
                Some(Key::Char('q')) => break,
                Some(Key::Char('b')) => break,
                _ => (),
            }
        }
        choice
    }

    /// Remove selected sub-task.
    fn remove_task(&mut self) {
        match self.selection {
            Some(index) => {
                if self.popup("Are you sure you want to delete this task? y/n") {
                    let mut current_task = self.current_task.borrow_mut();
                    current_task.sub_tasks.remove(index);
                    self.selection = None;
                }
            }
            None => (),
        }
    }

    /// Save todo list to file.
    fn save(&self) {
        let current = self.current_task.borrow();
        let filename = match self.save_file.clone() {
            Some(f) => f,
            None => {
                let mut buffer = match home_dir() {
                    Some(dir) => dir,
                    None => {
                        warn!("Unable to locate home directory.");
                        return;
                    }
                };
                buffer.push(".todo/save.txt");
                buffer
            }
        };

        current.save(filename.as_path())
    }
}

/// Determine number of tabs at start of string line.
fn tab_num(line: &str) -> usize {
    let mut num = 0;
    while line[num..].starts_with(" ") {
        num += 1;
    }
    num / 4
}