add grid_max
This commit is contained in:
@@ -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::<f64>() {
|
||||
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::<CellType>(self.grid.selected_cell, None);
|
||||
}
|
||||
}
|
||||
|
||||
self.mode = Mode::Normal;
|
||||
|
||||
@@ -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<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) {
|
||||
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
|
||||
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.));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
@@ -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::<CellType>((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::<CellType>(loc, None);
|
||||
app.mode = Mode::Normal;
|
||||
}
|
||||
"gg" => {
|
||||
|
||||
Reference in New Issue
Block a user