Skip to main content

quest_tui/
actions.rs

1//! Actions to do after a specific event occurs
2
3use crate::{App, InputMode, Quest};
4
5pub fn new_quest(app: &mut App) {
6    app.input_mode = InputMode::Adding;
7    app.selected_quest = None;
8}
9
10pub fn exit_app(app: &mut App) {
11    app.should_exit = true;
12}
13
14pub fn list_up(app: &mut App) {
15    if let Some(index) = app.selected_quest {
16        if index > 0 {
17            app.selected_quest = Some(index - 1);
18        }
19    }
20}
21
22pub fn list_down(app: &mut App) {
23    if let Some(index) = app.selected_quest {
24        if index < app.quests.len() - 1 {
25            app.selected_quest = Some(index + 1);
26        }
27    }
28}
29
30pub fn check_and_uncheck_quest(app: &mut App) {
31    if let Some(index) = app.selected_quest {
32        app.quests[index].completed = !app.quests[index].completed;
33    }
34}
35
36pub fn delete_quest(app: &mut App) {
37    if let Some(index) = app.selected_quest {
38        app.quests.remove(index);
39        if app.quests.is_empty() {
40            app.selected_quest = None;
41        } else if app.selected_quest.unwrap() == app.quests.len() {
42            app.selected_quest = Some(app.quests.len() - 1);
43        }
44    }
45}
46
47pub fn save_quest(app: &mut App) {
48    let new_quest = Quest::new(app.input.drain(..).collect());
49    app.quests.push(new_quest);
50}
51
52pub fn exit_adding(app: &mut App) {
53    app.input_mode = InputMode::Normal;
54    app.selected_quest = Some(0);
55}
56
57pub fn input_add_char(app: &mut App, c: char) {
58    app.input.push(c);
59}
60
61pub fn input_del_char(app: &mut App) {
62    app.input.pop();
63}