Compare commits
14 Commits
052828c89c
...
full-copy-
| Author | SHA1 | Date | |
|---|---|---|---|
| d5d58694bb | |||
| c2e0661a45 | |||
| 6ec7d90ac5 | |||
| 53dcf2ffc9 | |||
| 86756a94ef | |||
| d242e1af21 | |||
| abffe6073f | |||
| d983995e8f | |||
| 045d1d6554 | |||
| 9691268d3d | |||
| f3356c1398 | |||
| b3b2c59a36 | |||
| 077b53f6ff | |||
| 0c78e7834b |
@@ -2,8 +2,12 @@
|
|||||||
|
|
||||||
*New* Spreadsheet Calculator Improved
|
*New* Spreadsheet Calculator Improved
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
Based loosely off sc-im (spreadsheet calculator improvised), which has dumb keybinds and not many features.
|
Based loosely off sc-im (spreadsheet calculator improvised), which has dumb keybinds and not many features.
|
||||||
|
|
||||||
|
[Checkout a demo file](readme/demo.nscim), download, then open with `neoscim demo.nscim`.
|
||||||
|
|
||||||
## Keybinds
|
## Keybinds
|
||||||
|
|
||||||
### Normal mode
|
### Normal mode
|
||||||
@@ -64,6 +68,7 @@ These commands operate on selections.
|
|||||||
| - | - |
|
| - | - |
|
||||||
| `:plot` | Plots the current selection to `plot.png` using gnuplot |
|
| `:plot` | Plots the current selection to `plot.png` using gnuplot |
|
||||||
| `:export <filename>` | Exports the current selection to a new file |
|
| `:export <filename>` | Exports the current selection to a new file |
|
||||||
|
| `:fill <value>` | (Aliased as `f`) Fill the selection with `<value>`, special variables `x`,`y`,`xi`, and `yi` are avaliable. Variables `x` and `y` are the global coordinates of the cell, and `xi` and `yi` are the local coordinates of the selection. |
|
||||||
|
|
||||||
## Math / Functions
|
## Math / Functions
|
||||||
|
|
||||||
|
|||||||
21
readme/demo.nscim
Normal file
21
readme/demo.nscim
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
,,,
|
||||||
|
Most of vim's movement works here as well,,,
|
||||||
|
,,,
|
||||||
|
,i,,
|
||||||
|
j,Movement,l,
|
||||||
|
,j,,
|
||||||
|
,,,
|
||||||
|
,"insert text with r, i, or a",,
|
||||||
|
,,,
|
||||||
|
1,,,
|
||||||
|
1,Text can overflow cells,,
|
||||||
|
1,But may get cut off if it's too long,,10
|
||||||
|
1,,,If it reaches another cell
|
||||||
|
1,,,
|
||||||
|
Total:,=sum(A:A),Sum A:A,
|
||||||
|
,=math::log2(B14),Do math on the output of another function,
|
||||||
|
,,,
|
||||||
|
,,,
|
||||||
|
,,11,Number.
|
||||||
|
,,=C18+1,Referencing the number and adding 1
|
||||||
|
,,,Copying the cell down will translate the reference
|
||||||
BIN
readme/screenshot.png
Normal file
BIN
readme/screenshot.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 21 KiB |
@@ -18,7 +18,10 @@ use ratatui::{
|
|||||||
use crate::app::{
|
use crate::app::{
|
||||||
clipboard::Clipboard,
|
clipboard::Clipboard,
|
||||||
error_msg::StatusMessage,
|
error_msg::StatusMessage,
|
||||||
logic::{self, calc::{Grid, get_header_size}, cell::CellType},
|
logic::{
|
||||||
|
calc::{Grid, get_header_size},
|
||||||
|
cell::CellType,
|
||||||
|
},
|
||||||
mode::Mode,
|
mode::Mode,
|
||||||
screen::ScreenSpace,
|
screen::ScreenSpace,
|
||||||
};
|
};
|
||||||
@@ -63,7 +66,7 @@ impl Widget for &App {
|
|||||||
for x in 0..x_max {
|
for x in 0..x_max {
|
||||||
for y in 0..y_max {
|
for y in 0..y_max {
|
||||||
let mut display = String::new();
|
let mut display = String::new();
|
||||||
let mut style = Style::new().fg(Color::White);
|
let mut style = Style::new();
|
||||||
|
|
||||||
// Custom width for the header of each row
|
// Custom width for the header of each row
|
||||||
let row_header_width = get_header_size() as u16;
|
let row_header_width = get_header_size() as u16;
|
||||||
@@ -148,6 +151,9 @@ impl Widget for &App {
|
|||||||
Ok(val) => {
|
Ok(val) => {
|
||||||
display = val.to_string();
|
display = val.to_string();
|
||||||
style = Style::new()
|
style = Style::new()
|
||||||
|
.fg(Color::White)
|
||||||
|
// TODO This breaks dumb terminals like the windows
|
||||||
|
// terminal
|
||||||
.underline_color(Color::DarkGray)
|
.underline_color(Color::DarkGray)
|
||||||
.add_modifier(Modifier::UNDERLINED);
|
.add_modifier(Modifier::UNDERLINED);
|
||||||
}
|
}
|
||||||
@@ -384,17 +390,20 @@ impl App {
|
|||||||
event::KeyCode::Enter => {
|
event::KeyCode::Enter => {
|
||||||
let v = editor.as_string();
|
let v = editor.as_string();
|
||||||
|
|
||||||
|
let cursor = self.grid.cursor();
|
||||||
|
self.grid.transact_on_grid(|grid| {
|
||||||
// try to insert as a float
|
// 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.cursor(), Some(v));
|
grid.set_cell_raw(cursor, Some(v));
|
||||||
} else {
|
} else {
|
||||||
// if you can't, then insert as a string
|
// if you can't, then insert as a string
|
||||||
if !v.is_empty() {
|
if !v.is_empty() {
|
||||||
self.grid.set_cell_raw(self.grid.cursor(), Some(v));
|
grid.set_cell_raw(cursor, Some(v.to_owned()));
|
||||||
} else {
|
} else {
|
||||||
self.grid.set_cell_raw::<CellType>(self.grid.cursor(), None);
|
grid.set_cell_raw::<CellType>(cursor, None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
self.mode = Mode::Normal;
|
self.mode = Mode::Normal;
|
||||||
}
|
}
|
||||||
@@ -410,10 +419,23 @@ impl App {
|
|||||||
},
|
},
|
||||||
Mode::Normal => match event::read()? {
|
Mode::Normal => match event::read()? {
|
||||||
event::Event::Key(key_event) => match key_event.code {
|
event::Event::Key(key_event) => match key_event.code {
|
||||||
event::KeyCode::F(_) => todo!(),
|
event::KeyCode::F(n) => {},
|
||||||
event::KeyCode::Char(c) => {
|
event::KeyCode::Char(c) => Mode::process_key(self, c),
|
||||||
Mode::process_key(self, c);
|
// Pretend that the arrow keys are vim movement keys
|
||||||
|
event::KeyCode::Left => Mode::process_key(self, 'h'),
|
||||||
|
event::KeyCode::Right => Mode::process_key(self, 'l'),
|
||||||
|
event::KeyCode::Up => Mode::process_key(self, 'k'),
|
||||||
|
event::KeyCode::Down => Mode::process_key(self, 'j'),
|
||||||
|
// Getting ctrl to work isn't going will right now. Use page keys for the time being.
|
||||||
|
event::KeyCode::PageUp => self.grid.redo(),
|
||||||
|
event::KeyCode::PageDown => self.grid.undo(),
|
||||||
|
event::KeyCode::Modifier(modifier_key_code) => {
|
||||||
|
if let event::ModifierKeyCode::LeftControl | event::ModifierKeyCode::RightControl = modifier_key_code {
|
||||||
|
// TODO my terminal (alacritty) isn't showing me ctrl presses. I know
|
||||||
|
// that they work tho, since ctrl+r works here in neovim.
|
||||||
|
// panic!("heard ctrl");
|
||||||
}
|
}
|
||||||
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|||||||
@@ -18,12 +18,7 @@ pub struct Clipboard {
|
|||||||
|
|
||||||
impl Clipboard {
|
impl Clipboard {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self { clipboard: Vec::new(), last_paste_cell: (0, 0), momentum: (0, 1), source_cell: (0, 0) }
|
||||||
clipboard: Vec::new(),
|
|
||||||
last_paste_cell: (0, 0),
|
|
||||||
momentum: (0, 1),
|
|
||||||
source_cell: (0, 0),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Panics if clipboard is 0 length (if you call after you
|
/// Panics if clipboard is 0 length (if you call after you
|
||||||
@@ -49,6 +44,8 @@ impl Clipboard {
|
|||||||
// cursor
|
// cursor
|
||||||
let (cx, cy) = into.cursor();
|
let (cx, cy) = into.cursor();
|
||||||
|
|
||||||
|
let cursor = into.cursor();
|
||||||
|
into.transact_on_grid(|grid| {
|
||||||
// iterate thru the clipbaord's cells
|
// iterate thru the clipbaord's cells
|
||||||
for (x, row) in self.clipboard.iter().enumerate() {
|
for (x, row) in self.clipboard.iter().enumerate() {
|
||||||
for (y, cell) in row.iter().enumerate() {
|
for (y, cell) in row.iter().enumerate() {
|
||||||
@@ -56,18 +53,19 @@ impl Clipboard {
|
|||||||
|
|
||||||
if translate {
|
if translate {
|
||||||
if let Some(cell) = cell {
|
if let Some(cell) = cell {
|
||||||
let trans = cell.translate_cell(self.source_cell, into.cursor());
|
let trans = cell.translate_cell(self.source_cell, cursor);
|
||||||
into.set_cell_raw(idx, Some(trans));
|
grid.set_cell_raw(idx, Some(trans));
|
||||||
} else {
|
} else {
|
||||||
// The cell at this location doesn't exist (empty)
|
// The cell at this location doesn't exist (empty)
|
||||||
into.set_cell_raw::<CellType>(idx, None);
|
grid.set_cell_raw::<CellType>(idx, None);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// translate = false
|
// translate = false
|
||||||
into.set_cell_raw::<CellType>(idx, cell.clone());
|
grid.set_cell_raw::<CellType>(idx, cell.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
let (lx, ly) = self.last_paste_cell;
|
let (lx, ly) = self.last_paste_cell;
|
||||||
self.momentum = (cx as i32 - lx as i32, cy as i32 - ly as i32);
|
self.momentum = (cx as i32 - lx as i32, cy as i32 - ly as i32);
|
||||||
@@ -109,16 +107,19 @@ impl Clipboard {
|
|||||||
|
|
||||||
// size the clipboard appropriately
|
// size the clipboard appropriately
|
||||||
self.clipboard.clear();
|
self.clipboard.clear();
|
||||||
|
|
||||||
|
from.transact_on_grid(|grid| {
|
||||||
// clone data into clipboard
|
// clone data into clipboard
|
||||||
for x in low_x..=hi_x {
|
for x in low_x..=hi_x {
|
||||||
let mut col = Vec::new();
|
let mut col = Vec::new();
|
||||||
for y in low_y..=hi_y {
|
for y in low_y..=hi_y {
|
||||||
let a = from.get_cell_raw(x, y);
|
let a = grid.get_cell_raw(x, y);
|
||||||
col.push(a.clone());
|
col.push(a.clone());
|
||||||
from.set_cell_raw::<CellType>((x, y), None);
|
grid.set_cell_raw::<CellType>((x, y), None);
|
||||||
}
|
}
|
||||||
self.clipboard.push(col);
|
self.clipboard.push(col);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
self.last_paste_cell = (low_x, low_y);
|
self.last_paste_cell = (low_x, low_y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -353,7 +354,7 @@ fn copy_paste_y_locked_var() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn issue_47() {
|
fn copy_paste_var_in_function() {
|
||||||
let mut app = App::new();
|
let mut app = App::new();
|
||||||
app.grid.set_cell("A0", 4.to_string());
|
app.grid.set_cell("A0", 4.to_string());
|
||||||
Mode::process_key(&mut app, 'j');
|
Mode::process_key(&mut app, 'j');
|
||||||
@@ -367,3 +368,31 @@ fn issue_47() {
|
|||||||
let a = app.grid.get_cell("A2").as_ref().expect("Should've been set by paste");
|
let a = app.grid.get_cell("A2").as_ref().expect("Should've been set by paste");
|
||||||
assert_eq!(a.to_string(), "=math::log2(A1)");
|
assert_eq!(a.to_string(), "=math::log2(A1)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn copy_paste_range_in_function() {
|
||||||
|
let mut app = App::new();
|
||||||
|
app.grid.set_cell("A0", 1.to_string());
|
||||||
|
app.grid.set_cell("A1", 1.to_string());
|
||||||
|
app.grid.set_cell("A2", 1.to_string());
|
||||||
|
|
||||||
|
app.grid.set_cell("B0", "=sum(A:A)".to_string());
|
||||||
|
app.grid.mv_cursor_to(1, 0);
|
||||||
|
app.mode = super::mode::Mode::Chord(Chord::new('y'));
|
||||||
|
Mode::process_key(&mut app, 'y');
|
||||||
|
Mode::process_key(&mut app, 'l');
|
||||||
|
Mode::process_key(&mut app, 'p');
|
||||||
|
|
||||||
|
let a = app.grid.get_cell("C0").as_ref().expect("Should've been set by paste");
|
||||||
|
assert_eq!(a.to_string(), "=sum(B:B)");
|
||||||
|
|
||||||
|
// now copy the range the other direction
|
||||||
|
app.grid.mv_cursor_to(2, 0);
|
||||||
|
app.mode = super::mode::Mode::Chord(Chord::new('y'));
|
||||||
|
Mode::process_key(&mut app, 'y');
|
||||||
|
app.grid.mv_cursor_to(1, 1);
|
||||||
|
Mode::process_key(&mut app, 'p');
|
||||||
|
|
||||||
|
let a = app.grid.get_cell("B1").as_ref().expect("Should've been set by paste");
|
||||||
|
assert_eq!(a.to_string(), "=sum(A:A)");
|
||||||
|
}
|
||||||
|
|||||||
@@ -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::{
|
calc::internal::CellGrid,
|
||||||
cell::{CSV_DELIMITER, CellType},
|
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";
|
||||||
|
|
||||||
pub struct Grid {
|
mod internal {
|
||||||
|
use crate::app::logic::{calc::LEN, cell::CellType};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct CellGrid {
|
||||||
// a b c ...
|
// a b c ...
|
||||||
// 0
|
// 0
|
||||||
// 1
|
// 1
|
||||||
// 2
|
// 2
|
||||||
// ...
|
// ...
|
||||||
cells: Vec<Vec<Option<CellType>>>,
|
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 {
|
||||||
|
/// Which grid in history are we currently on
|
||||||
|
current_grid: usize,
|
||||||
|
/// An array of grids, thru history
|
||||||
|
grid_history: Vec<CellGrid>,
|
||||||
/// (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> {
|
||||||
@@ -68,6 +154,8 @@ impl Grid {
|
|||||||
|
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
file.read_to_string(&mut buf)?;
|
file.read_to_string(&mut buf)?;
|
||||||
|
|
||||||
|
grid.transact_on_grid(|grid| {
|
||||||
for (yi, line) in buf.lines().enumerate() {
|
for (yi, line) in buf.lines().enumerate() {
|
||||||
let cells = Self::parse_csv_line(line);
|
let cells = Self::parse_csv_line(line);
|
||||||
|
|
||||||
@@ -76,6 +164,7 @@ impl Grid {
|
|||||||
grid.set_cell_raw((xi, yi), cell);
|
grid.set_cell_raw((xi, yi), cell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// force dirty back off, we just read the data so it's gtg
|
// force dirty back off, we just read the data so it's gtg
|
||||||
grid.dirty = false;
|
grid.dirty = false;
|
||||||
@@ -112,19 +201,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 +231,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]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn undo(&mut self) {
|
||||||
|
self.current_grid = self.current_grid.saturating_sub(1);
|
||||||
|
}
|
||||||
|
pub fn redo(&mut self) {
|
||||||
|
self.current_grid = min(self.grid_history.len() - 1, self.current_grid + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub 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,38 +371,36 @@ 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)) {
|
||||||
|
self.transact_on_grid(|grid: &mut CellGrid| {
|
||||||
|
grid.insert_row(insertion_y);
|
||||||
for x in 0..LEN {
|
for x in 0..LEN {
|
||||||
self.cells[x].insert(insertion_y, None);
|
|
||||||
self.cells[x].pop();
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
)}) {
|
}) {
|
||||||
self.set_cell_raw((x,y), Some(cell));
|
grid.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);
|
|
||||||
}
|
|
||||||
self.cells.insert(insertion_x, v);
|
|
||||||
// keep the grid LEN
|
|
||||||
self.cells.pop();
|
|
||||||
for x in 0..LEN {
|
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), (1, 0), |rolling, old, new| {
|
f.custom_translate_cell((0, 0), (1, 0), |rolling, old, new| {
|
||||||
if let Some((arg_x, _)) = Grid::parse_to_idx(old) {
|
if let Some((arg_x, _)) = Grid::parse_to_idx(old) {
|
||||||
// add 1 because of the insertion
|
// add 1 because of the insertion
|
||||||
@@ -293,60 +410,20 @@ impl Grid {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}) {
|
}) {
|
||||||
self.set_cell_raw((x, y), Some(cell));
|
grid.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.
|
||||||
pub fn evaluate(&self, mut eq: &str) -> Result<f64, String> {
|
pub fn evaluate(&self, mut eq: &str) -> Result<CellType, String> {
|
||||||
if eq.starts_with('=') {
|
if eq.starts_with('=') {
|
||||||
eq = &eq[1..];
|
eq = &eq[1..];
|
||||||
} else {
|
} else {
|
||||||
@@ -360,11 +437,15 @@ impl Grid {
|
|||||||
if v.is_number() {
|
if v.is_number() {
|
||||||
if v.is_float() {
|
if v.is_float() {
|
||||||
let val = v.as_float().expect("Value lied about being a float");
|
let val = v.as_float().expect("Value lied about being a float");
|
||||||
return Ok(val);
|
return Ok(CellType::Number(val));
|
||||||
} else if v.is_int() {
|
} else if v.is_int() {
|
||||||
let i = v.as_int().expect("Value lied about being an int");
|
let val = v.as_int().expect("Value lied about being an int");
|
||||||
return Ok(i as f64);
|
return Ok(CellType::Number(val as f64));
|
||||||
}
|
}
|
||||||
|
} else if v.is_string() {
|
||||||
|
// ^^ This allows for functions to return a string
|
||||||
|
let s = v.as_string().expect("Value lied about being a String");
|
||||||
|
return Ok(CellType::String(s));
|
||||||
}
|
}
|
||||||
return Err("Result is NaN".to_string());
|
return Err("Result is NaN".to_string());
|
||||||
};
|
};
|
||||||
@@ -377,10 +458,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}"));
|
||||||
}
|
}
|
||||||
@@ -389,12 +467,22 @@ impl Grid {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn char_to_idx(i: &str) -> usize {
|
||||||
|
let x_idx = i
|
||||||
|
.chars()
|
||||||
|
.filter(|f| f.is_alphabetic())
|
||||||
|
.enumerate()
|
||||||
|
.map(|(idx, c)| ((c.to_ascii_lowercase() as usize).saturating_sub(97)) + (26 * idx))
|
||||||
|
.fold(0, |a, b| a + b);
|
||||||
|
x_idx
|
||||||
|
}
|
||||||
|
|
||||||
/// Parse values in the format of A0, C10 ZZ99, etc, and
|
/// Parse values in the format of A0, C10 ZZ99, etc, and
|
||||||
/// turn them into an X,Y index.
|
/// turn them into an X,Y index.
|
||||||
pub fn parse_to_idx(i: &str) -> Option<(usize, usize)> {
|
pub fn parse_to_idx(i: &str) -> Option<(usize, usize)> {
|
||||||
let i = i.replace('$', "");
|
let i = i.replace('$', "");
|
||||||
|
|
||||||
let chars = i.chars().take_while(|c| c.is_alphabetic()).collect::<Vec<char>>();
|
let chars = i.chars().take_while(|c| c.is_alphabetic()).collect::<String>();
|
||||||
let nums = i.chars().skip(chars.len()).take_while(|c| c.is_numeric()).collect::<String>();
|
let nums = i.chars().skip(chars.len()).take_while(|c| c.is_numeric()).collect::<String>();
|
||||||
|
|
||||||
// At least half the arguments are gone
|
// At least half the arguments are gone
|
||||||
@@ -403,11 +491,7 @@ impl Grid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get the x index from the chars
|
// get the x index from the chars
|
||||||
let x_idx = chars
|
let x_idx = Self::char_to_idx(&chars);
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.map(|(idx, c)| (c.to_ascii_lowercase() as usize - 97) + (26 * idx))
|
|
||||||
.fold(0, |a, b| a + b);
|
|
||||||
|
|
||||||
// get the y index from the numbers
|
// get the y index from the numbers
|
||||||
if let Ok(y_idx) = nums.parse::<usize>() {
|
if let Ok(y_idx) = nums.parse::<usize>() {
|
||||||
@@ -419,15 +503,13 @@ 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.get_grid_mut().set_cell_raw(loc, Some(val))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
self.dirty = true;
|
self.dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -446,7 +528,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 {
|
||||||
@@ -499,11 +581,11 @@ fn saving_csv() {
|
|||||||
// insure that the cells are there
|
// insure that the cells are there
|
||||||
let cell = app.grid.get_cell_raw(0, 10).as_ref().expect("Should've been set");
|
let cell = app.grid.get_cell_raw(0, 10).as_ref().expect("Should've been set");
|
||||||
let res = app.grid.evaluate(&cell.to_string()).expect("Should evaluate");
|
let res = app.grid.evaluate(&cell.to_string()).expect("Should evaluate");
|
||||||
assert_eq!(res, 11.0);
|
assert_eq!(res, (11.0).into());
|
||||||
assert_eq!(cell.escaped_csv_string(), "=A9+A$0");
|
assert_eq!(cell.escaped_csv_string(), "=A9+A$0");
|
||||||
let cell = app.grid.get_cell_raw(1, 10).as_ref().expect("Should've been set");
|
let cell = app.grid.get_cell_raw(1, 10).as_ref().expect("Should've been set");
|
||||||
let res = app.grid.evaluate(&cell.to_string()).expect("Should evaluate");
|
let res = app.grid.evaluate(&cell.to_string()).expect("Should evaluate");
|
||||||
assert_eq!(res, 121.0);
|
assert_eq!(res, (121.0).into());
|
||||||
assert_eq!(cell.escaped_csv_string(), "=A10^2");
|
assert_eq!(cell.escaped_csv_string(), "=A10^2");
|
||||||
|
|
||||||
// set saving the file
|
// set saving the file
|
||||||
@@ -542,11 +624,11 @@ fn saving_neoscim() {
|
|||||||
// insure that the cells are there
|
// insure that the cells are there
|
||||||
let cell = app.grid.get_cell_raw(0, 10).as_ref().expect("Should've been set");
|
let cell = app.grid.get_cell_raw(0, 10).as_ref().expect("Should've been set");
|
||||||
let res = app.grid.evaluate(&cell.to_string()).expect("Should evaluate");
|
let res = app.grid.evaluate(&cell.to_string()).expect("Should evaluate");
|
||||||
assert_eq!(res, 11.0);
|
assert_eq!(res, (11.0).into());
|
||||||
assert_eq!(cell.escaped_csv_string(), "=A9+A$0");
|
assert_eq!(cell.escaped_csv_string(), "=A9+A$0");
|
||||||
let cell = app.grid.get_cell_raw(1, 10).as_ref().expect("Should've been set");
|
let cell = app.grid.get_cell_raw(1, 10).as_ref().expect("Should've been set");
|
||||||
let res = app.grid.evaluate(&cell.to_string()).expect("Should evaluate");
|
let res = app.grid.evaluate(&cell.to_string()).expect("Should evaluate");
|
||||||
assert_eq!(res, 121.0);
|
assert_eq!(res, (121.0).into());
|
||||||
assert_eq!(cell.escaped_csv_string(), "=A10^2");
|
assert_eq!(cell.escaped_csv_string(), "=A10^2");
|
||||||
|
|
||||||
// set saving the file
|
// set saving the file
|
||||||
@@ -564,7 +646,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());
|
||||||
|
|
||||||
@@ -587,6 +669,7 @@ fn alphanumeric_indexing() {
|
|||||||
assert_eq!(Grid::parse_to_idx("A"), None);
|
assert_eq!(Grid::parse_to_idx("A"), None);
|
||||||
assert_eq!(Grid::parse_to_idx(":"), None);
|
assert_eq!(Grid::parse_to_idx(":"), None);
|
||||||
assert_eq!(Grid::parse_to_idx("="), None);
|
assert_eq!(Grid::parse_to_idx("="), None);
|
||||||
|
assert_eq!(Grid::parse_to_idx("A:A"), None);
|
||||||
|
|
||||||
assert_eq!(Grid::num_to_char(0).trim(), "A");
|
assert_eq!(Grid::num_to_char(0).trim(), "A");
|
||||||
assert_eq!(Grid::num_to_char(25).trim(), "Z");
|
assert_eq!(Grid::num_to_char(25).trim(), "Z");
|
||||||
@@ -607,31 +690,31 @@ fn valid_equations() {
|
|||||||
// cell math
|
// cell math
|
||||||
let cell = grid.get_cell("C0").as_ref().expect("Just set it");
|
let cell = grid.get_cell("C0").as_ref().expect("Just set it");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
||||||
assert_eq!(res, 3.);
|
assert_eq!(res, (3.).into());
|
||||||
|
|
||||||
// divide floats
|
// divide floats
|
||||||
grid.set_cell("D0", "=5./2.".to_string());
|
grid.set_cell("D0", "=5./2.".to_string());
|
||||||
let cell = grid.get_cell("D0").as_ref().expect("I just set this");
|
let cell = grid.get_cell("D0").as_ref().expect("I just set this");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should be ok");
|
let res = grid.evaluate(&cell.to_string()).expect("Should be ok");
|
||||||
assert_eq!(res, 2.5);
|
assert_eq!(res, (2.5).into());
|
||||||
|
|
||||||
// Float / Int mix
|
// Float / Int mix
|
||||||
grid.set_cell("D0", "=5./2".to_string());
|
grid.set_cell("D0", "=5./2".to_string());
|
||||||
let cell = grid.get_cell("D0").as_ref().expect("I just set this");
|
let cell = grid.get_cell("D0").as_ref().expect("I just set this");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should be ok");
|
let res = grid.evaluate(&cell.to_string()).expect("Should be ok");
|
||||||
assert_eq!(res, 2.5);
|
assert_eq!(res, (2.5).into());
|
||||||
|
|
||||||
// divide "ints" (should become floats)
|
// divide "ints" (should become floats)
|
||||||
grid.set_cell("D0", "=5/2".to_string());
|
grid.set_cell("D0", "=5/2".to_string());
|
||||||
let cell = grid.get_cell("D0").as_ref().expect("I just set this");
|
let cell = grid.get_cell("D0").as_ref().expect("I just set this");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should be ok");
|
let res = grid.evaluate(&cell.to_string()).expect("Should be ok");
|
||||||
assert_eq!(res, 2.5);
|
assert_eq!(res, (2.5).into());
|
||||||
|
|
||||||
// Non-equation that should still be valid
|
// Non-equation that should still be valid
|
||||||
grid.set_cell("D0", "=10".to_string());
|
grid.set_cell("D0", "=10".to_string());
|
||||||
let cell = grid.get_cell("D0").as_ref().expect("I just set this");
|
let cell = grid.get_cell("D0").as_ref().expect("I just set this");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should be ok");
|
let res = grid.evaluate(&cell.to_string()).expect("Should be ok");
|
||||||
assert_eq!(res, 10.);
|
assert_eq!(res, (10.).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cell = output of Cell = value of Cells.
|
// Cell = output of Cell = value of Cells.
|
||||||
@@ -648,7 +731,7 @@ fn fn_of_fn() {
|
|||||||
let res = grid.evaluate(&cell.to_string());
|
let res = grid.evaluate(&cell.to_string());
|
||||||
|
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), 6.);
|
assert_eq!(res.unwrap(), (6.).into());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
panic!("Cell not found");
|
panic!("Cell not found");
|
||||||
@@ -684,7 +767,7 @@ fn invalid_equations() {
|
|||||||
let cell = grid.get_cell("B0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("B0").as_ref().expect("Just set the cell");
|
||||||
let res = grid.evaluate(&cell.to_string());
|
let res = grid.evaluate(&cell.to_string());
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert!(res.is_ok_and(|v| v == 10.));
|
assert!(res.is_ok_and(|v| v == (10.).into()));
|
||||||
|
|
||||||
// Trailing comma in function call
|
// Trailing comma in function call
|
||||||
grid.set_cell("A0", 5.);
|
grid.set_cell("A0", 5.);
|
||||||
@@ -692,7 +775,7 @@ fn invalid_equations() {
|
|||||||
grid.set_cell("B0", "=avg(A0,A1,)".to_string());
|
grid.set_cell("B0", "=avg(A0,A1,)".to_string());
|
||||||
let cell = grid.get_cell("B0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("B0").as_ref().expect("Just set the cell");
|
||||||
let res = grid.evaluate(&cell.to_string());
|
let res = grid.evaluate(&cell.to_string());
|
||||||
assert_eq!(res.unwrap(), 7.5);
|
assert_eq!(res.unwrap(), (7.5).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -700,17 +783,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);
|
||||||
}
|
}
|
||||||
@@ -723,19 +806,19 @@ fn avg_function() {
|
|||||||
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
||||||
let res = grid.evaluate(&cell.to_string());
|
let res = grid.evaluate(&cell.to_string());
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), 5.);
|
assert_eq!(res.unwrap(), (5.).into());
|
||||||
|
|
||||||
grid.set_cell("A0", "=avg(5,10)".to_string());
|
grid.set_cell("A0", "=avg(5,10)".to_string());
|
||||||
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
||||||
let res = grid.evaluate(&cell.to_string());
|
let res = grid.evaluate(&cell.to_string());
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), 7.5);
|
assert_eq!(res.unwrap(), (7.5).into());
|
||||||
|
|
||||||
grid.set_cell("A0", "=avg(5,10,15)".to_string());
|
grid.set_cell("A0", "=avg(5,10,15)".to_string());
|
||||||
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
||||||
let res = grid.evaluate(&cell.to_string());
|
let res = grid.evaluate(&cell.to_string());
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), 10.);
|
assert_eq!(res.unwrap(), (10.).into());
|
||||||
|
|
||||||
grid.set_cell("A0", "=avg(foo)".to_string());
|
grid.set_cell("A0", "=avg(foo)".to_string());
|
||||||
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
||||||
@@ -761,13 +844,13 @@ fn sum_function() {
|
|||||||
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
||||||
let res = grid.evaluate(&cell.to_string());
|
let res = grid.evaluate(&cell.to_string());
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), 5.);
|
assert_eq!(res.unwrap(), (5.).into());
|
||||||
|
|
||||||
grid.set_cell("A0", "=sum(5,10)".to_string());
|
grid.set_cell("A0", "=sum(5,10)".to_string());
|
||||||
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
||||||
let res = grid.evaluate(&cell.to_string());
|
let res = grid.evaluate(&cell.to_string());
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), 15.);
|
assert_eq!(res.unwrap(), (15.).into());
|
||||||
|
|
||||||
grid.set_cell("A0", "=sum(foo)".to_string());
|
grid.set_cell("A0", "=sum(foo)".to_string());
|
||||||
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
||||||
@@ -797,7 +880,7 @@ fn xlookup_function() {
|
|||||||
let cell = grid.get_cell("B0").as_ref().expect("Just set the cell");
|
let cell = grid.get_cell("B0").as_ref().expect("Just set the cell");
|
||||||
let res = grid.evaluate(&cell.to_string());
|
let res = grid.evaluate(&cell.to_string());
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(res.unwrap(), 31.);
|
assert_eq!(res.unwrap(), (31.).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -851,6 +934,19 @@ fn parse_csv() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn invalid_ranges() {
|
||||||
|
let mut grid = Grid::new();
|
||||||
|
|
||||||
|
grid.set_cell("A0", 2.);
|
||||||
|
grid.set_cell("A1", 1.);
|
||||||
|
// ASCII to number conversion needs to not overflow
|
||||||
|
grid.set_cell("B0", "=sum($:A)".to_string());
|
||||||
|
|
||||||
|
let cell = grid.get_cell("B0").as_ref().expect("Just set it");
|
||||||
|
let _ = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ranges() {
|
fn ranges() {
|
||||||
let mut grid = Grid::new();
|
let mut grid = Grid::new();
|
||||||
@@ -862,13 +958,13 @@ fn ranges() {
|
|||||||
// range with numbers
|
// range with numbers
|
||||||
let cell = grid.get_cell("B0").as_ref().expect("Just set it");
|
let cell = grid.get_cell("B0").as_ref().expect("Just set it");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
||||||
assert_eq!(res, 3.);
|
assert_eq!(res, (3.).into());
|
||||||
|
|
||||||
// use range output as input for other function
|
// use range output as input for other function
|
||||||
grid.set_cell("B1", "=B0*2".to_string());
|
grid.set_cell("B1", "=B0*2".to_string());
|
||||||
let cell = grid.get_cell("B1").as_ref().expect("Just set it");
|
let cell = grid.get_cell("B1").as_ref().expect("Just set it");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
||||||
assert_eq!(res, 6.);
|
assert_eq!(res, (6.).into());
|
||||||
|
|
||||||
// use equation outputs as range input
|
// use equation outputs as range input
|
||||||
grid.set_cell("A2", "=C0+1".to_string());
|
grid.set_cell("A2", "=C0+1".to_string());
|
||||||
@@ -876,11 +972,11 @@ fn ranges() {
|
|||||||
|
|
||||||
let cell = grid.get_cell("A2").as_ref().expect("Just set it");
|
let cell = grid.get_cell("A2").as_ref().expect("Just set it");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
||||||
assert_eq!(res, 6.);
|
assert_eq!(res, (6.).into());
|
||||||
|
|
||||||
let cell = grid.get_cell("B0").as_ref().expect("Just set it");
|
let cell = grid.get_cell("B0").as_ref().expect("Just set it");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
||||||
assert_eq!(res, 9.);
|
assert_eq!(res, (9.).into());
|
||||||
|
|
||||||
// use function outputs as range input
|
// use function outputs as range input
|
||||||
grid.set_cell("B1", 2.);
|
grid.set_cell("B1", 2.);
|
||||||
@@ -889,7 +985,7 @@ fn ranges() {
|
|||||||
|
|
||||||
let cell = grid.get_cell("C0").as_ref().expect("Just set it");
|
let cell = grid.get_cell("C0").as_ref().expect("Just set it");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
||||||
assert_eq!(res, 5.);
|
assert_eq!(res, (5.).into());
|
||||||
|
|
||||||
// use range outputs as range input
|
// use range outputs as range input
|
||||||
grid.set_cell("D0", "=sum(C:C)".to_string());
|
grid.set_cell("D0", "=sum(C:C)".to_string());
|
||||||
@@ -897,7 +993,7 @@ fn ranges() {
|
|||||||
|
|
||||||
let cell = grid.get_cell("D0").as_ref().expect("Just set it");
|
let cell = grid.get_cell("D0").as_ref().expect("Just set it");
|
||||||
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
let res = grid.evaluate(&cell.to_string()).expect("Should evaluate.");
|
||||||
assert_eq!(res, 6.);
|
assert_eq!(res, (6.).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -1067,6 +1163,34 @@ fn cell_eval_depth() {
|
|||||||
assert_eq!(c.to_string(), "=A5+$A$0");
|
assert_eq!(c.to_string(), "=A5+$A$0");
|
||||||
|
|
||||||
let res = app.grid.evaluate(&c.to_string()).expect("Should evaluate");
|
let res = app.grid.evaluate(&c.to_string()).expect("Should evaluate");
|
||||||
assert_eq!(res, 7.);
|
assert_eq!(res, (7.).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn return_string_from_fn() {
|
||||||
|
let mut grid = Grid::new();
|
||||||
|
grid.set_cell("A0", "=if(2>1, \"A\", \"B\")".to_string()); // true, A
|
||||||
|
grid.set_cell("A1", "=if(1>2, \"A\", \"B\")".to_string()); // false, B
|
||||||
|
|
||||||
|
let cell = grid.get_cell("A0").as_ref().expect("Just set the cell");
|
||||||
|
let res = grid.evaluate(&cell.to_string());
|
||||||
|
assert!(res.is_ok());
|
||||||
|
if let Ok(f) = res {
|
||||||
|
if let CellType::String(s) = f {
|
||||||
|
assert_eq!("A", s);
|
||||||
|
} else {
|
||||||
|
unreachable!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cell = grid.get_cell("A1").as_ref().expect("Just set the cell");
|
||||||
|
let res = grid.evaluate(&cell.to_string());
|
||||||
|
assert!(res.is_ok());
|
||||||
|
if let Ok(f) = res {
|
||||||
|
if let CellType::String(s) = f {
|
||||||
|
assert_eq!("B", s);
|
||||||
|
} else {
|
||||||
|
unreachable!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ impl CellType {
|
|||||||
CellType::Equation(eq) => {
|
CellType::Equation(eq) => {
|
||||||
// Populate the context
|
// Populate the context
|
||||||
let ctx = ExtractionContext::new();
|
let ctx = ExtractionContext::new();
|
||||||
let _ = eval_with_context(eq, &ctx);
|
let _ = eval_with_context(&eq[1..], &ctx);
|
||||||
|
|
||||||
let mut equation = eq.clone();
|
let mut equation = eq.clone();
|
||||||
// translate standard vars A0 -> A1
|
// translate standard vars A0 -> A1
|
||||||
@@ -73,7 +73,10 @@ impl CellType {
|
|||||||
let mut lock_y = false;
|
let mut lock_y = false;
|
||||||
|
|
||||||
if old_var.contains('$') {
|
if old_var.contains('$') {
|
||||||
let locations = old_var.char_indices().filter(|(_, c)| *c == '$').map(|(i, _)| i).collect::<Vec<usize>>();
|
let locations = old_var
|
||||||
|
.char_indices()
|
||||||
|
.filter(|(_, c)| *c == '$').map(|(i, _)| i)
|
||||||
|
.collect::<Vec<usize>>();
|
||||||
match locations.len() {
|
match locations.len() {
|
||||||
1 => {
|
1 => {
|
||||||
if locations[0] == 0 {
|
if locations[0] == 0 {
|
||||||
@@ -136,6 +139,40 @@ impl CellType {
|
|||||||
// rolling = rolling.replace(&old_var, &new_var);
|
// rolling = rolling.replace(&old_var, &new_var);
|
||||||
} else {
|
} else {
|
||||||
// why you coping invalid stuff, nerd?
|
// why you coping invalid stuff, nerd?
|
||||||
|
//
|
||||||
|
// could be copying a range
|
||||||
|
if old_var.contains(':') {
|
||||||
|
let parts = old_var.split(':').collect::<Vec<&str>>();
|
||||||
|
// This means the var was formatted as X:X
|
||||||
|
if parts.len() == 2 {
|
||||||
|
// how far is the movement?
|
||||||
|
let dx = to.0 as i32 - from.0 as i32;
|
||||||
|
|
||||||
|
let range_start = parts[0];
|
||||||
|
let range_end = parts[1];
|
||||||
|
|
||||||
|
// get the letters as numbers
|
||||||
|
let xs = Grid::char_to_idx(range_start) as i32;
|
||||||
|
let xe = Grid::char_to_idx(range_end) as i32;
|
||||||
|
|
||||||
|
// apply movement
|
||||||
|
let mut new_range_start = xs+dx;
|
||||||
|
let mut new_range_end = xe+dx;
|
||||||
|
|
||||||
|
// bottom out at 0
|
||||||
|
if new_range_start < 0 {
|
||||||
|
new_range_start = 0;
|
||||||
|
}
|
||||||
|
if new_range_end < 0 {
|
||||||
|
new_range_end = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert the index back into a letter and then submit it
|
||||||
|
let start = Grid::num_to_char(new_range_start as usize);
|
||||||
|
let end = Grid::num_to_char(new_range_end as usize);
|
||||||
|
equation = replace_fn(&equation, &old_var, &format!("{}:{}", start.trim(), end.trim()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return equation.into();
|
return equation.into();
|
||||||
@@ -158,3 +195,15 @@ impl Display for CellType {
|
|||||||
write!(f, "{d}")
|
write!(f, "{d}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for CellType {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
match (self, other) {
|
||||||
|
(Self::Number(left), Self::Number(right)) => left == right,
|
||||||
|
(Self::String(left), Self::String(right)) => left == right,
|
||||||
|
(Self::Equation(left), Self::Equation(right)) => left == right,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ impl<'a> CallbackContext<'a> {
|
|||||||
let as_index = |s: &str| {
|
let as_index = |s: &str| {
|
||||||
s.char_indices()
|
s.char_indices()
|
||||||
// .filter(|f| f.1 as u8 >= 97) // prevent sub with overflow errors
|
// .filter(|f| f.1 as u8 >= 97) // prevent sub with overflow errors
|
||||||
.map(|(idx, c)| (c.to_ascii_lowercase() as usize - 97) + (26 * idx))
|
.map(|(idx, c)| ((c.to_ascii_lowercase() as usize).saturating_sub(97)) + (26 * idx))
|
||||||
.fold(0, |a, b| a + b)
|
.fold(0, |a, b| a + b)
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -162,8 +162,8 @@ impl<'a> Context for CallbackContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(mut trail) = self.eval_breadcrumbs.write() {
|
if let Ok(mut trail) = self.eval_breadcrumbs.write() {
|
||||||
let find = trail.iter().filter(|id| *id == identifier).collect::<Vec<&String>>();
|
let find = trail.iter().filter(|id| *id == identifier).count();
|
||||||
if find.len() > 0 {
|
if find > 0 {
|
||||||
// recursion detected
|
// recursion detected
|
||||||
return None;
|
return None;
|
||||||
} else {
|
} else {
|
||||||
@@ -195,7 +195,7 @@ impl<'a> Context for CallbackContext<'a> {
|
|||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
match e {
|
match e {
|
||||||
EvalexprError::VariableIdentifierNotFound(_) => {
|
EvalexprError::VariableIdentifierNotFound(_err) => {
|
||||||
// If the variable isn't found, that's ~~probably~~ because
|
// If the variable isn't found, that's ~~probably~~ because
|
||||||
// of recursive reference, considering all references
|
// of recursive reference, considering all references
|
||||||
// are grabbed straight from the table.
|
// are grabbed straight from the table.
|
||||||
@@ -217,8 +217,14 @@ impl<'a> Context for CallbackContext<'a> {
|
|||||||
CellType::Number(e) => vals.push(Value::Float(*e)),
|
CellType::Number(e) => vals.push(Value::Float(*e)),
|
||||||
CellType::String(s) => vals.push(Value::String(s.to_owned())),
|
CellType::String(s) => vals.push(Value::String(s.to_owned())),
|
||||||
CellType::Equation(eq) => {
|
CellType::Equation(eq) => {
|
||||||
if let Ok(val) = eval_with_context(&eq[1..], self) {
|
match eval_with_context(&eq[1..], self) {
|
||||||
vals.push(val);
|
Ok(val) => vals.push(val),
|
||||||
|
Err(_err) => {
|
||||||
|
// At this point we are getting an error because
|
||||||
|
// recursion protection made this equation return
|
||||||
|
// None. We now don't get any evaluation.
|
||||||
|
return None
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -270,10 +276,19 @@ impl ExtractionContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn dump_vars(&self) -> Vec<String> {
|
pub fn dump_vars(&self) -> Vec<String> {
|
||||||
if let Ok(r) = self.var_registry.read() { r.clone() } else { Vec::new() }
|
if let Ok(r) = self.var_registry.read() {
|
||||||
|
r.clone()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn dump_fns(&self) -> Vec<String> {
|
pub fn dump_fns(&self) -> Vec<String> {
|
||||||
if let Ok(r) = self.fn_registry.read() { r.clone() } else { Vec::new() }
|
if let Ok(r) = self.fn_registry.read() {
|
||||||
|
r.clone()
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,8 +316,7 @@ impl Context for ExtractionContext {
|
|||||||
} else {
|
} else {
|
||||||
panic!("The RwLock should always be write-able")
|
panic!("The RwLock should always be write-able")
|
||||||
}
|
}
|
||||||
// Ok(Value::Int(1))
|
Ok(Value::Int(1))
|
||||||
unimplemented!("Extracting function identifier not implemented yet")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn are_builtin_functions_disabled(&self) -> bool {
|
fn are_builtin_functions_disabled(&self) -> bool {
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ use std::{
|
|||||||
cmp::{max, min},
|
cmp::{max, min},
|
||||||
fmt::Display,
|
fmt::Display,
|
||||||
fs,
|
fs,
|
||||||
path::PathBuf, process::Command,
|
path::PathBuf,
|
||||||
|
process::Command,
|
||||||
};
|
};
|
||||||
|
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
@@ -15,7 +16,7 @@ use crate::app::{
|
|||||||
app::App,
|
app::App,
|
||||||
error_msg::StatusMessage,
|
error_msg::StatusMessage,
|
||||||
logic::{
|
logic::{
|
||||||
calc::{CSV_EXT, CUSTOM_EXT, Grid, LEN},
|
calc::{Grid, LEN},
|
||||||
cell::CellType,
|
cell::CellType,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -153,11 +154,13 @@ impl Mode {
|
|||||||
|
|
||||||
let mut save_range = |to: &str| {
|
let mut save_range = |to: &str| {
|
||||||
let mut g = Grid::new();
|
let mut g = Grid::new();
|
||||||
|
g.transact_on_grid(|grid| {
|
||||||
for (i, x) in (low_x..=hi_x).enumerate() {
|
for (i, x) in (low_x..=hi_x).enumerate() {
|
||||||
for (j, y) in (low_y..=hi_y).enumerate() {
|
for (j, y) in (low_y..=hi_y).enumerate() {
|
||||||
g.set_cell_raw((i, j), app.grid.get_cell_raw(x, y).clone());
|
grid.set_cell_raw((i, j), grid.get_cell_raw(x, y).clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
if let Err(_e) = g.save_to(to) {
|
if let Err(_e) = g.save_to(to) {
|
||||||
app.msg = StatusMessage::error("Failed to save file");
|
app.msg = StatusMessage::error("Failed to save file");
|
||||||
}
|
}
|
||||||
@@ -171,10 +174,27 @@ impl Mode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "unknown"
|
return "unknown";
|
||||||
};
|
};
|
||||||
|
|
||||||
match args[0] {
|
match args[0] {
|
||||||
|
"f" | "fill" => {
|
||||||
|
app.grid.transact_on_grid(|grid| {
|
||||||
|
for (i, x) in (low_x..=hi_x).enumerate() {
|
||||||
|
for (j, y) in (low_y..=hi_y).enumerate() {
|
||||||
|
let arg = args
|
||||||
|
.get(1)
|
||||||
|
.map(|s| s.replace("xi", &i.to_string()))
|
||||||
|
.map(|s| s.replace("yi", &j.to_string()))
|
||||||
|
.map(|s| s.replace("x", &x.to_string()))
|
||||||
|
.map(|s| s.replace("y", &y.to_string()));
|
||||||
|
grid.set_cell_raw((x, y), arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.mode = Mode::Normal
|
||||||
|
}
|
||||||
"export" => {
|
"export" => {
|
||||||
if let Some(arg1) = args.get(1) {
|
if let Some(arg1) = args.get(1) {
|
||||||
save_range(&arg1);
|
save_range(&arg1);
|
||||||
@@ -187,11 +207,7 @@ impl Mode {
|
|||||||
// Use gnuplot to plot the selected data.
|
// Use gnuplot to plot the selected data.
|
||||||
// * Temp data will be stored in /tmp/
|
// * Temp data will be stored in /tmp/
|
||||||
// * Output will either be plot.png or a name that you pass in
|
// * Output will either be plot.png or a name that you pass in
|
||||||
let output_filename = if let Some(arg1) = args.get(1) {
|
let output_filename = if let Some(arg1) = args.get(1) { arg1 } else { "plot.png" };
|
||||||
arg1
|
|
||||||
} else {
|
|
||||||
"plot.png"
|
|
||||||
};
|
|
||||||
|
|
||||||
save_range("/tmp/plot.csv");
|
save_range("/tmp/plot.csv");
|
||||||
let plot = include_str!("../../template.gnuplot");
|
let plot = include_str!("../../template.gnuplot");
|
||||||
@@ -205,7 +221,9 @@ impl Mode {
|
|||||||
let cmd_res = Command::new("gnuplot").arg("/tmp/plot.p").output();
|
let cmd_res = Command::new("gnuplot").arg("/tmp/plot.p").output();
|
||||||
if let Err(err) = cmd_res {
|
if let Err(err) = cmd_res {
|
||||||
match err.kind() {
|
match err.kind() {
|
||||||
std::io::ErrorKind::NotFound => app.msg = StatusMessage::error("Error - Is gnuplot installed?"),
|
std::io::ErrorKind::NotFound => {
|
||||||
|
app.msg = StatusMessage::error("Error - Is gnuplot installed?")
|
||||||
|
}
|
||||||
_ => app.msg = StatusMessage::error(format!("{err}")),
|
_ => app.msg = StatusMessage::error(format!("{err}")),
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
@@ -261,7 +279,7 @@ impl Mode {
|
|||||||
// Go to bottom of column
|
// Go to bottom of column
|
||||||
'G' => {
|
'G' => {
|
||||||
let (x, _) = app.grid.cursor();
|
let (x, _) = app.grid.cursor();
|
||||||
app.grid.mv_cursor_to(x, super::logic::calc::LEN,);
|
app.grid.mv_cursor_to(x, super::logic::calc::LEN);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// edit cell
|
// edit cell
|
||||||
@@ -304,6 +322,11 @@ impl Mode {
|
|||||||
app.mode = Mode::Command(Chord::new(':'))
|
app.mode = Mode::Command(Chord::new(':'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// undo
|
||||||
|
'u' => {
|
||||||
|
app.grid.undo();
|
||||||
|
}
|
||||||
|
// paste
|
||||||
'p' => {
|
'p' => {
|
||||||
app.clipboard.paste(&mut app.grid, true);
|
app.clipboard.paste(&mut app.grid, true);
|
||||||
app.grid.apply_momentum(app.clipboard.momentum());
|
app.grid.apply_momentum(app.clipboard.momentum());
|
||||||
@@ -493,9 +516,7 @@ pub struct Chord {
|
|||||||
impl From<String> for Chord {
|
impl From<String> for Chord {
|
||||||
fn from(value: String) -> Self {
|
fn from(value: String) -> Self {
|
||||||
let b = value.as_bytes().iter().map(|f| *f as char).collect();
|
let b = value.as_bytes().iter().map(|f| *f as char).collect();
|
||||||
Chord {
|
Chord { buf: b }
|
||||||
buf: b,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -504,9 +525,7 @@ impl Chord {
|
|||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
buf.push(inital);
|
buf.push(inital);
|
||||||
|
|
||||||
Self {
|
Self { buf }
|
||||||
buf,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn backspace(&mut self) {
|
pub fn backspace(&mut self) {
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::RwLock};
|
|||||||
|
|
||||||
use ratatui::prelude;
|
use ratatui::prelude;
|
||||||
|
|
||||||
use crate::app::logic::calc::{self, LEN};
|
use crate::app::logic::calc::LEN;
|
||||||
|
|
||||||
pub struct ScreenSpace {
|
pub struct ScreenSpace {
|
||||||
/// This is measured in cells.
|
/// This is measured in cells.
|
||||||
|
|||||||
Reference in New Issue
Block a user