todo_cli_manikya/
app.rs

1pub struct App {
2    pub status: Status,
3}
4
5/// The current status of application
6pub enum Status {
7    /// Editing state
8    ///
9    /// edit stores the buffer value for the new task
10    ///
11    /// previous tells if we are editing a task or creating new one
12    Editing {
13        edit: String,
14        previous: Option<usize>,
15    },
16    /// Idle state
17    Idle,
18    /// Exiting state
19    ///
20    /// This is to avoid abrupt closure and ask before exit
21    Exiting,
22}
23
24impl App {
25    /// Generate new app instance with default values
26    pub fn new() -> Self {
27        Self {
28            status: Status::Idle,
29        }
30    }
31    /// Change status of the app
32    pub fn switch_status(&mut self, new_status: Status) {
33        self.status = new_status;
34    }
35
36    /// Add a character to the current task being added/changed
37    pub fn add_char(&mut self, ch: char) {
38        if let Status::Editing { edit, previous: _ } = &mut self.status {
39            edit.push(ch);
40        }
41    }
42
43    /// Remove a charecter fromt he current task being added/changed
44    pub fn pop_char(&mut self) {
45        if let Status::Editing { edit, previous: _ } = &mut self.status {
46            edit.pop();
47        }
48    }
49
50    /// Get access to the task which is being written by user while being added
51    pub fn get_editing_task(&self) -> String {
52        if let Status::Editing { edit, previous: _ } = &self.status {
53            edit.to_string()
54        } else {
55            String::new()
56        }
57    }
58
59    // TODO is it needed?
60    pub fn get_prev_task(&self) -> Option<usize> {
61        if let Status::Editing { edit: _, previous } = &self.status {
62            *previous
63        } else {
64            None
65        }
66    }
67}
68
69impl Default for App {
70    fn default() -> Self {
71        Self::new()
72    }
73}