From d286ed3eb877365c01f7a7ec56de9a0dc84bab2e Mon Sep 17 00:00:00 2001 From: sam-anthony Date: Sun, 6 Mar 2022 21:56:57 -0330 Subject: simplify/remove a lot of stuff --- src/app.rs | 90 +------------------------------------ src/input.rs | 6 --- src/main.rs | 109 ++++++++++----------------------------------- src/ui.rs | 67 +++++----------------------- src/unit_of_measurement.rs | 27 +++++++---- 5 files changed, 53 insertions(+), 246 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index bc12812..dedb11e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,82 +1,9 @@ -use crate::input::{InputMode, InputParam, Row}; - -#[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, - } - } -} +use crate::input::{InputParam, Row}; pub struct App { - pub tab: Tab, - tab_titles: Vec, - rows: Vec, selected_row: usize, selected_column: InputParam, - - pub input_mode: InputMode, } impl App { @@ -84,18 +11,6 @@ impl App { &self.rows } - pub fn tab_titles(&self) -> &Vec { - &self.tab_titles - } - - pub fn next_tab(&mut self) { - self.tab = self.tab.next(); - } - - pub fn previous_tab(&mut self) { - self.tab = self.tab.previous(); - } - pub fn next_row(&mut self) { if self.selected_row < self.rows.len() - 1 { self.selected_row += 1; @@ -155,12 +70,9 @@ impl App { impl Default for App { fn default() -> App { App { - 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()), - input_mode: InputMode::Normal, } } } diff --git a/src/input.rs b/src/input.rs index 8f80a1d..6507986 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,9 +1,3 @@ -// Intended to emulate vim modes. -pub enum InputMode { - Normal, // Navigating the ui. - Insert, // Editing a parameter. -} - // A row in the inputs table has one of each variation. #[derive(Clone)] pub enum InputParam { diff --git a/src/main.rs b/src/main.rs index 207dde7..d3901da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,14 +7,9 @@ use std::{error::Error, io}; use tui::{ backend::{Backend, CrosstermBackend}, layout::{Constraint, Direction, Layout}, - widgets::Paragraph, Frame, Terminal, }; -use volute::{ - app::{App, Tab}, - input::InputMode, - ui, -}; +use volute::{app::App, ui}; fn main() -> Result<(), Box> { // setup terminal @@ -50,91 +45,33 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( terminal.draw(|f| ui(f, &app))?; if let Event::Key(key) = event::read()? { - match app.tab { - Tab::Const => match key.code { - KeyCode::Char('q') => { - return Ok(()); - } - KeyCode::Char('L') => app.next_tab(), - KeyCode::Char('H') => app.previous_tab(), - _ => {} - }, - Tab::Input => match app.input_mode { - InputMode::Normal => match key.code { - KeyCode::Char('q') => { - return Ok(()); - } - KeyCode::Char('L') => app.next_tab(), - KeyCode::Char('H') => app.previous_tab(), - KeyCode::Char('j') => app.next_row(), - KeyCode::Char('k') => app.previous_row(), - KeyCode::Char('l') => app.next_column(), - KeyCode::Char('h') => app.previous_column(), - KeyCode::Char('i') => { - app.input_mode = InputMode::Insert; - } - KeyCode::Char('p') => app.insert_row(), - KeyCode::Char('d') => app.remove_row(), - _ => {} - }, - InputMode::Insert => match key.code { - KeyCode::Esc | KeyCode::Enter => { - app.input_mode = InputMode::Normal; - } - KeyCode::Char(c) => { - if ('0'..'a').contains(&c) { - app.selected_input_param_mut().push(c); - } - } - KeyCode::Backspace => app.selected_input_param_mut().pop(), - _ => {} - }, - }, - Tab::Config => match key.code { - KeyCode::Char('q') => { - return Ok(()); + match key.code { + KeyCode::Char('q') => { + return Ok(()); + } + KeyCode::Char('j') => app.next_row(), + KeyCode::Char('k') => app.previous_row(), + KeyCode::Char('l') => app.next_column(), + KeyCode::Char('h') => app.previous_column(), + KeyCode::Char('p') => app.insert_row(), + KeyCode::Char('d') => app.remove_row(), + KeyCode::Char(c) => { + if ('0'..'a').contains(&c) { + app.selected_input_param_mut().push(c); } - KeyCode::Char('L') => app.next_tab(), - KeyCode::Char('H') => app.previous_tab(), - _ => {} - }, + } + KeyCode::Backspace => app.selected_input_param_mut().pop(), + _ => {} } } } } fn ui(f: &mut Frame, app: &App) { - match app.tab { - Tab::Const => { - let layout = Layout::default() - .direction(Direction::Vertical) - .constraints(ui::constraints(app).as_ref()) - .split(f.size()); - f.render_widget(ui::tabs(app), layout[0]); - f.render_widget(Paragraph::new("Const Tab"), layout[1]); - } - Tab::Input => { - let layout = Layout::default() - .direction(Direction::Vertical) - .constraints(ui::constraints(app).as_ref()) - .split(f.size()); - f.render_widget(ui::tabs(app), layout[0]); - f.render_widget(ui::footer(app), layout[3]); - - let table_layout = Layout::default() - .direction(Direction::Horizontal) - .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) - .split(layout[1]); - f.render_widget(ui::input_table(app), table_layout[0]); - f.render_widget(ui::output_table(app), table_layout[1]); - } - Tab::Config => { - let layout = Layout::default() - .direction(Direction::Vertical) - .constraints(ui::constraints(app).as_ref()) - .split(f.size()); - f.render_widget(ui::tabs(app), layout[0]); - f.render_widget(Paragraph::new("Config tab"), layout[1]); - } - } + let layout = Layout::default() + .direction(Direction::Horizontal) + .constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()) + .split(f.size()); + f.render_widget(ui::input_table(app), layout[0]); + f.render_widget(ui::output_table(app), layout[1]); } diff --git a/src/ui.rs b/src/ui.rs index 8505018..9411157 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,58 +1,22 @@ use crate::{ - app::{App, Tab}, - input::InputMode, - unit_of_measurement::pressure::*, + app::App, + unit_of_measurement::{ + pressure::{self, Pressure}, + UnitOfMeasurement, + }, }; use std::ptr; use tui::{ layout::Constraint, - style::{Color, Modifier, Style}, - text::Spans, - widgets::{self, Block, Borders, Cell, Paragraph, Table, Tabs, Widget}, + style::{Color, Style}, + widgets::{self, Block, Borders, Cell, Paragraph, Table, Widget}, }; -pub fn constraints(app: &App) -> Vec { - match app.tab { - Tab::Const | Tab::Config => { - vec![Constraint::Length(3), Constraint::Length(3)] - } - Tab::Input => { - vec![ - Constraint::Length(3), // Tabs - Constraint::Length(app.rows().len() as u16 + 3), // tables - Constraint::Max(100), // Spacer - Constraint::Length(1), // Footer - ] - } - } -} - -pub fn tabs(app: &App) -> impl Widget + '_ { - let titles = app - .tab_titles() - .iter() - .map(|t| Spans::from(t.as_str())) - .collect(); - Tabs::new(titles) - .block(Block::default().borders(Borders::ALL).title("Tabs")) - .select(app.tab as usize) - .highlight_style( - Style::default() - .fg(Color::Yellow) - .add_modifier(Modifier::BOLD), - ) -} - pub fn input_table(app: &App) -> impl Widget { let rows = app.rows().iter().map(|row| { let cells = row.iter().map(|item| { if ptr::eq(item, app.selected_input_param()) { - Cell::from(item.string()).style(match app.input_mode { - InputMode::Normal => Style::default().fg(Color::Yellow), - InputMode::Insert => Style::default() - .fg(Color::Blue) - .add_modifier(Modifier::ITALIC), - }) + Cell::from(item.string()).style(Style::default().fg(Color::Yellow)) } else { Cell::from(item.string()) } @@ -72,20 +36,9 @@ pub fn input_table(app: &App) -> impl Widget { pub fn output_table(app: &App) -> impl Widget { let map = match app.rows()[0].map.string().parse::() { - Ok(p) => Pressure::from_unit(PressureUnit::KiloPascal, p), + Ok(p) => Pressure::from_unit(pressure::Unit::KiloPascal, p), Err(_) => Pressure::default(), }; - Paragraph::new(map.as_unit(PressureUnit::KiloPascal).to_string()) + Paragraph::new(map.as_unit(pressure::Unit::KiloPascal).to_string()) .block(Block::default().title("map").borders(Borders::ALL)) } - -pub fn footer(app: &App) -> impl Widget { - 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)) - } - } -} diff --git a/src/unit_of_measurement.rs b/src/unit_of_measurement.rs index 30a6ff3..a3b4b77 100644 --- a/src/unit_of_measurement.rs +++ b/src/unit_of_measurement.rs @@ -1,22 +1,33 @@ +pub trait UnitOfMeasurement { + type Unit; + + fn from_unit(unit: Self::Unit, n: i32) -> Self; + fn as_unit(&self, unit: Self::Unit) -> i32; +} + pub mod pressure { - pub enum PressureUnit { - Pascal = 1, // base unit. Every other variant will be a multiple of this. - KiloPascal = 1000, - } + use super::UnitOfMeasurement; #[derive(Default)] pub struct Pressure { - val: i32, // Base unit is pascals. + val: i32, } - impl Pressure { - pub fn from_unit(unit: PressureUnit, n: i32) -> Self { + pub enum Unit { + Pascal = 1, + KiloPascal = 1000, + } + + impl UnitOfMeasurement for Pressure { + type Unit = Unit; + + fn from_unit(unit: Self::Unit, n: i32) -> Self { Self { val: n * unit as i32, } } - pub fn as_unit(&self, unit: PressureUnit) -> i32 { + fn as_unit(&self, unit: Self::Unit) -> i32 { self.val / unit as i32 } } -- cgit v1.2.3