add grid_max

This commit is contained in:
2025-11-12 08:39:25 -07:00
parent 8a682376d9
commit 37463f46c3
3 changed files with 63 additions and 10 deletions

View File

@@ -246,10 +246,16 @@ impl App {
event::KeyCode::Enter => { event::KeyCode::Enter => {
let v = editor.as_string(); let v = editor.as_string();
// try to insert as a float
if let Ok(v) = v.parse::<f64>() { if let Ok(v) = v.parse::<f64>() {
self.grid.set_cell_raw(self.grid.selected_cell, v); self.grid.set_cell_raw(self.grid.selected_cell, Some(v));
} else { } 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::<CellType>(self.grid.selected_cell, None);
}
} }
self.mode = Mode::Normal; self.mode = Mode::Normal;

View File

@@ -1,6 +1,7 @@
use std::fmt::Display; use std::fmt::Display;
use evalexpr::*; use evalexpr::*;
use ratatui::buffer::Cell;
use crate::app::logic::ctx; use crate::app::logic::ctx;
@@ -27,6 +28,30 @@ impl std::fmt::Debug for Grid {
} }
impl 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 { pub fn new() -> Self {
let mut a = Vec::with_capacity(LEN); let mut a = Vec::with_capacity(LEN);
for _ in 0..LEN { for _ in 0..LEN {
@@ -110,15 +135,17 @@ impl Grid {
} }
pub fn set_cell<T: Into<CellType>>(&mut self, cell_id: &str, val: T) { /// Helper for tests
#[cfg(test)]
fn set_cell<T: Into<CellType>>(&mut self, cell_id: &str, val: T) {
if let Some(loc) = Self::parse_to_idx(cell_id) { 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<T: Into<CellType>>(&mut self, (x,y): (usize, usize), val: T) { pub fn set_cell_raw<T: Into<CellType>>(&mut self, (x,y): (usize, usize), val: Option<T>) {
// TODO check oob // TODO check oob
self.cells[x][y] = Some(val.into()); self.cells[x][y] = val.map(|v| v.into());
} }
/// Get cells via text like: /// Get cells via text like:
@@ -333,3 +360,23 @@ fn invalid_equations() {
assert!(res.is_some_and(|v| v == 10.)); assert!(res.is_some_and(|v| v == 10.));
} }
#[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);
}

View File

@@ -7,7 +7,7 @@ use ratatui::{
prelude, style::{Color, Style}, widgets::{Paragraph, Widget} 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 { pub enum Mode {
Insert(Chord), Insert(Chord),
@@ -142,7 +142,7 @@ impl Mode {
if key == 'd' { if key == 'd' {
for x in low_x..=hi_x { for x in low_x..=hi_x {
for y in low_y..=hi_y { for y in low_y..=hi_y {
app.grid.set_cell_raw((x, y), String::new()); app.grid.set_cell_raw::<CellType>((x, y), None);
} }
} }
app.mode = Mode::Normal app.mode = Mode::Normal
@@ -179,7 +179,7 @@ impl Mode {
Err(_) => match chord.as_string().as_str() { Err(_) => match chord.as_string().as_str() {
"d " | "dw" => { "d " | "dw" => {
let loc = app.grid.selected_cell; let loc = app.grid.selected_cell;
app.grid.set_cell_raw(loc, String::new()); app.grid.set_cell_raw::<CellType>(loc, None);
app.mode = Mode::Normal; app.mode = Mode::Normal;
} }
"gg" => { "gg" => {