aboutsummaryrefslogtreecommitdiffstats
path: root/src/app.rs
blob: 25002bdee777ba3f71c0befcc99603e0c1c291c5 (plain) (blame)
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
use crate::input::{InputMode, InputParam, Row};

pub const INPUT_TAB_INDEX: usize = 0;
pub const CONFIG_TAB_INDEX: usize = 1;

pub struct App {
    pub tab_index: usize,
    tab_titles: [&'static str; 2],

    pub rows: Vec<Row>,
    pub selected_row: usize,
    pub selected_column: InputParam,

    pub input_mode: InputMode,
}

impl App {
    pub fn tab_titles(&self) -> &[&str] {
        &self.tab_titles
    }

    pub fn next_tab(&mut self) {
        self.tab_index = (self.tab_index + 1) % self.tab_titles.len();
    }

    pub fn previous_tab(&mut self) {
        if self.tab_index > 0 {
            self.tab_index -= 1;
        } else {
            self.tab_index = self.tab_titles.len() - 1;
        }
    }

    pub fn next_row(&mut self) {
        if self.selected_row < self.rows.len() - 1 {
            self.selected_row += 1;
        } else {
            self.selected_row = 0;
        }
    }

    pub fn previous_row(&mut self) {
        if self.selected_row > 0 {
            self.selected_row -= 1;
        } else {
            self.selected_row = self.rows.len() - 1;
        }
    }

    pub fn next_column(&mut self) {
        self.selected_column = self.selected_column.next();
    }

    pub fn previous_column(&mut self) {
        self.selected_column = self.selected_column.previous();
    }

    pub fn insert_row(&mut self) {
        let index = self.selected_row;
        self.rows.insert(index, self.rows[index].clone());
    }

    pub fn remove_row(&mut self) {
        if self.rows.len() > 1 {
            self.rows.remove(self.selected_row);
            if self.selected_row > 0 {
                self.selected_row -= 1;
            }
        }
    }
}

impl Default for App {
    fn default() -> App {
        App {
            tab_index: INPUT_TAB_INDEX,
            tab_titles: ["Input", "Config"],
            rows: vec![Row::default()],
            selected_row: 0,
            selected_column: InputParam::Rpm(String::new()),
            input_mode: InputMode::Normal,
        }
    }
}