Compare commits
4 Commits
d983995e8f
...
53dcf2ffc9
| Author | SHA1 | Date | |
|---|---|---|---|
| 53dcf2ffc9 | |||
| 86756a94ef | |||
| d242e1af21 | |||
| abffe6073f |
@@ -2,20 +2,21 @@ use std::{
|
|||||||
cmp::{max, min},
|
cmp::{max, min},
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io::{Read, Write},
|
io::{Read, Write},
|
||||||
path::PathBuf
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
|
|
||||||
use evalexpr::*;
|
use evalexpr::*;
|
||||||
|
|
||||||
use crate::app::{
|
use crate::app::{
|
||||||
logic::{
|
logic::{
|
||||||
cell::{CSV_DELIMITER, CellType},
|
calc::internal::CellGrid, cell::{CSV_DELIMITER, CellType}, ctx
|
||||||
ctx,
|
}
|
||||||
}, mode::Mode,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use crate::app::app::App;
|
use crate::app::app::App;
|
||||||
|
#[cfg(test)]
|
||||||
|
use crate::app::mode::Mode;
|
||||||
|
|
||||||
pub fn get_header_size() -> usize {
|
pub fn get_header_size() -> usize {
|
||||||
let row_header_width = LEN.to_string().len();
|
let row_header_width = LEN.to_string().len();
|
||||||
@@ -26,13 +27,109 @@ pub const LEN: usize = 1001;
|
|||||||
pub const CSV_EXT: &str = "csv";
|
pub const CSV_EXT: &str = "csv";
|
||||||
pub const CUSTOM_EXT: &str = "nscim";
|
pub const CUSTOM_EXT: &str = "nscim";
|
||||||
|
|
||||||
|
mod internal {
|
||||||
|
use crate::app::logic::{calc::LEN, cell::CellType};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct CellGrid {
|
||||||
|
// a b c ...
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
|
// 2
|
||||||
|
// ...
|
||||||
|
cells: Vec<Vec<Option<CellType>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CellGrid {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let mut a = Vec::with_capacity(LEN);
|
||||||
|
for _ in 0..LEN {
|
||||||
|
let mut b = Vec::with_capacity(LEN);
|
||||||
|
for _ in 0..LEN {
|
||||||
|
b.push(None)
|
||||||
|
}
|
||||||
|
a.push(b)
|
||||||
|
}
|
||||||
|
Self { cells: a }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_row(&mut self, y: usize) {
|
||||||
|
for x in 0..LEN {
|
||||||
|
self.cells[x].insert(y, None);
|
||||||
|
self.cells[x].pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_column(&mut self, x: usize) {
|
||||||
|
let mut v = Vec::with_capacity(LEN);
|
||||||
|
for _ in 0..LEN {
|
||||||
|
v.push(None);
|
||||||
|
}
|
||||||
|
// let clone = self.grid_history[self.current_grid].clone();
|
||||||
|
self.cells.insert(x, v);
|
||||||
|
// keep the grid LEN
|
||||||
|
self.cells.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_cell_raw(&self, x: usize, y: usize) -> &Option<CellType> {
|
||||||
|
if x >= LEN || y >= LEN {
|
||||||
|
return &None;
|
||||||
|
}
|
||||||
|
&self.cells[x][y]
|
||||||
|
}
|
||||||
|
pub fn set_cell_raw<T: Into<CellType>>(&mut self, (x, y): (usize, usize), val: Option<T>) {
|
||||||
|
// TODO check oob
|
||||||
|
self.cells[x][y] = val.map(|v| v.into());
|
||||||
|
}
|
||||||
|
/// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn max_y_at_x(&self, x: usize) -> usize {
|
||||||
|
let mut max_y = 0;
|
||||||
|
if let Some(col) = self.cells.get(x) {
|
||||||
|
// we could do fancy things like .take_while(not null) but then
|
||||||
|
// we would have to deal with empty cells and stuff, which sounds
|
||||||
|
// boring. This will be fast "enough", considering the grid is
|
||||||
|
// probably only like 1k cells
|
||||||
|
for (yi, cell) in col.iter().enumerate() {
|
||||||
|
if cell.is_some() {
|
||||||
|
if yi > max_y {
|
||||||
|
max_y = yi
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
max_y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Grid {
|
pub struct Grid {
|
||||||
// a b c ...
|
/// Which grid in history are we currently on
|
||||||
// 0
|
current_grid: usize,
|
||||||
// 1
|
/// An array of grids, thru history
|
||||||
// 2
|
grid_history: Vec<CellGrid>,
|
||||||
// ...
|
|
||||||
cells: Vec<Vec<Option<CellType>>>,
|
|
||||||
/// (X, Y)
|
/// (X, Y)
|
||||||
selected_cell: (usize, usize),
|
selected_cell: (usize, usize),
|
||||||
/// Have unsaved modifications been made?
|
/// Have unsaved modifications been made?
|
||||||
@@ -47,20 +144,9 @@ impl std::fmt::Debug for Grid {
|
|||||||
|
|
||||||
impl Grid {
|
impl Grid {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut a = Vec::with_capacity(LEN);
|
let x = CellGrid::new();
|
||||||
for _ in 0..LEN {
|
|
||||||
let mut b = Vec::with_capacity(LEN);
|
|
||||||
for _ in 0..LEN {
|
|
||||||
b.push(None)
|
|
||||||
}
|
|
||||||
a.push(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
Self {
|
Self { current_grid: 0, grid_history: vec![x], selected_cell: (0, 0), dirty: false }
|
||||||
cells: a,
|
|
||||||
selected_cell: (0, 0),
|
|
||||||
dirty: false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_from_file(file: &mut File) -> std::io::Result<Self> {
|
pub fn new_from_file(file: &mut File) -> std::io::Result<Self> {
|
||||||
@@ -112,19 +198,15 @@ impl Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut f = fs::OpenOptions::new().write(true).append(false).truncate(true).create(true).open(path)?;
|
let mut f = fs::OpenOptions::new().write(true).append(false).truncate(true).create(true).open(path)?;
|
||||||
let (mx, my) = self.max();
|
let (mx, my) = self.get_grid().max();
|
||||||
for y in 0..=my {
|
for y in 0..=my {
|
||||||
for x in 0..=mx {
|
for x in 0..=mx {
|
||||||
let cell = &self.cells[x][y];
|
let cell = &self.get_grid().get_cell_raw(x, y);
|
||||||
|
|
||||||
// newline after the cell, because it's end of line.
|
// newline after the cell, because it's end of line.
|
||||||
// else, just put a comma after the cell.
|
// else, just put a comma after the cell.
|
||||||
let is_last = x==mx;
|
let is_last = x == mx;
|
||||||
let delim = if is_last {
|
let delim = if is_last { '\n' } else { CSV_DELIMITER };
|
||||||
'\n'
|
|
||||||
} else {
|
|
||||||
CSV_DELIMITER
|
|
||||||
};
|
|
||||||
|
|
||||||
let data = if let Some(cell) = cell {
|
let data = if let Some(cell) = cell {
|
||||||
if let Ok(val) = self.evaluate(&cell.to_string())
|
if let Ok(val) = self.evaluate(&cell.to_string())
|
||||||
@@ -146,6 +228,40 @@ impl Grid {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_grid<'a>(&'a self) -> &'a CellGrid {
|
||||||
|
&self.grid_history[self.current_grid]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_grid_mut<'a>(&'a mut self) -> &'a mut CellGrid {
|
||||||
|
&mut self.grid_history[self.current_grid]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn undo(&mut self) {
|
||||||
|
self.current_grid.saturating_sub(1);
|
||||||
|
}
|
||||||
|
fn redo(&mut self) {
|
||||||
|
self.current_grid += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transact_on_grid<F>(&mut self, mut action: F)
|
||||||
|
where
|
||||||
|
F: FnMut(&mut CellGrid) -> (),
|
||||||
|
{
|
||||||
|
// push on a new reality
|
||||||
|
let new = self.get_grid().clone();
|
||||||
|
self.grid_history.push(new);
|
||||||
|
self.current_grid += 1;
|
||||||
|
|
||||||
|
// delete the other fork of the history
|
||||||
|
for i in self.current_grid+1..self.grid_history.len() {
|
||||||
|
self.grid_history.remove(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
action(&mut self.grid_history[self.current_grid]);
|
||||||
|
|
||||||
|
self.dirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn needs_to_be_saved(&self) -> bool {
|
pub fn needs_to_be_saved(&self) -> bool {
|
||||||
self.dirty
|
self.dirty
|
||||||
}
|
}
|
||||||
@@ -252,97 +368,55 @@ impl Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_row_above(&mut self, (_x, insertion_y): (usize, usize)) {
|
pub fn insert_row_above(&mut self, (_x, insertion_y): (usize, usize)) {
|
||||||
for x in 0..LEN {
|
self.transact_on_grid(|grid: &mut CellGrid| {
|
||||||
self.cells[x].insert(insertion_y, None);
|
grid.insert_row(insertion_y);
|
||||||
self.cells[x].pop();
|
for x in 0..LEN {
|
||||||
for y in 0..LEN {
|
for y in 0..LEN {
|
||||||
if let Some(cell) = self.get_cell_raw(x, y).as_ref().map(|f| {
|
if let Some(cell) = grid.get_cell_raw(x, y).as_ref().map(|f| {
|
||||||
f.custom_translate_cell((0, 0), (0, 1), |rolling, old, new| {
|
f.custom_translate_cell((0, 0), (0, 1), |rolling, old, new| {
|
||||||
if let Some((_, arg_y)) = Grid::parse_to_idx(old) {
|
if let Some((_, arg_y)) = Grid::parse_to_idx(old) {
|
||||||
if arg_y < insertion_y { rolling.to_owned() } else { rolling.replace(old, new) }
|
if arg_y < insertion_y { rolling.to_owned() } else { rolling.replace(old, new) }
|
||||||
} else {
|
} else {
|
||||||
unimplemented!("Invalid variable wanted to be translated")
|
unimplemented!("Invalid variable wanted to be translated")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}) {
|
||||||
|
grid.set_cell_raw((x, y), Some(cell));
|
||||||
}
|
}
|
||||||
)}) {
|
|
||||||
self.set_cell_raw((x,y), Some(cell));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_row_below(&mut self, (x, y): (usize, usize)) {
|
pub fn insert_row_below(&mut self, (x, y): (usize, usize)) {
|
||||||
self.insert_row_above((x,y+1));
|
self.insert_row_above((x, y + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_column_before(&mut self, (insertion_x, _y): (usize, usize)) {
|
pub fn insert_column_before(&mut self, (insertion_x, _y): (usize, usize)) {
|
||||||
let mut v = Vec::with_capacity(LEN);
|
self.transact_on_grid(|grid| {
|
||||||
for _ in 0..LEN {
|
grid.insert_column(insertion_x);
|
||||||
v.push(None);
|
for x in 0..LEN {
|
||||||
}
|
for y in 0..LEN {
|
||||||
self.cells.insert(insertion_x, v);
|
if let Some(cell) = grid.get_cell_raw(x, y).as_ref().map(|f| {
|
||||||
// keep the grid LEN
|
f.custom_translate_cell((0, 0), (1, 0), |rolling, old, new| {
|
||||||
self.cells.pop();
|
if let Some((arg_x, _)) = Grid::parse_to_idx(old) {
|
||||||
for x in 0..LEN {
|
// add 1 because of the insertion
|
||||||
for y in 0..LEN {
|
if arg_x < insertion_x { rolling.to_owned() } else { rolling.replace(old, new) }
|
||||||
if let Some(cell) = self.get_cell_raw(x, y).as_ref().map(|f| {
|
} else {
|
||||||
f.custom_translate_cell((0, 0), (1, 0), |rolling, old, new| {
|
unimplemented!("Invalid variable wanted to be translated")
|
||||||
if let Some((arg_x, _)) = Grid::parse_to_idx(old) {
|
}
|
||||||
// add 1 because of the insertion
|
})
|
||||||
if arg_x < insertion_x { rolling.to_owned() } else { rolling.replace(old, new) }
|
}) {
|
||||||
} else {
|
grid.set_cell_raw((x, y), Some(cell));
|
||||||
unimplemented!("Invalid variable wanted to be translated")
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
}) {
|
|
||||||
self.set_cell_raw((x, y), Some(cell));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_column_after(&mut self, (x, y): (usize, usize)) {
|
pub fn insert_column_after(&mut self, (x, y): (usize, usize)) {
|
||||||
self.insert_column_before((x + 1, y));
|
self.insert_column_before((x + 1, y));
|
||||||
}
|
}
|
||||||
/// Iterate over the entire grid and see where
|
|
||||||
/// the farthest modified cell is.
|
|
||||||
#[must_use]
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn max_y_at_x(&self, x: usize) -> usize {
|
|
||||||
let mut max_y = 0;
|
|
||||||
if let Some(col) = self.cells.get(x) {
|
|
||||||
// we could do fancy things like .take_while(not null) but then
|
|
||||||
// we would have to deal with empty cells and stuff, which sounds
|
|
||||||
// boring. This will be fast "enough", considering the grid is
|
|
||||||
// probably only like 1k cells
|
|
||||||
for (yi, cell) in col.iter().enumerate() {
|
|
||||||
if cell.is_some() {
|
|
||||||
if yi > max_y {
|
|
||||||
max_y = yi
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
max_y
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Only evaluates equations, such as `=10` or `=A1/C2`, not
|
/// Only evaluates equations, such as `=10` or `=A1/C2`, not
|
||||||
/// strings or numbers.
|
/// strings or numbers.
|
||||||
@@ -381,10 +455,7 @@ impl Grid {
|
|||||||
EvalexprError::VariableIdentifierNotFound(var_not_found) => {
|
EvalexprError::VariableIdentifierNotFound(var_not_found) => {
|
||||||
return Err(format!("\"{var_not_found}\" is not a variable"));
|
return Err(format!("\"{var_not_found}\" is not a variable"));
|
||||||
}
|
}
|
||||||
EvalexprError::TypeError {
|
EvalexprError::TypeError { expected: e, actual: a } => {
|
||||||
expected: e,
|
|
||||||
actual: a,
|
|
||||||
} => {
|
|
||||||
// IE: You put a string into a function that wants a float
|
// IE: You put a string into a function that wants a float
|
||||||
return Err(format!("Wanted {e:?}, got {a}"));
|
return Err(format!("Wanted {e:?}, got {a}"));
|
||||||
}
|
}
|
||||||
@@ -429,15 +500,21 @@ impl Grid {
|
|||||||
|
|
||||||
/// Helper for tests
|
/// Helper for tests
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
/// Don't ever remove this from being just a test-helper.
|
||||||
|
/// This function doesn't correctly use the undo/redo api, which would require doing
|
||||||
|
/// transactions on the grid instead of direct access.
|
||||||
pub fn set_cell<T: Into<CellType>>(&mut self, cell_id: &str, val: T) {
|
pub 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, Some(val));
|
self.set_cell_raw(loc, Some(val));
|
||||||
}
|
}
|
||||||
|
self.dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[deprecated]
|
||||||
|
/// You should get the grid then transact on the grid it's self
|
||||||
pub fn set_cell_raw<T: Into<CellType>>(&mut self, (x, y): (usize, usize), val: Option<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] = val.map(|v| v.into());
|
self.get_grid_mut().set_cell_raw((x,y), val.map(|v| v.into()));
|
||||||
self.dirty = true;
|
self.dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -456,7 +533,7 @@ impl Grid {
|
|||||||
if x >= LEN || y >= LEN {
|
if x >= LEN || y >= LEN {
|
||||||
return &None;
|
return &None;
|
||||||
}
|
}
|
||||||
&self.cells[x][y]
|
&self.get_grid().get_cell_raw(x,y)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn num_to_char(idx: usize) -> String {
|
pub fn num_to_char(idx: usize) -> String {
|
||||||
@@ -574,7 +651,7 @@ fn saving_neoscim() {
|
|||||||
fn cell_strings() {
|
fn cell_strings() {
|
||||||
let mut grid = Grid::new();
|
let mut grid = Grid::new();
|
||||||
|
|
||||||
assert!(&grid.cells[0][0].is_none());
|
assert!(&grid.get_grid().get_cell_raw(0,0).is_none());
|
||||||
grid.set_cell("A0", "Hello".to_string());
|
grid.set_cell("A0", "Hello".to_string());
|
||||||
assert!(grid.get_cell("A0").is_some());
|
assert!(grid.get_cell("A0").is_some());
|
||||||
|
|
||||||
@@ -711,17 +788,17 @@ fn grid_max() {
|
|||||||
let mut grid = Grid::new();
|
let mut grid = Grid::new();
|
||||||
|
|
||||||
grid.set_cell("A0", 1.);
|
grid.set_cell("A0", 1.);
|
||||||
let (mx, my) = grid.max();
|
let (mx, my) = grid.get_grid().max();
|
||||||
assert_eq!(mx, 0);
|
assert_eq!(mx, 0);
|
||||||
assert_eq!(my, 0);
|
assert_eq!(my, 0);
|
||||||
|
|
||||||
grid.set_cell("B0", 1.);
|
grid.set_cell("B0", 1.);
|
||||||
let (mx, my) = grid.max();
|
let (mx, my) = grid.get_grid().max();
|
||||||
assert_eq!(mx, 1);
|
assert_eq!(mx, 1);
|
||||||
assert_eq!(my, 0);
|
assert_eq!(my, 0);
|
||||||
|
|
||||||
grid.set_cell("B5", 1.);
|
grid.set_cell("B5", 1.);
|
||||||
let (mx, my) = grid.max();
|
let (mx, my) = grid.get_grid().max();
|
||||||
assert_eq!(mx, 1);
|
assert_eq!(mx, 1);
|
||||||
assert_eq!(my, 5);
|
assert_eq!(my, 5);
|
||||||
}
|
}
|
||||||
@@ -1072,7 +1149,7 @@ fn insert_row_above_3() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn cell_eval_depth() {
|
fn cell_eval_depth() {
|
||||||
use crate::app::mode::*;
|
use crate::app::mode::*;
|
||||||
let mut app= App::new();
|
let mut app = App::new();
|
||||||
|
|
||||||
app.grid.set_cell("A0", 1.);
|
app.grid.set_cell("A0", 1.);
|
||||||
app.grid.set_cell("A1", "=A0+$A$0".to_string());
|
app.grid.set_cell("A1", "=A0+$A$0".to_string());
|
||||||
@@ -1122,4 +1199,3 @@ fn return_string_from_fn() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,8 +34,8 @@ impl<'a> CallbackContext<'a> {
|
|||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
|
|
||||||
for x in start_idx..=end_idx {
|
for x in start_idx..=end_idx {
|
||||||
for y in 0..=self.variables.max_y_at_x(x) {
|
for y in 0..=self.variables.get_grid().max_y_at_x(x) {
|
||||||
if let Some(s) = self.variables.get_cell_raw(x, y) {
|
if let Some(s) = self.variables.get_grid().get_cell_raw(x, y) {
|
||||||
buf.push(s);
|
buf.push(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user