aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 3ca86729e056f6ffe3e66b344012348ccee2e99a (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
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
use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use std::{error::Error, io};
use tui::{
    backend::{Backend, CrosstermBackend},
    layout::{Constraint, Direction, Layout},
    style::{Color, Modifier, Style},
    widgets::{self, Block, Borders, Cell, Paragraph, Table},
    Frame, Terminal,
};

// Intended to emulate vim modes.
enum InputMode {
    Normal, // Navigating the ui.
    Insert, // Editing a parameter.
}

// A row in the inputs table has one of each variation.
#[derive(Clone)]
enum InputParam {
    Rpm(String), // Revolutions per minute
    Ve(String),  // Volumetric efficiency
    Map(String), // Manifold absolute pressure
}
impl InputParam {
    /* Acts like the push() method of a Vec.
     * Appends the given char to the end of the string contained by the
     * InputParam.
     */
    fn push(&mut self, c: char) {
        match self {
            Self::Rpm(rpm) => {
                rpm.push(c);
                *self = Self::Rpm(rpm.to_string());
            }
            Self::Ve(ve) => {
                ve.push(c);
                *self = Self::Ve(ve.to_string());
            }
            Self::Map(map) => {
                map.push(c);
                *self = Self::Map(map.to_string());
            }
        }
    }
    /* Acts like the pop() method of a Vec.
     * Removes the last char from the string contained by the InputParam.
     */
    fn pop(&mut self) {
        match self {
            Self::Rpm(rpm) => {
                rpm.pop();
                *self = Self::Rpm(rpm.to_string());
            }
            Self::Ve(ve) => {
                ve.pop();
                *self = Self::Rpm(ve.to_string());
            }
            Self::Map(map) => {
                map.pop();
                *self = Self::Map(map.to_string());
            }
        }
    }
    // Return a copy of the string contained by the InputParam.
    fn string(&self) -> String {
        match self {
            Self::Rpm(rpm) => rpm.to_string(),
            Self::Ve(ve) => ve.to_string(),
            Self::Map(map) => map.to_string(),
        }
    }
    /* next() and previous() allow InputParam to act as a circular iterator of
     * sorts. next() will return the next variation as they are defined. When
     * it reaches the end, the first variation will be returned:
     *     RPM->VE->MAP->RPM->etc...
     * previous() simply goes the opposite direction:
     *     MAP->VE->RPM->MAP->etc...
     */
    fn next(&self) -> Self {
        match self {
            Self::Rpm(_) => Self::Ve(String::new()),
            Self::Ve(_) => Self::Map(String::new()),
            Self::Map(_) => Self::Rpm(String::new()),
        }
    }
    fn previous(&self) -> Self {
        match self {
            Self::Rpm(_) => Self::Map(String::new()),
            Self::Ve(_) => Self::Rpm(String::new()),
            Self::Map(_) => Self::Ve(String::new()),
        }
    }
}

// A row in the inputs table. Contains one of each variation of InputParam.
#[derive(Clone)]
struct Row {
    rpm: InputParam,
    ve: InputParam,
    map: InputParam,
}
impl Default for Row {
    fn default() -> Self {
        Self {
            rpm: InputParam::Rpm(String::from("7000")),
            ve: InputParam::Ve(String::from("95")),
            map: InputParam::Map(String::from("200")),
        }
    }
}

// Holds the state of the application.
struct App {
    rows: Vec<Row>,

    selected_row: usize,
    selected_column: InputParam,

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

fn main() -> Result<(), Box<dyn Error>> {
    // setup terminal
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    // create app and run it
    let app = App::default();
    let res = run_app(&mut terminal, app);

    // restore terminal
    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    if let Err(err) = res {
        println!("{:?}", err)
    }

    Ok(())
}

// Input handling
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
    loop {
        terminal.draw(|f| ui(f, &app))?;

        if let Event::Key(key) = event::read()? {
            match app.input_mode {
                // Navigating
                InputMode::Normal => match key.code {
                    // Enter insert mode
                    KeyCode::Char('i') => {
                        app.input_mode = InputMode::Insert;
                    }
                    // Quit
                    KeyCode::Char('q') => {
                        return Ok(());
                    }
                    // Navigate up
                    KeyCode::Char('k') => {
                        if app.selected_row > 0 {
                            app.selected_row -= 1;
                        } else {
                            app.selected_row = app.rows.len() - 1;
                        }
                    }
                    // Navigate down
                    KeyCode::Char('j') => {
                        if app.selected_row < app.rows.len() - 1 {
                            app.selected_row += 1;
                        } else {
                            app.selected_row = 0;
                        }
                    }
                    // Navigate right
                    KeyCode::Char('l') => {
                        app.selected_column = app.selected_column.next();
                    }
                    // Navigate left
                    KeyCode::Char('h') => {
                        app.selected_column = app.selected_column.previous();
                    }
                    // Add row
                    KeyCode::Char('p') => {
                        app.rows
                            .insert(app.selected_row, app.rows[app.selected_row].clone());
                    }
                    // Remove row
                    KeyCode::Char('d') => {
                        if app.rows.len() > 1 {
                            app.rows.remove(app.selected_row);
                            if app.selected_row > 0 {
                                app.selected_row -= 1;
                            }
                        }
                    }
                    _ => {}
                },
                InputMode::Insert => match key.code {
                    // Exit insert mode
                    KeyCode::Esc => {
                        app.input_mode = InputMode::Normal;
                    }
                    // Exit insert mode
                    KeyCode::Enter => {
                        app.input_mode = InputMode::Normal;
                    }
                    // Append a caracter to the currently selected parameter.
                    KeyCode::Char(c) => match app.selected_column {
                        InputParam::Rpm(_) => {
                            app.rows[app.selected_row].rpm.push(c);
                        }
                        InputParam::Ve(_) => {
                            app.rows[app.selected_row].ve.push(c);
                        }
                        InputParam::Map(_) => {
                            app.rows[app.selected_row].map.push(c);
                        }
                    },
                    // Remove a character from the currently selected parameter.
                    KeyCode::Backspace => match app.selected_column {
                        InputParam::Rpm(_) => {
                            app.rows[app.selected_row].rpm.pop();
                        }
                        InputParam::Ve(_) => {
                            app.rows[app.selected_row].ve.pop();
                        }
                        InputParam::Map(_) => {
                            app.rows[app.selected_row].map.pop();
                        }
                    },

                    _ => {}
                },
            }
        }
    }
}

fn ui<B: Backend>(f: &mut Frame<B>, app: &App) {
    let layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints(
            [
                Constraint::Min(app.rows.len() as u16 + 2),
                Constraint::Length(1),
            ]
            .as_ref(),
        )
        .split(f.size());

    /* This is used so I can have named fields instead of indexing a vector of
     * Cells when styling the selected input parameter.
     */
    struct VirtualRow<'a> {
        rpm: Cell<'a>,
        ve: Cell<'a>,
        map: Cell<'a>,
    }

    let mut rows: Vec<VirtualRow> = app
        .rows
        .iter()
        .map(|row| VirtualRow {
            rpm: Cell::from(row.rpm.string()),
            ve: Cell::from(row.ve.string()),
            map: Cell::from(row.map.string()),
        })
        .collect();

    // Highlight the selected parameter
    let selected_parameter = match app.selected_column {
        InputParam::Rpm(_) => &mut rows[app.selected_row].rpm,
        InputParam::Ve(_) => &mut rows[app.selected_row].ve,
        InputParam::Map(_) => &mut rows[app.selected_row].map,
    };
    *selected_parameter = selected_parameter.clone().style(match app.input_mode {
        InputMode::Normal => Style::default().fg(Color::Yellow),
        InputMode::Insert => Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::ITALIC),
    });

    let table = Table::new(
        rows.iter()
            .map(|row| widgets::Row::new(vec![row.rpm.clone(), row.ve.clone(), row.map.clone()]))
            .collect::<Vec<widgets::Row>>(),
    )
    .header(widgets::Row::new(vec!["rpm", "ve", "map"]))
    .block(Block::default().borders(Borders::ALL).title("Table"))
    .widths(&[
        Constraint::Length(5),
        Constraint::Length(3),
        Constraint::Length(3),
    ]);
    f.render_widget(table, layout[0]);

    let footer = match app.input_mode {
        InputMode::Normal => {
            Paragraph::new("Normal").style(Style::default().fg(Color::Black).bg(Color::Yellow))
        }
        InputMode::Insert => {
            Paragraph::new("Insert").style(Style::default().fg(Color::Black).bg(Color::Blue))
        }
    };
    f.render_widget(footer, layout[1]);
}