From 37463f46c3426eceafd96c77edbde742ade09fa6 Mon Sep 17 00:00:00 2001 From: Rushmore75 Date: Wed, 12 Nov 2025 08:39:25 -0700 Subject: [PATCH] add grid_max --- src/app/app.rs | 10 ++++++-- src/app/logic/calc.rs | 57 +++++++++++++++++++++++++++++++++++++++---- src/app/mode.rs | 6 ++--- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/app/app.rs b/src/app/app.rs index 9923763..e0b8cda 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -246,10 +246,16 @@ impl App { event::KeyCode::Enter => { let v = editor.as_string(); + // try to insert as a float if let Ok(v) = v.parse::() { - self.grid.set_cell_raw(self.grid.selected_cell, v); + self.grid.set_cell_raw(self.grid.selected_cell, Some(v)); } else { - self.grid.set_cell_raw(self.grid.selected_cell, v); + // if you can't, then insert as a string + if !v.is_empty() { + self.grid.set_cell_raw(self.grid.selected_cell, Some(v)); + } else { + self.grid.set_cell_raw::(self.grid.selected_cell, None); + } } self.mode = Mode::Normal; diff --git a/src/app/logic/calc.rs b/src/app/logic/calc.rs index 7f11501..fb1fed1 100644 --- a/src/app/logic/calc.rs +++ b/src/app/logic/calc.rs @@ -1,6 +1,7 @@ use std::fmt::Display; use evalexpr::*; +use ratatui::buffer::Cell; use crate::app::logic::ctx; @@ -27,6 +28,30 @@ impl std::fmt::Debug for Grid { } impl Grid { + + /// Iterate over the entire grid and see where + /// the farthest modified cell is. + #[must_use] + pub fn max(&self) -> (usize, usize) { + let mut max_x = 0; + let mut max_y = 0; + + for (xi, x) in self.cells.iter().enumerate() { + for (yi, cell) in x.iter().enumerate() { + if cell.is_some() { + if yi > max_y { + max_y = yi + } + if xi > max_x { + max_x = xi + } + } + } + } + + (max_x, max_y) + } + pub fn new() -> Self { let mut a = Vec::with_capacity(LEN); for _ in 0..LEN { @@ -110,15 +135,17 @@ impl Grid { } - pub fn set_cell>(&mut self, cell_id: &str, val: T) { + /// Helper for tests + #[cfg(test)] + fn set_cell>(&mut self, cell_id: &str, val: T) { if let Some(loc) = Self::parse_to_idx(cell_id) { - self.set_cell_raw(loc, val); + self.set_cell_raw(loc, Some(val)); } } - pub fn set_cell_raw>(&mut self, (x,y): (usize, usize), val: T) { + pub fn set_cell_raw>(&mut self, (x,y): (usize, usize), val: Option) { // TODO check oob - self.cells[x][y] = Some(val.into()); + self.cells[x][y] = val.map(|v| v.into()); } /// Get cells via text like: @@ -332,4 +359,24 @@ fn invalid_equations() { assert!(res.is_some()); assert!(res.is_some_and(|v| v == 10.)); -} \ No newline at end of file +} + +#[test] +fn grid_max() { + let mut grid = Grid::new(); + + grid.set_cell("A0", 1.); + let (mx, my) = grid.max(); + assert_eq!(mx, 0); + assert_eq!(my, 0); + + grid.set_cell("B0", 1.); + let (mx, my) = grid.max(); + assert_eq!(mx, 1); + assert_eq!(my, 0); + + grid.set_cell("B5", 1.); + let (mx, my) = grid.max(); + assert_eq!(mx, 1); + assert_eq!(my, 5); +} diff --git a/src/app/mode.rs b/src/app/mode.rs index 6fcda5e..601a95f 100644 --- a/src/app/mode.rs +++ b/src/app/mode.rs @@ -7,7 +7,7 @@ use ratatui::{ prelude, style::{Color, Style}, widgets::{Paragraph, Widget} }; -use crate::app::{app::App, logic::calc::LEN, error_msg::ErrorMessage}; +use crate::app::{app::App, error_msg::ErrorMessage, logic::calc::{CellType, LEN}}; pub enum Mode { Insert(Chord), @@ -142,7 +142,7 @@ impl Mode { if key == 'd' { for x in low_x..=hi_x { for y in low_y..=hi_y { - app.grid.set_cell_raw((x, y), String::new()); + app.grid.set_cell_raw::((x, y), None); } } app.mode = Mode::Normal @@ -179,7 +179,7 @@ impl Mode { Err(_) => match chord.as_string().as_str() { "d " | "dw" => { let loc = app.grid.selected_cell; - app.grid.set_cell_raw(loc, String::new()); + app.grid.set_cell_raw::(loc, None); app.mode = Mode::Normal; } "gg" => {