From d0c7b0a39ed6703940da4896d2caa79ad36590e0 Mon Sep 17 00:00:00 2001 From: sam-anthony Date: Wed, 9 Feb 2022 17:54:34 -0330 Subject: small tweaks --- src/app.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 77 insertions(+), 15 deletions(-) (limited to 'src/app.rs') diff --git a/src/app.rs b/src/app.rs index 92f89c0..bc12812 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,11 +1,76 @@ use crate::input::{InputMode, InputParam, Row}; -pub const INPUT_TAB_INDEX: usize = 0; -pub const CONFIG_TAB_INDEX: usize = 1; +#[derive(Copy, Clone)] +pub enum Tab { + Const = 0, + Input = 1, + Config = 2, +} + +impl Tab { + fn next(&self) -> Self { + match self { + Self::Const => Self::Input, + Self::Input => Self::Config, + Self::Config => Self::Const, + } + } + + fn previous(&self) -> Self { + match self { + Self::Const => Self::Config, + Self::Input => Self::Const, + Self::Config => Self::Input, + } + } + + fn string(&self) -> String { + match self { + Self::Const => "Const".to_string(), + Self::Input => "Input".to_string(), + Self::Config => "Config".to_string(), + } + } +} + +impl IntoIterator for Tab { + type Item = Tab; + type IntoIter = TabIter; + + fn into_iter(self) -> Self::IntoIter { + TabIter { tab: Some(self) } + } +} + +pub struct TabIter { + tab: Option, +} + +impl Iterator for TabIter { + type Item = Tab; + + fn next(&mut self) -> Option { + match self.tab { + Some(Tab::Const) => { + self.tab = Some(Tab::Input); + Some(Tab::Const) + } + Some(Tab::Input) => { + self.tab = Some(Tab::Config); + Some(Tab::Input) + } + Some(Tab::Config) => { + self.tab = None; + Some(Tab::Config) + } + None => None, + } + } +} pub struct App { - pub tab_index: usize, - tab_titles: [&'static str; 2], + pub tab: Tab, + tab_titles: Vec, rows: Vec, selected_row: usize, @@ -19,20 +84,16 @@ impl App { &self.rows } - pub fn tab_titles(&self) -> &[&str] { + pub fn tab_titles(&self) -> &Vec { &self.tab_titles } pub fn next_tab(&mut self) { - self.tab_index = (self.tab_index + 1) % self.tab_titles.len(); + self.tab = self.tab.next(); } pub fn previous_tab(&mut self) { - if self.tab_index > 0 { - self.tab_index -= 1; - } else { - self.tab_index = self.tab_titles.len() - 1; - } + self.tab = self.tab.previous(); } pub fn next_row(&mut self) { @@ -67,8 +128,9 @@ impl App { 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; + // If we remove the last row, the selected row will be out of range. + if self.selected_row >= self.rows.len() { + self.selected_row = self.rows.len() - 1; } } } @@ -93,8 +155,8 @@ impl App { impl Default for App { fn default() -> App { App { - tab_index: INPUT_TAB_INDEX, - tab_titles: ["Input", "Config"], + tab: Tab::Const, + tab_titles: Tab::Const.into_iter().map(|t| t.string()).collect(), rows: vec![Row::default()], selected_row: 0, selected_column: InputParam::Rpm(String::new()), -- cgit v1.2.3