todotxt-tui 0.3.0

Todo.txt TUI is a highly customizable terminal-based application for managing your todo tasks. It follows the todo.txt format and offers a wide range of configuration options to suit your needs.
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
mod handle_event_trait;
mod popup;
mod ui_event;
mod ui_state;

use popup::Popup;

pub use handle_event_trait::HandleEvent;
pub use ui_event::*;
pub use ui_state::UIState;

use crate::{
    config::{Config, PasteBehavior, UiConfig, WidgetBorderType},
    file_worker::{FileWorker, FileWorkerCommands},
    layout::{Layout, Render},
    todo::{autocomplete, ToDo},
};
use anyhow::Result;
use crossterm::{
    event::{
        self, read, DisableBracketedPaste, DisableMouseCapture, EnableBracketedPaste,
        EnableMouseCapture, Event, KeyCode, MouseEvent,
    },
    execute,
    terminal::{
        disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, SetTitle,
    },
    ExecutableCommand,
};
use std::{
    io,
    panic::{set_hook, take_hook},
    sync::{mpsc::Sender, Arc, Mutex},
};
use tui::{
    backend::{Backend, CrosstermBackend},
    layout::{Constraint, Direction, Layout as tuiLayout, Position, Rect},
    style::{Color, Style},
    widgets::{Block, Borders, Paragraph},
    Terminal,
};
use tui_input::{backend::crossterm::EventHandler, Input};

/// Enum representing the different modes of the UI.
#[derive(Debug, PartialEq, Eq)]
enum Mode {
    Input,
    Edit,
    Search,
    Normal,
}

/// The struct representing the UI for the application.
pub struct UI {
    input_chunk: Rect,
    tinput: Input,
    layout: Layout,
    mode: Mode,
    data: Arc<Mutex<ToDo>>,
    tx: Sender<FileWorkerCommands>,
    quit: bool,
    active_color: Color,
    config: UiConfig,
    border_type: WidgetBorderType,
    popup: Popup,
    help_text: String,
}

impl UI {
    /// Creates a new instance of the UI.
    ///
    /// # Arguments
    ///
    /// * `layout` - The initial layout configuration for the UI.
    /// * `data` - Shared data representing the to-do list.
    /// * `tx` - Sender for communicating with the file worker.
    ///
    pub fn new(
        layout: Layout,
        data: Arc<Mutex<ToDo>>,
        tx: Sender<FileWorkerCommands>,
        config: &Config,
    ) -> UI {
        UI {
            input_chunk: Rect::default(),
            tinput: Input::default(),
            layout,
            mode: Mode::Normal,
            data,
            tx,
            quit: false,
            active_color: *config.styles.active_color,
            config: config.ui_config.clone(),
            border_type: config.widget_base_config.border_type,
            popup: Popup::new(config.widget_base_config.border_type),
            help_text: Self::build_help_text(config),
        }
    }

    fn build_help_text(config: &Config) -> String {
        fn section(text: &mut String, title: &str, handler: &EventHandlerUI) {
            if handler.is_empty() {
                return;
            }
            text.push_str(&format!(" {title}:\n"));
            for (key, event) in handler.entries() {
                if *event != UIEvent::None {
                    text.push_str(&format!("  {:<12} {event}\n", key.to_string()));
                }
            }
            text.push('\n');
        }

        let mut text = String::new();
        section(&mut text, "Window", &config.ui_config.window_keybinds);
        section(
            &mut text,
            "List navigation",
            &config.list_config.list_keybind,
        );
        section(
            &mut text,
            "Task actions",
            &config.widget_base_config.tasks_keybind,
        );
        section(
            &mut text,
            "Category actions",
            &config.widget_base_config.category_keybind,
        );
        text
    }

    /// Builds a new `UI` instance using the provided configuration.
    ///
    /// # Arguments
    ///
    /// * `config`: A reference to a `Config` struct containing all necessary settings for building the UI.
    ///
    /// # Returns
    ///
    /// * On success, returns an `Ok(UI)` containing the newly built UI.
    /// * On failure, returns an `Err(Result<(), ErrorKind>)`.
    pub fn build(config: &Config) -> Result<UI> {
        let mut todo = ToDo::new(
            config.todo_config.clone(),
            config.hook_paths.clone(),
            config.styles.clone(),
        );

        let mut init_widget = config.ui_config.init_widget;
        if let Some(path) = &config.ui_config.save_state_path {
            match UIState::load(path) {
                Ok(UIState { active, todo_state }) => {
                    todo.update_state(todo_state);
                    init_widget = active;
                }
                Err(e) => log::error!("Cannot load state: {e}"),
            }
        }

        let mut layout = Layout::from_str(&config.ui_config.layout, &todo, config)?;

        let todo = Arc::new(Mutex::new(todo));
        let file_worker = FileWorker::new(config.file_worker_config.clone(), todo.clone())?;

        file_worker.load()?;
        let tx = file_worker.run()?;

        layout.select_widget(init_widget, &todo.lock().unwrap());

        Ok(UI::new(layout, todo, tx.clone(), config))
    }

    /// Updates the input chunk of the UI based on the main chunk's dimensions.
    ///
    /// This method recalculates the position and size of the input chunk based on the dimensions
    /// of the main chunk, ensuring proper rendering of the input field.
    ///
    /// # Arguments
    ///
    /// * `main_chunk` - The main chunk's dimensions, typically representing the entire terminal window.
    fn update_chunk(&mut self, main_chunk: Rect) {
        let layout = tuiLayout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Length(3), Constraint::Min(1)])
            .split(main_chunk);
        self.input_chunk = layout[0];
        self.layout.update_chunk(layout[1]);
    }

    /// Runs the user interface, handling setup and cleanup of terminal interactions.
    ///
    /// This method enables raw mode, sets up the terminal, and enters the main event loop.
    ///
    /// # Returns
    ///
    /// An `Result` indicating the success of running the user interface.
    pub fn run(&mut self) -> Result<()> {
        fn restore_tui(enable_mouse: bool, paste_behavior: PasteBehavior) -> io::Result<()> {
            disable_raw_mode()?;
            execute!(io::stdout(), LeaveAlternateScreen,)?;
            if enable_mouse {
                execute!(io::stdout(), DisableMouseCapture,)?;
            }
            if paste_behavior != PasteBehavior::AsKeys {
                execute!(io::stdout(), DisableBracketedPaste,)?;
            }
            Ok(())
        }

        fn run_ui(this: &mut UI) -> Result<()> {
            // setup terminal
            enable_raw_mode()?;
            execute!(io::stdout(), EnterAlternateScreen,)?;
            if this.config.enable_mouse {
                execute!(io::stdout(), EnableMouseCapture,)?;
            }
            if this.config.paste_behavior != PasteBehavior::AsKeys {
                execute!(io::stdout(), EnableBracketedPaste,)?;
            }

            let mut backend = CrosstermBackend::new(io::stdout());
            backend.execute(SetTitle(this.config.window_title.clone()))?;

            let mut terminal = Terminal::new(backend)?;
            terminal.hide_cursor()?;
            let size = terminal.size()?;
            this.update_chunk(Rect::new(0, 0, size.width, size.height));

            this.draw(&mut terminal)?;
            this.main_loop(&mut terminal)?;

            // restore terminal
            restore_tui(this.config.enable_mouse, this.config.paste_behavior)?;
            terminal.show_cursor()?;

            Ok(())
        }

        // Setup panic hook.
        let orig_hook = take_hook();
        let enable_mouse = self.config.enable_mouse;
        let paste_behavior = self.config.paste_behavior;
        set_hook(Box::new(move |panic_info| {
            let _ = restore_tui(enable_mouse, paste_behavior);
            orig_hook(panic_info);
        }));

        if let Err(e) = run_ui(self) {
            self.tx.send(FileWorkerCommands::Exit).unwrap();
            Err(e)
        } else {
            Ok(())
        }
    }

    /// Handles the main event loop of the UI.
    ///
    /// # Arguments
    ///
    /// * `terminal` - The TUI Terminal.
    ///
    /// # Returns
    ///
    /// An `Result` indicating the success of the main loop.
    fn main_loop<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> Result<()> {
        let mut versions = self.data.lock().unwrap().get_version().get_version_all();
        loop {
            if event::poll(self.config.list_refresh_rate)? {
                if self.process_event()? {
                    break;
                }
                versions = self.data.lock().unwrap().get_version().get_version_all();
                self.draw(terminal)?;
            } else if !self
                .data
                .lock()
                .unwrap()
                .get_version()
                .is_actual_all(versions)
            {
                versions = self.data.lock().unwrap().get_version().get_version_all();
                self.draw(terminal)?;
            }
        }
        Ok(())
    }

    /// Draws the UI on the terminal.
    ///
    /// # Arguments
    ///
    /// * `terminal` - The TUI Terminal.
    ///
    /// # Returns
    ///
    /// An `Result` indicating the success of drawing.
    fn draw<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> Result<()> {
        let mut block = Block::default()
            .borders(Borders::ALL)
            .title("Input")
            .border_type(self.border_type.into());
        if self.mode == Mode::Input || self.mode == Mode::Edit || self.mode == Mode::Search {
            block = block.border_style(Style::default().fg(self.active_color));
        }
        terminal
            .draw(|f| {
                f.render_widget(
                    Paragraph::new(self.tinput.value()).block(block),
                    self.input_chunk,
                );
                self.layout.render(f, &self.data.lock().unwrap());

                if self.mode == Mode::Input || self.mode == Mode::Edit {
                    let width = self.input_chunk.width.max(3) - 3;
                    let scroll = self.tinput.visual_scroll(width as usize);
                    f.set_cursor_position(Position {
                        x: self.input_chunk.x
                            + (self.tinput.visual_cursor().max(scroll) - scroll) as u16
                            + 1,
                        y: self.input_chunk.y + 1,
                    });
                }

                self.popup.render_popup(f);
            })
            .map_err(|e| anyhow::anyhow!("{e}"))?;
        Ok(())
    }

    /// Handles various user events.
    ///
    /// # Returns
    ///
    /// An `Result` indicating whether the application should exit.
    fn process_event(&mut self) -> Result<bool> {
        self.handle_event_window(read()?);
        Ok(self.quit)
    }

    /// Handles window events, such as resizing and mouse clicks, to manage the
    /// user interface state.
    ///
    /// # Arguments
    ///
    /// * `e`: The event that triggers the function, which can be a resize event
    ///   or a mouse click event.
    ///
    /// # Details
    ///
    /// This function processes different types of events:
    /// - **Resize Event**: Adjusts the UI chunk based on the new window dimensions.
    /// - **Mouse Click Event**: Triggers a click action in the layout manager
    ///   at the specified column and row.
    /// - **Keyboard Events**: Depending on the current mode (`Mode::Input`, `Mode::Edit`,
    ///   or `Mode::Normal`), handles input for task creation, editing, and general navigation
    ///   using specific keys.
    fn handle_event_window(&mut self, e: Event) {
        match (&e, &self.mode) {
            (Event::Resize(width, height), _) => {
                log::debug!("Resize event: width {width}, height {height}");
                self.update_chunk(Rect::new(0, 0, *width, *height));
            }
            (
                Event::Mouse(MouseEvent {
                    kind: event::MouseEventKind::Up(event::MouseButton::Left),
                    column,
                    row,
                    modifiers: _,
                }),
                _,
            ) => {
                log::debug!("Mouse event: column {column}, row {row}");
                self.layout.click(*column, *row, &self.data.lock().unwrap());
            }
            (Event::Paste(s), Mode::Normal) => {
                if self.config.paste_behavior == PasteBehavior::Insert {
                    log::debug!("Pasted: {s}");
                    if let Err(e) = self.data.lock().unwrap().new_task(s) {
                        log::error!("Error while pasting new task: {e}");
                        self.popup.add_message(format!("Failed paste task: {e}"));
                    }
                }
            }
            (Event::Paste(s), _) => self.tinput = Input::new(self.tinput.value().to_owned() + s),
            (Event::Key(event), Mode::Input) => match event.code {
                KeyCode::Enter => {
                    if let Err(e) = self.data.lock().unwrap().new_task(self.tinput.value()) {
                        log::error!("Error while adding new task: {e}");
                        self.popup.add_message(format!("Failed add task: {e}"));
                    }
                    self.tinput.reset();
                    self.mode = Mode::Normal;
                    self.layout.focus(&self.data.lock().unwrap());
                }
                KeyCode::Esc => {
                    self.mode = Mode::Normal;
                    self.layout.focus(&self.data.lock().unwrap());
                }
                KeyCode::Tab => {
                    if let Some(input) =
                        autocomplete(&self.data.lock().unwrap(), self.tinput.value())
                    {
                        self.tinput = input.into();
                    }
                }
                _ => {
                    self.tinput.handle_event(&e);
                }
            },
            (Event::Key(event), Mode::Edit) => match event.code {
                KeyCode::Enter => {
                    if let Err(e) = self.data.lock().unwrap().update_active(self.tinput.value()) {
                        log::error!("Error while updating existing task: {e}");
                        self.popup.add_message(format!("Failed update task: {e}"));
                    }
                    self.tinput.reset();
                    self.mode = Mode::Normal;
                    self.layout.focus(&self.data.lock().unwrap());
                }
                KeyCode::Esc => {
                    self.tinput.reset();
                    self.mode = Mode::Normal;
                    self.layout.focus(&self.data.lock().unwrap());
                }
                KeyCode::Tab => {
                    if let Some(input) =
                        autocomplete(&self.data.lock().unwrap(), self.tinput.value())
                    {
                        self.tinput = input.into();
                    }
                }
                _ => {
                    self.tinput.handle_event(&e);
                }
            },
            (Event::Key(event), Mode::Search) => match event.code {
                KeyCode::Enter => {
                    self.mode = Mode::Normal;
                    self.layout.focus(&self.data.lock().unwrap());
                    self.tinput.reset();
                }
                KeyCode::Esc => {
                    self.tinput.reset();
                    self.mode = Mode::Normal;
                    self.layout.clean_search();
                    self.layout.focus(&self.data.lock().unwrap());
                }
                _ => {
                    self.tinput.handle_event(&e);
                    self.layout.search(self.tinput.to_string())
                }
            },
            (Event::Key(event), Mode::Normal) => {
                log::debug!("Handle event: {:?}", event);
                if self.popup.is_help_visible() && event.code == KeyCode::Esc {
                    self.popup.hide_help();
                } else if !self.handle(event) && !self.popup.is_help_visible() {
                    self.layout
                        .handle_key(event, &mut self.data.lock().unwrap());
                }
            }
            _ => {}
        }
    }

    fn handle(&mut self, event: &event::KeyEvent) -> bool {
        use UIEvent::*;
        match self.config.window_keybinds.get_event(event) {
            Quit => {
                if let Some(path) = &self.config.save_state_path {
                    if let Err(e) =
                        UIState::new(&self.layout, &self.data.lock().unwrap()).save(path)
                    {
                        log::error!("Error while saving UI state: {}", e);
                    }
                }
                self.quit = true;
            }
            InsertMode => {
                self.mode = Mode::Input;
                self.layout.unfocus();
            }
            MoveRight => {
                self.layout.right(&self.data.lock().unwrap());
            }
            MoveLeft => {
                self.layout.left(&self.data.lock().unwrap());
            }
            MoveUp => {
                self.layout.up(&self.data.lock().unwrap());
            }
            MoveDown => {
                self.layout.down(&self.data.lock().unwrap());
            }
            Save => {
                if let Err(e) = self.tx.send(FileWorkerCommands::ForceSave) {
                    log::error!("Error while send signal to save todo list: {e}");
                    self.popup
                        .add_message(format!("Cannot save todo list: {e}"));
                }
            }
            Load => {
                if let Err(e) = self.tx.send(FileWorkerCommands::Load) {
                    log::error!("Error while send signal to load todo list: {e}");
                    self.popup
                        .add_message(format!("Cannot load todo list: {e}"));
                }
            }
            EditMode => {
                if let Some(active) = self.data.lock().unwrap().get_active() {
                    self.tinput = active.to_string().into();
                    self.mode = Mode::Edit;
                    self.layout.unfocus();
                    // self.in
                }
            }
            SearchMode => {
                self.tinput.reset();
                self.mode = Mode::Search;
                self.layout.unfocus();
            }
            ShowHelp => {
                if self.popup.is_help_visible() {
                    self.popup.hide_help();
                } else {
                    self.popup.show_help(self.help_text.clone());
                }
            }
            _ => {
                return false;
            }
        }
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{config::Conf, layout::widget::WidgetType};
    use crossterm::event::KeyEvent;
    use std::{env, str::FromStr};
    use test_log::test;

    macro_rules! handle_event {
        ($ui:expr, $code:expr) => {
            let key_shortcut = KeyShortcut::from_str($code)?;
            let event = Event::Key(KeyEvent::new(key_shortcut.key, key_shortcut.modifiers));
            $ui.handle_event_window(event);
        };
    }

    fn default_ui() -> Result<UI> {
        let config = Config::from_reader(
            format!(
                r#"
            todo_path = "{}test_behaviour_todo.txt"
            save_state_path = "/this/path/does/not/exists"
            save_policy = "ManualOnly"

            [list_keybind]
            E = "EditMode"
            Enter = "Select"
            I = "InsertMode"
            u = "Load"
            S = "Save"
            j = "ListDown"
            q = "Quit"
            "#,
                env::var("TODO_TUI_TEST_DIR")?
            )
            .as_bytes(),
        )?;
        UI::build(&config)
    }

    #[test]
    fn test_moves() -> Result<()> {
        let mut ui = default_ui()?;
        ui.update_chunk(Rect::new(0, 0, 20, 20));

        handle_event!(ui, "S+j");
        assert_eq!(ui.layout.get_active_widget(), WidgetType::List);

        handle_event!(ui, "S+l");
        assert_eq!(ui.layout.get_active_widget(), WidgetType::Done);

        handle_event!(ui, "S+k");
        assert_eq!(ui.layout.get_active_widget(), WidgetType::Done);

        handle_event!(ui, "S+l");
        assert_eq!(ui.layout.get_active_widget(), WidgetType::Done);

        handle_event!(ui, "S+j");
        assert_eq!(ui.layout.get_active_widget(), WidgetType::Context);

        handle_event!(ui, "S+h");
        assert_eq!(ui.layout.get_active_widget(), WidgetType::List);

        Ok(())
    }

    #[test]
    fn test_behaviour() -> Result<()> {
        let mut ui = default_ui()?;
        ui.update_chunk(Rect::new(0, 0, 20, 20));

        let event = Event::Resize(50, 50);
        ui.handle_event_window(event);

        handle_event!(ui, "j");
        assert_eq!(ui.layout.get_active_widget(), WidgetType::List);

        handle_event!(ui, "Enter");
        assert!(ui.data.lock().unwrap().get_active().is_some());

        handle_event!(ui, "S+i");
        assert_eq!(ui.mode, Mode::Input);

        handle_event!(ui, "Esc");
        assert_eq!(ui.mode, Mode::Normal);

        handle_event!(ui, "S+e");
        assert_eq!(ui.mode, Mode::Edit);

        handle_event!(ui, "Esc");
        assert_eq!(ui.mode, Mode::Normal);

        handle_event!(ui, "/");
        assert_eq!(ui.mode, Mode::Search);

        handle_event!(ui, "Esc");
        assert_eq!(ui.mode, Mode::Normal);

        handle_event!(ui, "/");
        assert_eq!(ui.mode, Mode::Search);

        handle_event!(ui, "a");
        assert_eq!(ui.mode, Mode::Search);

        handle_event!(ui, "Enter");
        assert_eq!(ui.mode, Mode::Normal);

        handle_event!(ui, "u");

        handle_event!(ui, "S+i");
        assert_eq!(ui.mode, Mode::Input);

        handle_event!(ui, "a");
        assert_eq!(ui.tinput.to_string(), "a");
        assert_eq!(ui.mode, Mode::Input);
        handle_event!(ui, "b");
        assert_eq!(ui.tinput.to_string(), "ab");
        assert_eq!(ui.mode, Mode::Input);
        handle_event!(ui, "c");
        assert_eq!(ui.tinput.to_string(), "abc");
        assert_eq!(ui.mode, Mode::Input);
        handle_event!(ui, "Tab");
        assert_eq!(ui.tinput.to_string(), "abc");
        assert_eq!(ui.mode, Mode::Input);

        handle_event!(ui, "Enter");
        assert_eq!(ui.tinput.to_string(), "");
        assert_eq!(ui.mode, Mode::Normal);

        handle_event!(ui, "S+e");
        assert_eq!(ui.mode, Mode::Edit);

        handle_event!(ui, " ");
        assert_eq!(ui.tinput.to_string(), "Second task ");
        assert_eq!(ui.mode, Mode::Edit);
        handle_event!(ui, "plus");
        assert_eq!(ui.tinput.to_string(), "Second task +");
        assert_eq!(ui.mode, Mode::Edit);
        handle_event!(ui, "a");
        assert_eq!(ui.tinput.to_string(), "Second task +a");
        assert_eq!(ui.mode, Mode::Edit);
        handle_event!(ui, "Tab");
        assert_eq!(ui.tinput.to_string(), "Second task +abcdef ");
        assert_eq!(ui.mode, Mode::Edit);
        handle_event!(ui, "Backspace");
        handle_event!(ui, "Backspace");
        handle_event!(ui, "Backspace");
        handle_event!(ui, "Backspace");
        handle_event!(ui, "Backspace");
        handle_event!(ui, "Backspace");
        handle_event!(ui, "Backspace");
        handle_event!(ui, "Backspace");
        handle_event!(ui, "Backspace");

        handle_event!(ui, "Enter");
        assert_eq!(ui.tinput.to_string(), "");
        assert_eq!(ui.mode, Mode::Normal);

        // Remove items added in this test
        handle_event!(ui, "S+g");
        handle_event!(ui, "x");
        handle_event!(ui, "x");
        handle_event!(ui, "x");

        // Quit TUI.
        assert!(!ui.quit);
        handle_event!(ui, "q");
        assert!(ui.quit);
        ui.quit = false;

        Ok(())
    }

    #[test]
    fn search_contexts() -> Result<()> {
        let mut ui = default_ui()?;
        ui.update_chunk(Rect::new(0, 0, 20, 20));

        handle_event!(ui, "S+l");
        assert_eq!(ui.layout.get_active_widget(), WidgetType::Done);

        handle_event!(ui, "S+j");
        assert_eq!(ui.layout.get_active_widget(), WidgetType::Context);

        handle_event!(ui, "/");
        assert_eq!(ui.mode, Mode::Search);

        handle_event!(ui, "a");
        assert_eq!(ui.mode, Mode::Search);

        handle_event!(ui, "b");
        assert_eq!(ui.mode, Mode::Search);

        handle_event!(ui, "Enter");
        assert_eq!(ui.mode, Mode::Normal);

        // clean search
        handle_event!(ui, "h");
        assert_eq!(ui.mode, Mode::Normal);

        Ok(())
    }
}