aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsam-anthony <samanthony6@protonmail.com>2022-02-04 15:52:17 -0330
committersam-anthony <samanthony6@protonmail.com>2022-02-04 15:52:17 -0330
commit36928f2351d0eb5fd27ccfe862f2cca21758f663 (patch)
tree9b4858d82822b2eb8b46534a050b39b319e700a7
parent02ecdb28dfe91b625f8bf51e0fbd2504a3731344 (diff)
downloadvolute-36928f2351d0eb5fd27ccfe862f2cca21758f663.zip
refactor
-rw-r--r--src/app.rs77
-rw-r--r--src/input.rs100
-rw-r--r--src/lib.rs2
-rw-r--r--src/main.rs182
4 files changed, 185 insertions, 176 deletions
diff --git a/src/app.rs b/src/app.rs
new file mode 100644
index 0000000..eaaccc8
--- /dev/null
+++ b/src/app.rs
@@ -0,0 +1,77 @@
+use crate::input::{InputMode, InputParam, Row};
+
+pub struct App {
+ pub tab_index: usize,
+ pub tab_titles: Vec<&'static str>,
+
+ pub rows: Vec<Row>,
+ pub selected_row: usize,
+ pub selected_column: InputParam,
+
+ pub input_mode: InputMode,
+}
+
+impl App {
+ 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: 0,
+ tab_titles: vec!["Input", "Config"],
+ 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
new file mode 100644
index 0000000..f6b1459
--- /dev/null
+++ b/src/input.rs
@@ -0,0 +1,100 @@
+// 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 {
+ 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.
+ */
+ pub 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.
+ */
+ pub 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.
+ pub 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...
+ */
+ pub fn next(&self) -> Self {
+ match self {
+ Self::Rpm(_) => Self::Ve(String::new()),
+ Self::Ve(_) => Self::Map(String::new()),
+ Self::Map(_) => Self::Rpm(String::new()),
+ }
+ }
+ pub 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)]
+pub struct Row {
+ pub rpm: InputParam,
+ pub ve: InputParam,
+ pub 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")),
+ }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 8fd6b17..1646e17 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,5 @@
+pub mod app;
+pub mod input;
pub mod unit_of_measurement;
use crate::unit_of_measurement::{Pressure, Temperature, Volume};
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<Row>,
- 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<dyn Error>> {
// setup terminal
@@ -283,6 +111,7 @@ fn ui<B: Backend>(f: &mut Frame<B>, 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<B: Backend>(f: &mut Frame<B>, 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)