From 36928f2351d0eb5fd27ccfe862f2cca21758f663 Mon Sep 17 00:00:00 2001 From: sam-anthony Date: Fri, 4 Feb 2022 15:52:17 -0330 Subject: refactor --- src/main.rs | 182 ++---------------------------------------------------------- 1 file changed, 6 insertions(+), 176 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 3d13648..3ba8c0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,182 +12,10 @@ use tui::{ widgets::{self, Block, Borders, Cell, Paragraph, Table, Tabs, Widget}, 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 { - tab_index: usize, - tab_titles: Vec<&'static str>, - - rows: Vec, - selected_row: usize, - selected_column: InputParam, - - input_mode: InputMode, -} -impl App { - fn next_tab(&mut self) { - self.tab_index = (self.tab_index + 1) % self.tab_titles.len(); - } - - fn previous_tab(&mut self) { - if self.tab_index > 0 { - self.tab_index -= 1; - } else { - self.tab_index = self.tab_titles.len() - 1; - } - } - - fn next_row(&mut self) { - if self.selected_row < self.rows.len() - 1 { - self.selected_row += 1; - } else { - self.selected_row = 0; - } - } - - fn previous_row(&mut self) { - if self.selected_row > 0 { - self.selected_row -= 1; - } else { - self.selected_row = self.rows.len() - 1; - } - } - - fn next_column(&mut self) { - self.selected_column = self.selected_column.next(); - } - - fn previous_column(&mut self) { - self.selected_column = self.selected_column.previous(); - } - - fn insert_row(&mut self) { - let index = self.selected_row; - self.rows.insert(index, self.rows[index].clone()); - } - - 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: 0, - tab_titles: vec!["Input", "Config"], - rows: vec![Row::default()], - selected_row: 0, - selected_column: InputParam::Rpm(String::new()), - input_mode: InputMode::Normal, - } - } -} +use volute::{ + app::App, + input::{InputMode, InputParam}, +}; fn main() -> Result<(), Box> { // setup terminal @@ -283,6 +111,7 @@ fn ui(f: &mut Frame, app: &App) { .highlight_style(Style::default().add_modifier(Modifier::BOLD)); match app.tab_index { + // Input tab 0 => { let layout = Layout::default() .direction(Direction::Vertical) @@ -299,6 +128,7 @@ fn ui(f: &mut Frame, app: &App) { f.render_widget(input_table(app), layout[1]); f.render_widget(footer(app), layout[2]); } + // Config tab 1 => { let layout = Layout::default() .direction(Direction::Vertical) -- cgit v1.2.3