Compare commits

1 Commits

Author SHA1 Message Date
dee88c916e wip 2025-11-19 09:41:55 -07:00
16 changed files with 531 additions and 991 deletions

View File

@@ -1,21 +0,0 @@
name: Test Rust project
on: [push]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
rust: [stable]
steps:
- uses: https://git.oliveratkinson.net/Oliver/setup-rust-action@master
with:
rust-version: ${{ matrix.rust }}
- uses: actions/checkout@master
- name: Run tests
run: cargo test --verbose
- name: Clippy
run: cargo clippy

View File

@@ -2,12 +2,8 @@
*New* Spreadsheet Calculator Improved *New* Spreadsheet Calculator Improved
![Screenshot](readme/screenshot.png)
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
@@ -18,15 +14,8 @@ Based loosely off sc-im (spreadsheet calculator improvised), which has dumb keyb
| `k` | Move up | | `k` | Move up |
| `h` | Move left | | `h` | Move left |
| `l` | Move right | | `l` | Move right |
| `0` | Go to beginning of the row | | `0` | Go to beginning of row |
| `g0` | Go to beginning of the visual row | | `gg` | Go to beginning of column |
| `$` | Go to end of the row |
| `g$` | Go to end of the visual row |
| `gg` | Go to beginning of the column |
| `G` | Go to end of column |
| `gG` | Go to end of the the visual column |
| `u`/`Page Down` | Undo[^undo] |
| `Page Up` | Redo[^undo] |
| `i`/`a` | Enter insert mode on current cell | | `i`/`a` | Enter insert mode on current cell |
| `r` | Enter insert mode on current cell, deleting contents | | `r` | Enter insert mode on current cell, deleting contents |
| `v` | Enter visual mode | | `v` | Enter visual mode |
@@ -45,8 +34,6 @@ Based loosely off sc-im (spreadsheet calculator improvised), which has dumb keyb
| n`G` | Jump to row "n" | | n`G` | Jump to row "n" |
| nX | Press "X", "n" times | | nX | Press "X", "n" times |
[^undo]: Page up/down keybinds are (probably) temporary until I can get ctrl+r to work for redo, which is the propper key for this action. See issue #25 for the status on this.
### Visual mode ### Visual mode
| Key | Action | | Key | Action |
@@ -64,16 +51,6 @@ Based loosely off sc-im (spreadsheet calculator improvised), which has dumb keyb
| `:q` | Quit program | | `:q` | Quit program |
| `:q!` | Quit program, even if the file isn't saved | | `:q!` | Quit program, even if the file isn't saved |
#### Visual mode commands
These commands operate on selections.
| Commmand | Description |
| - | - |
| `:plot` | Plots the current selection to `plot.png` using gnuplot |
| `: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
### Math ### Math
@@ -91,11 +68,11 @@ These commands operate on selections.
| Identifier | Arg Qty | Argument Types | Description | | Identifier | Arg Qty | Argument Types | Description |
| - | - | - | - | | - | - | - | - |
| `avg` | 1 | Numeric | Returns the average of the arguments | | `avg` | >= 1 | Numeric | Returns the average of the arguments |
| `sum` | 1 | Numbers | Returns the sum of the arguments | | `sum` | >= 1 | Numbers | Returns the sum of the arguments |
| `xlookup` | 3 | Number/String, Range, Range | Searches for the number/string in the first range, returning the item at the same index in the second range | | `xlookup` | 3 | Range, Number/String, Range | Searches for the number/string in the first range, returning the item at the same index in the second range |
| `min` | 1 | Numeric | Returns the minimum of the arguments | | `min` | >= 1 | Numeric | Returns the minimum of the arguments |
| `max` | 1 | Numeric | Returns the maximum of the arguments | | `max` | >= 1 | Numeric | Returns the maximum of the arguments |
| `len` | 1 | String/Tuple | Returns the character length of a string, or the amount of elements in a tuple (not recursively) | | `len` | 1 | String/Tuple | Returns the character length of a string, or the amount of elements in a tuple (not recursively) |
| `floor` | 1 | Numeric | Returns the largest integer less than or equal to a number | | `floor` | 1 | Numeric | Returns the largest integer less than or equal to a number |
| `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 | | `round` | 1 | Numeric | Returns the nearest integer to a number. Rounds half-way cases away from 0.0 |
@@ -135,7 +112,7 @@ These commands operate on selections.
| `str::to_lowercase` | 1 | String | Returns the lower-case version of the string | | `str::to_lowercase` | 1 | String | Returns the lower-case version of the string |
| `str::to_uppercase` | 1 | String | Returns the upper-case version of the string | | `str::to_uppercase` | 1 | String | Returns the upper-case version of the string |
| `str::trim` | 1 | String | Strips whitespace from the start and the end of the string | | `str::trim` | 1 | String | Strips whitespace from the start and the end of the string |
| `str::from` | 0 | Any | Returns passed value as string | | `str::from` | >= 0 | Any | Returns passed value as string |
| `str::substring` | 3 | String, Int, Int | Returns a substring of the first argument, starting at the second argument and ending at the third argument. If the last argument is omitted, the substring extends to the end of the string | | `str::substring` | 3 | String, Int, Int | Returns a substring of the first argument, starting at the second argument and ending at the third argument. If the last argument is omitted, the substring extends to the end of the string |
| `bitand` | 2 | Int | Computes the bitwise and of the given integers | | `bitand` | 2 | Int | Computes the bitwise and of the given integers |
| `bitor` | 2 | Int | Computes the bitwise or of the given integers | | `bitor` | 2 | Int | Computes the bitwise or of the given integers |
@@ -155,8 +132,8 @@ If the cell can be a number (parsed into a float) it will be, if not, then if th
* Cell references move with copy / paste * Cell references move with copy / paste
* =A0: Will translate * =A0: Will translate
* =$A0: Will translate Y only (numbers) * =$A0: Will translate Y only
* =A$0: Will translate X only (letters) * =$A0: Will translate X only
* =$A$0: No translation * =$A$0: No translation
* Keybinds are closer to vim keying (i/a start inserting into a cell) * Keybinds are closer to vim keying (i/a start inserting into a cell)

View File

@@ -1,21 +0,0 @@
,,,
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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

View File

@@ -8,21 +8,19 @@ use std::{
use ratatui::{ use ratatui::{
DefaultTerminal, Frame, DefaultTerminal, Frame,
crossterm::event, crossterm::event::{self, KeyCode},
layout::{self, Constraint, Layout, Rect}, layout::{self, Constraint, Layout, Margin, Rect},
prelude, prelude,
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
widgets::{Paragraph, Widget}, widgets::{Block, Borders, Paragraph, Widget},
}; };
use crate::app::{ use crate::app::{
clipboard::Clipboard, clipboard::Clipboard,
error_msg::StatusMessage, error_msg::StatusMessage,
logic::{ logic::{calc::Grid, cell::CellType},
calc::{Grid, LEN, get_header_size},
cell::CellType,
},
mode::Mode, mode::Mode,
plot::Plot,
screen::ScreenSpace, screen::ScreenSpace,
}; };
@@ -38,15 +36,14 @@ pub struct App {
// this could probably be a normal array // this could probably be a normal array
pub marks: HashMap<char, (usize, usize)>, pub marks: HashMap<char, (usize, usize)>,
pub clipboard: Clipboard, pub clipboard: Clipboard,
pub plot_popup: Option<Plot>,
} }
impl Widget for &App { impl Widget for &App {
fn render(self, area: prelude::Rect, buf: &mut prelude::Buffer) { fn render(self, area: prelude::Rect, buf: &mut prelude::Buffer) {
// let now = std::time::Instant::now();
let (x_max, y_max) = self.screen.how_many_cells_fit_in(&area, &self.vars); let (x_max, y_max) = self.screen.how_many_cells_fit_in(&area, &self.vars);
let is_visually_selected = |x: usize, y: usize| -> bool { let is_selected = |x: usize, y: usize| -> bool {
if let Mode::Visual((mut x1, mut y1)) | Mode::VisualCmd((mut x1, mut y1), _) = self.mode { if let Mode::Visual((mut x1, mut y1)) | Mode::VisualCmd((mut x1, mut y1), _) = self.mode {
let (mut x2, mut y2) = self.grid.cursor(); let (mut x2, mut y2) = self.grid.cursor();
x1 += 1; x1 += 1;
@@ -65,52 +62,13 @@ impl Widget for &App {
false false
}; };
// cells that are related by reference to the cursor's cell
// (inputs to formulas and such)
let cells_of_interest: Vec<(usize, usize)> = {
let ctx = crate::app::logic::ctx::ExtractionContext::new();
let (x, y) = self.grid.cursor();
if let Some(cell) = self.grid.get_cell_raw(x, y) {
if let CellType::Equation(eq) = cell {
let _ = evalexpr::eval_with_context(&eq[1..], &ctx);
let vars = ctx.dump_vars();
let mut interest = Vec::new();
for var in vars {
if let Some(a) = Grid::parse_to_idx(&var) {
interest.push(a);
} else if let Some((start, end)) = Grid::range_as_indices(&var) {
// insert coords:
// (start, 0..len)
// ..
// (end, 0..len)
for x in start..=end {
for y in 0..=super::logic::calc::LEN {
interest.push((x,y))
}
}
}
}
interest
} else {
Vec::new()
}
} else {
Vec::new()
}
};
// Custom width for the header of each row
let row_header_width = get_header_size() as u16;
// ^^ Feels like it oculd be static but evaluating string lens doesn't work at
// compile time. Thus cannot be static.
let cell_width = self.screen.get_cell_width(&self.vars) as u16;
let cell_height = self.screen.get_cell_height(&self.vars) as u16;
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(); let mut style = Style::new().fg(Color::White);
let cell_width = self.screen.get_cell_width(&self.vars) as u16;
let cell_height = self.screen.get_cell_height(&self.vars) as u16;
// Minus 1 because of header cells, // Minus 1 because of header cells,
// the grid is shifted over (1,1), so if you need // the grid is shifted over (1,1), so if you need
@@ -124,26 +82,12 @@ impl Widget for &App {
y_idx = y as usize - 1 + self.screen.scroll_y(); y_idx = y as usize - 1 + self.screen.scroll_y();
} }
// don't render non-accessible cells
if x_idx > LEN-1 {
continue;
}
const ORANGE1: Color = Color::Rgb(200, 160, 0); const ORANGE1: Color = Color::Rgb(200, 160, 0);
const ORANGE2: Color = Color::Rgb(180, 130, 0); const ORANGE2: Color = Color::Rgb(180, 130, 0);
let mut should_render = true; let mut should_render = true;
let mut suggest_upper_bound = None; let mut suggest_upper_bound = None;
/// Center the text "99 " -> " 99 "
fn center_text(text: &str, avaliable_space: i32) -> String {
let margin = avaliable_space - text.len() as i32;
let margin = margin / 2;
let l_margin = (0..margin).map(|_| ' ').collect::<String>();
let r_margin = (0..(margin - (l_margin.len() as i32))).map(|_| ' ').collect::<String>();
format!("{l_margin}{text}{r_margin}")
}
match (x == 0, y == 0) { match (x == 0, y == 0) {
// 0,0 vi mode // 0,0 vi mode
(true, true) => { (true, true) => {
@@ -152,11 +96,11 @@ impl Widget for &App {
} }
// row names // row names
(true, false) => { (true, false) => {
display = center_text(&y_idx.to_string(), row_header_width as i32); display = y_idx.to_string();
let bg = if y_idx == self.grid.cursor().1 { let bg = if y_idx == self.grid.cursor().1 {
Color::DarkGray Color::DarkGray
} else if y_idx.is_multiple_of(2) { } else if y_idx % 2 == 0 {
ORANGE1 ORANGE1
} else { } else {
ORANGE2 ORANGE2
@@ -165,11 +109,11 @@ impl Widget for &App {
} }
// column names // column names
(false, true) => { (false, true) => {
display = center_text(&Grid::num_to_char(x_idx), cell_width as i32); display = Grid::num_to_char(x_idx);
let bg = if x_idx == self.grid.cursor().0 { let bg = if x_idx == self.grid.cursor().0 {
Color::DarkGray Color::DarkGray
} else if x_idx.is_multiple_of(2) { } else if x_idx % 2 == 0 {
ORANGE1 ORANGE1
} else { } else {
ORANGE2 ORANGE2
@@ -181,7 +125,6 @@ impl Widget for &App {
(false, false) => { (false, false) => {
match self.grid.get_cell_raw(x_idx, y_idx) { match self.grid.get_cell_raw(x_idx, y_idx) {
Some(cell) => { Some(cell) => {
// Render in different colors based on type of contents
match cell { match cell {
CellType::Number(c) => display = c.to_string(), CellType::Number(c) => display = c.to_string(),
CellType::String(s) => { CellType::String(s) => {
@@ -193,9 +136,6 @@ 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);
} }
@@ -211,12 +151,10 @@ impl Widget for &App {
} }
} }
// ===================================================
// Allow for text in one cell to visually overflow into empty cells
suggest_upper_bound = Some(display.len() as u16); suggest_upper_bound = Some(display.len() as u16);
// check for cells to the right, see if we should truncate the cell width // check for cells to the right, see if we should truncate the cell width
for i in 1..(display.len() as f32 / cell_width as f32).ceil() as usize { for i in 1..(display.len() as f32 / cell_width as f32).ceil() as usize {
if self.grid.get_cell_raw(x_idx + i, y_idx).is_some() { if let Some(_) = self.grid.get_cell_raw(x_idx + i, y_idx) {
suggest_upper_bound = Some(cell_width * i as u16); suggest_upper_bound = Some(cell_width * i as u16);
break; break;
} }
@@ -228,25 +166,15 @@ impl Widget for &App {
display.push('…'); display.push('…');
} }
} }
// ===================================================
} }
// Don't render blank cells
None => should_render = false, None => should_render = false,
} }
if cells_of_interest.contains(&(x_idx, y_idx)) { if is_selected(x.into(), y.into()) {
style = style.fg(Color::Yellow);
should_render = true;
}
if is_visually_selected(x.into(), y.into()) {
style = style.bg(Color::Blue); style = style.bg(Color::Blue);
// Make it so that cells render when selected. This fixes issue #32
should_render = true;
} }
if (x_idx, y_idx) == self.grid.cursor() { if (x_idx, y_idx) == self.grid.cursor() {
should_render = true; should_render = true;
style = Style::new().fg(Color::Black).bg(Color::White); style = Style::new().fg(Color::Black).bg(Color::White);
// modify the style of the cell you are editing // modify the style of the cell you are editing
if let Mode::Insert(_) = self.mode { if let Mode::Insert(_) = self.mode {
@@ -255,24 +183,14 @@ impl Widget for &App {
} }
} }
} }
if should_render { if should_render {
let mut x_off = area.x + (x * cell_width); let x_off = area.x + (x * cell_width);
let y_off = area.y + (y * cell_height); let y_off = area.y + (y * cell_height);
// Adjust for the fact that the first column let area = if let Some(suggestion) = suggest_upper_bound {
// is smaller, since it is just headers
if x > 0 {
x_off = x_off - (cell_width - row_header_width);
}
// If this is the row header column
let area = if x == 0 && y != 0 {
Rect::new(x_off, y_off, row_header_width, cell_height)
} else if let Some(suggestion) = suggest_upper_bound {
let max_available_width = area.width - x_off; let max_available_width = area.width - x_off;
// draw the biggest cell possible, without going OOB off the screen // draw the biggest cell possible, without going OOB off the screen
let width = min(max_available_width, suggestion); let width = min(max_available_width, suggestion as u16);
// Don't draw too small tho, we want full-sized cells, minium // Don't draw too small tho, we want full-sized cells, minium
let width = max(cell_width, width); let width = max(cell_width, width);
@@ -285,9 +203,6 @@ impl Widget for &App {
} }
} }
} }
// let ns = now.elapsed().as_nanos() as f64;
// eprintln!("Rendered in {}ms", ns/1_000_000.);
} }
} }
@@ -304,6 +219,7 @@ impl App {
marks: HashMap::new(), marks: HashMap::new(),
clipboard: Clipboard::new(), clipboard: Clipboard::new(),
file_modified_date: SystemTime::now(), file_modified_date: SystemTime::now(),
plot_popup: None,
} }
} }
@@ -333,19 +249,22 @@ impl App {
} }
fn file_name_display(&self) -> String { fn file_name_display(&self) -> String {
let mut file_name = "[No Name]"; let file_name_status = {
let mut icon = ""; let mut file_name = "[No Name]";
if let Some(file) = &self.file { let mut icon = "";
if let Some(f) = file.file_name() { if let Some(file) = &self.file {
if let Some(f) = f.to_str() { if let Some(f) = file.file_name() {
file_name = f; if let Some(f) = f.to_str() {
file_name = f;
}
} }
} }
} if self.grid.needs_to_be_saved() {
if self.grid.needs_to_be_saved() { icon = "[+]";
icon = "[+]"; }
} format!("{file_name}{icon}")
format!("{file_name}{icon}") };
file_name_status
} }
fn draw(&self, frame: &mut Frame) { fn draw(&self, frame: &mut Frame) {
@@ -376,8 +295,6 @@ impl App {
let cmd_line_left = cmd_line_split[0]; let cmd_line_left = cmd_line_split[0];
let cmd_line_status = cmd_line_split[1]; let cmd_line_status = cmd_line_split[1];
let cmd_line_right = cmd_line_split[2]; let cmd_line_right = cmd_line_split[2];
#[cfg(debug_assertions)]
let cmd_line_debug = cmd_line_split[3]; let cmd_line_debug = cmd_line_split[3];
// ====================================================== // ======================================================
@@ -401,9 +318,46 @@ impl App {
)), )),
cmd_line_debug, cmd_line_debug,
); );
// popups
if let Some(plot) = &self.plot_popup {
let block = Block::default()
.title("Plot Editor")
.title_alignment(layout::Alignment::Center)
.border_type(ratatui::widgets::BorderType::Rounded)
.borders(Borders::all())
.style(Style::default().fg(Color::White));
let popup_y = 20;
let popup_x = 40;
let a = frame.area();
let xpos = (a.width / 2) - (popup_x / 2);
let ypos = (a.height / 2) - (popup_y / 2);
let area = Rect::new(xpos, ypos, popup_x, popup_y);
frame.render_widget(ratatui::widgets::Clear, area);
frame.render_widget(block, area);
let area = area.inner(Margin::new(1, 1));
frame.render_widget(plot, area);
}
} }
fn handle_events(&mut self) -> io::Result<()> { fn handle_events(&mut self) -> io::Result<()> {
if let Some(plot) = &mut self.plot_popup {
if let event::Event::Key(key) = event::read()? {
match key.code {
KeyCode::Esc => self.plot_popup = None,
KeyCode::PrintScreen | KeyCode::Enter => unimplemented!("Generate plot data"),
KeyCode::Delete => plot.del_column(),
KeyCode::Insert => plot.add_column(),
KeyCode::Char(c) => plot.process_key(c),
_ => {}
}
}
return Ok(());
}
match &mut self.mode { match &mut self.mode {
Mode::VisualCmd(pos, chord) => match event::read()? { Mode::VisualCmd(pos, chord) => match event::read()? {
event::Event::Key(key) => match key.code { event::Event::Key(key) => match key.code {
@@ -412,7 +366,7 @@ impl App {
event::KeyCode::Char(c) => chord.add_char(c), event::KeyCode::Char(c) => chord.add_char(c),
event::KeyCode::Enter => { event::KeyCode::Enter => {
// tmp is to get around reference issues. // tmp is to get around reference issues.
let tmp = *pos; let tmp = pos.clone();
Mode::process_cmd(self); Mode::process_cmd(self);
self.mode = Mode::Visual(tmp) self.mode = Mode::Visual(tmp)
} }
@@ -442,20 +396,17 @@ impl App {
event::KeyCode::Enter => { event::KeyCode::Enter => {
let v = editor.as_string(); let v = editor.as_string();
let cursor = self.grid.cursor(); // try to insert as a float
self.grid.transact_on_grid(|grid| { if let Ok(v) = v.parse::<f64>() {
// try to insert as a float self.grid.set_cell_raw(self.grid.cursor(), Some(v));
if let Ok(v) = v.parse::<f64>() { } else {
grid.set_cell_raw(cursor, Some(v)); // if you can't, then insert as a string
if !v.is_empty() {
self.grid.set_cell_raw(self.grid.cursor(), Some(v));
} else { } else {
// if you can't, then insert as a string self.grid.set_cell_raw::<CellType>(self.grid.cursor(), None);
if !v.is_empty() {
grid.set_cell_raw(cursor, Some(v.to_owned()));
} else {
grid.set_cell_raw::<CellType>(cursor, None);
}
} }
}); }
self.mode = Mode::Normal; self.mode = Mode::Normal;
} }
@@ -471,24 +422,9 @@ 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(_n) => {} event::KeyCode::F(_) => todo!(),
event::KeyCode::Char(c) => Mode::process_key(self, c), event::KeyCode::Char(c) => {
// Pretend that the arrow keys are vim movement keys Mode::process_key(self, c);
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");
}
} }
_ => {} _ => {}
}, },

View File

@@ -18,7 +18,12 @@ pub struct Clipboard {
impl Clipboard { impl Clipboard {
pub fn new() -> Self { pub fn new() -> Self {
Self { clipboard: Vec::new(), last_paste_cell: (0, 0), momentum: (0, 1), source_cell: (0, 0) } Self {
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
@@ -44,28 +49,24 @@ impl Clipboard {
// cursor // cursor
let (cx, cy) = into.cursor(); let (cx, cy) = into.cursor();
let cursor = into.cursor(); for (x, row) in self.clipboard.iter().enumerate() {
into.transact_on_grid(|grid| { for (y, cell) in row.iter().enumerate() {
// iterate thru the clipbaord's cells let idx = (x + cx, y + cy);
for (x, row) in self.clipboard.iter().enumerate() {
for (y, cell) in row.iter().enumerate() {
let idx = (x + cx, y + cy);
if translate { if translate {
if let Some(cell) = cell { if let Some(cell) = cell {
let trans = cell.translate_cell(self.source_cell, cursor); let trans = cell.translate_cell(self.source_cell, into.cursor());
grid.set_cell_raw(idx, Some(trans)); into.set_cell_raw(idx, Some(trans));
} else {
// The cell at this location doesn't exist (empty)
grid.set_cell_raw::<CellType>(idx, None);
}
} else { } else {
// translate = false // cell doesn't exist, no need to translate
grid.set_cell_raw::<CellType>(idx, cell.clone()); into.set_cell_raw::<CellType>(idx, None);
} }
} else {
// translate = false
into.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);
@@ -107,19 +108,16 @@ impl Clipboard {
// size the clipboard appropriately // size the clipboard appropriately
self.clipboard.clear(); self.clipboard.clear();
// clone data into clipboard
from.transact_on_grid(|grid| { for x in low_x..=hi_x {
// clone data into clipboard let mut col = Vec::new();
for x in low_x..=hi_x { for y in low_y..=hi_y {
let mut col = Vec::new(); let a = from.get_cell_raw(x, y);
for y in low_y..=hi_y { col.push(a.clone());
let a = grid.get_cell_raw(x, y); from.set_cell_raw::<CellType>((x, y), None);
col.push(a.clone());
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);
} }
} }
@@ -351,48 +349,4 @@ fn copy_paste_y_locked_var() {
Mode::process_key(&mut app, 'p'); Mode::process_key(&mut app, 'p');
let c = app.grid.get_cell("B2").as_ref().expect("Just set it"); let c = app.grid.get_cell("B2").as_ref().expect("Just set it");
assert_eq!(c.to_string(), "=B$0"); assert_eq!(c.to_string(), "=B$0");
} }
#[test]
fn copy_paste_var_in_function() {
let mut app = App::new();
app.grid.set_cell("A0", 4.to_string());
Mode::process_key(&mut app, 'j');
app.grid.set_cell("A1", "=math::log2(A0)".to_string());
app.mode = super::mode::Mode::Chord(Chord::new('y'));
Mode::process_key(&mut app, 'y');
Mode::process_key(&mut app, 'j');
Mode::process_key(&mut app, 'p');
let a = app.grid.get_cell("A2").as_ref().expect("Should've been set by paste");
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)");
}

View File

@@ -47,7 +47,7 @@ impl Widget for &StatusMessage {
let msg = if self.start.elapsed().as_secs() > 3 { let msg = if self.start.elapsed().as_secs() > 3 {
String::new() String::new()
} else { } else {
self.msg.clone().unwrap_or_default() self.msg.clone().unwrap_or(String::new())
}; };
let style = match self.msg_type { let style = match self.msg_type {
@@ -57,4 +57,4 @@ impl Widget for &StatusMessage {
Paragraph::new(msg).style(style).render(area, buf); Paragraph::new(msg).style(style).render(area, buf);
} }
} }

View File

@@ -2,134 +2,32 @@ 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::logic::{ use crate::app::{
calc::internal::CellGrid, logic::{
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 const LEN: usize = 1000;
LEN.to_string().len()
}
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 {
/// Which grid in history are we currently on // a b c ...
current_grid: usize, // 0
/// An array of grids, thru history // 1
grid_history: Vec<CellGrid>, // 2
// ...
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?
@@ -144,9 +42,20 @@ impl std::fmt::Debug for Grid {
impl Grid { impl Grid {
pub fn new() -> Self { pub fn new() -> Self {
let x = CellGrid::new(); 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 { current_grid: 0, grid_history: vec![x], selected_cell: (0, 0), dirty: false } Self {
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> {
@@ -154,17 +63,14 @@ impl Grid {
let mut buf = String::new(); let mut buf = String::new();
file.read_to_string(&mut buf)?; file.read_to_string(&mut buf)?;
for (yi, line) in buf.lines().enumerate() {
let cells = Self::parse_csv_line(line);
grid.transact_on_grid(|grid| { for (xi, cell) in cells.into_iter().enumerate() {
for (yi, line) in buf.lines().enumerate() { // This gets automatically duck-typed
let cells = Self::parse_csv_line(line); grid.set_cell_raw((xi, yi), cell);
for (xi, cell) in cells.into_iter().enumerate() {
// This gets automatically duck-typed
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;
@@ -175,7 +81,7 @@ impl Grid {
/// Save file to `path` as a csv. Path with have `csv` appended to it if it /// Save file to `path` as a csv. Path with have `csv` appended to it if it
/// does not already have the extension. /// does not already have the extension.
pub fn save_to(&mut self, path: impl Into<PathBuf>) -> std::io::Result<()> { pub fn save_to(&mut self, path: impl Into<PathBuf>) -> std::io::Result<()> {
let mut path = path.into(); let path = path.into();
let resolve_values; let resolve_values;
@@ -188,34 +94,36 @@ impl Grid {
resolve_values = false; resolve_values = false;
} }
_ => { _ => {
// File as an extension but isn't ours. resolve_values = false;
// Save as csv-like // path.add_extension(CUSTOM_EXT);
resolve_values = true;
} }
}, },
None => { None => {
// File has no extension. Save it as our file type
resolve_values = false; resolve_values = false;
path.add_extension(CUSTOM_EXT); // path.add_extension(CUSTOM_EXT);
} }
} }
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.get_grid().max(); let (mx, my) = self.max();
for y in 0..=my { for y in 0..=my {
for x in 0..=mx { for x in 0..=mx {
let cell = &self.get_grid().get_cell_raw(x, y); let cell = &self.cells[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 { '\n' } else { CSV_DELIMITER }; let delim = if is_last {
'\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())
&& resolve_values && resolve_values
{ {
format!("{val}{delim}") format!("{}{}", val.to_string(), delim)
} else { } else {
format!("{}{}", cell.escaped_csv_string(), delim) format!("{}{}", cell.escaped_csv_string(), delim)
} }
@@ -231,37 +139,6 @@ impl Grid {
Ok(()) Ok(())
} }
pub fn get_grid(&self) -> &CellGrid {
&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
} }
@@ -368,84 +245,101 @@ 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| { for x in 0..LEN {
grid.insert_row(insertion_y); self.cells[x].insert(insertion_y, None);
for x in 0..LEN { self.cells[x].pop();
for y in 0..LEN { for y in 0..LEN {
if let Some(cell) = grid.get_cell_raw(x, y).as_ref().map(|f| { if let Some(cell) = self.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 if Grid::range_as_indices(old).is_some() { } else {
// ranges are not changed when moved vertically unimplemented!("Invalid variable wanted to be translated")
rolling.to_string() }
} else {
#[cfg(debug_assertions)]
unimplemented!("Invalid variable wanted to be translated");
#[cfg(not(debug_assertions))]
rolling.to_string()
}
})
}) {
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)) {
self.transact_on_grid(|grid| { let mut v = Vec::with_capacity(LEN);
grid.insert_column(insertion_x); for _ in 0..LEN {
for x in 0..LEN { v.push(None);
for y in 0..LEN { }
if let Some(cell) = grid.get_cell_raw(x, y).as_ref().map(|f| { self.cells.insert(insertion_x, v);
f.custom_translate_cell((0, 0), (1, 0), |rolling, old, new| { // keep the grid LEN
if let Some((arg_x, _)) = Grid::parse_to_idx(old) { self.cells.pop();
// add 1 because of the insertion for x in 0..LEN {
if arg_x < insertion_x { rolling.to_owned() } else { rolling.replace(old, new) } for y in 0..LEN {
} else if let Some((start, end)) = Grid::range_as_indices(old) { if let Some(cell) = self.get_cell_raw(x, y).as_ref().map(|f| {
let mut range_start = Grid::num_to_char(start); f.custom_translate_cell((0, 0), (1, 0), |rolling, old, new| {
let mut range_end = Grid::num_to_char(end); if let Some((arg_x, _)) = Grid::parse_to_idx(old) {
// add 1 because of the insertion
if start >= insertion_x { if arg_x < insertion_x { rolling.to_owned() } else { rolling.replace(old, new) }
range_start = Grid::num_to_char(start+1); } else {
} unimplemented!("Invalid variable wanted to be translated")
}
if end >= insertion_x { })
range_end = Grid::num_to_char(end+1); }) {
} self.set_cell_raw((x, y), Some(cell));
let new = format!("{range_start}:{range_end}");
rolling.replace(old, &new)
} else {
#[cfg(debug_assertions)]
unimplemented!("Invalid variable wanted to be translated");
#[cfg(not(debug_assertions))]
rolling.to_string()
}
})
}) {
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<CellType, String> { pub fn evaluate(&self, mut eq: &str) -> Result<f64, String> {
if eq.starts_with('=') { if eq.starts_with('=') {
eq = &eq[1..]; eq = &eq[1..];
} else { } else {
@@ -453,21 +347,17 @@ impl Grid {
return Err(format!("\"{eq}\" is not an equation")); return Err(format!("\"{eq}\" is not an equation"));
} }
let ctx = ctx::CallbackContext::new(self); let ctx = ctx::CallbackContext::new(&self);
let prep_for_return = |v: Value| { let prep_for_return = |v: Value| {
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(CellType::Number(val)); return Ok(val);
} else if v.is_int() { } else if v.is_int() {
let val = v.as_int().expect("Value lied about being an int"); let i = v.as_int().expect("Value lied about being an int");
return Ok(CellType::Number(val as f64)); return Ok(i 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());
}; };
@@ -480,7 +370,10 @@ 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 { expected: e, actual: a } => { EvalexprError::TypeError {
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}"));
} }
@@ -489,37 +382,12 @@ impl Grid {
} }
} }
/// Gets the indices of the range labels.
/// A:B -> (0,1)
pub fn range_as_indices(range: &str) -> Option<(usize, usize)> {
let v = range.split(':').collect::<Vec<&str>>();
if v.len() == 2 {
let start_col = v[0];
let end_col = v[1];
let start_idx = Grid::char_to_idx(start_col);
let end_idx = Grid::char_to_idx(end_col);
return Some((start_idx, end_idx));
}
None
}
pub fn char_to_idx(i: &str) -> usize {
i
.chars()
.filter(|f| f.is_alphabetic())
.enumerate()
.map(|(idx, c)| ((c.to_ascii_lowercase() as usize).saturating_sub(97)) + (26 * idx))
.sum()
}
/// 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::<String>(); let chars = i.chars().take_while(|c| c.is_alphabetic()).collect::<Vec<char>>();
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
@@ -528,7 +396,11 @@ impl Grid {
} }
// get the x index from the chars // get the x index from the chars
let x_idx = Self::char_to_idx(&chars); let x_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>() {
@@ -540,12 +412,15 @@ impl Grid {
/// Helper for tests /// Helper for tests
#[cfg(test)] #[cfg(test)]
pub fn set_cell<T: Into<CellType> + Clone>(&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.transact_on_grid(|grid| { self.set_cell_raw(loc, Some(val));
grid.set_cell_raw(loc, Some(val.clone()));
});
} }
}
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;
} }
@@ -564,7 +439,7 @@ impl Grid {
if x >= LEN || y >= LEN { if x >= LEN || y >= LEN {
return &None; return &None;
} }
self.get_grid().get_cell_raw(x, y) &self.cells[x][y]
} }
pub fn num_to_char(idx: usize) -> String { pub fn num_to_char(idx: usize) -> String {
@@ -581,7 +456,7 @@ impl Grid {
} }
word[1] = ((idx % 26) + 65) as u8 as char; word[1] = ((idx % 26) + 65) as u8 as char;
word.iter().filter(|a| !a.is_ascii_whitespace()).collect() word.iter().collect()
} }
} }
@@ -617,11 +492,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).into()); assert_eq!(res, 11.0);
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).into()); assert_eq!(res, 121.0);
assert_eq!(cell.escaped_csv_string(), "=A10^2"); assert_eq!(cell.escaped_csv_string(), "=A10^2");
// set saving the file // set saving the file
@@ -660,16 +535,16 @@ 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).into()); assert_eq!(res, 11.0);
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).into()); assert_eq!(res, 121.0);
assert_eq!(cell.escaped_csv_string(), "=A10^2"); assert_eq!(cell.escaped_csv_string(), "=A10^2");
// set saving the file // set saving the file
let filename = format!("/tmp/file.{CUSTOM_EXT}"); let filename= "/tmp/file.neoscim";
app.grid.save_to(&filename).expect("This will only work on linux systems"); app.grid.save_to(filename).expect("This will only work on linux systems");
let mut file = fs::OpenOptions::new().read(true).open(filename).expect("Just wrote the file"); let mut file = fs::OpenOptions::new().read(true).open(filename).expect("Just wrote the file");
let mut buf = String::new(); let mut buf = String::new();
file.read_to_string(&mut buf).expect("Just opened the file"); file.read_to_string(&mut buf).expect("Just opened the file");
@@ -682,7 +557,7 @@ fn saving_neoscim() {
fn cell_strings() { fn cell_strings() {
let mut grid = Grid::new(); let mut grid = Grid::new();
assert!(&grid.get_grid().get_cell_raw(0, 0).is_none()); assert!(&grid.cells[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());
@@ -705,10 +580,9 @@ 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), "A"); assert_eq!(Grid::num_to_char(0).trim(), "A");
assert_eq!(Grid::num_to_char(25), "Z"); assert_eq!(Grid::num_to_char(25).trim(), "Z");
assert_eq!(Grid::num_to_char(26), "AA"); assert_eq!(Grid::num_to_char(26), "AA");
assert_eq!(Grid::num_to_char(51), "AZ"); assert_eq!(Grid::num_to_char(51), "AZ");
assert_eq!(Grid::num_to_char(701), "ZZ"); assert_eq!(Grid::num_to_char(701), "ZZ");
@@ -726,31 +600,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.).into()); assert_eq!(res, 3.);
// 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).into()); assert_eq!(res, 2.5);
// 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).into()); assert_eq!(res, 2.5);
// 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).into()); assert_eq!(res, 2.5);
// 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.).into()); assert_eq!(res, 10.);
} }
// Cell = output of Cell = value of Cells. // Cell = output of Cell = value of Cells.
@@ -767,7 +641,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.).into()); assert_eq!(res.unwrap(), 6.);
return; return;
} }
panic!("Cell not found"); panic!("Cell not found");
@@ -803,7 +677,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.).into())); assert!(res.is_ok_and(|v| v == 10.));
// Trailing comma in function call // Trailing comma in function call
grid.set_cell("A0", 5.); grid.set_cell("A0", 5.);
@@ -811,7 +685,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).into()); assert_eq!(res.unwrap(), 7.5);
} }
#[test] #[test]
@@ -819,17 +693,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.get_grid().max(); let (mx, my) = 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.get_grid().max(); let (mx, my) = 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.get_grid().max(); let (mx, my) = grid.max();
assert_eq!(mx, 1); assert_eq!(mx, 1);
assert_eq!(my, 5); assert_eq!(my, 5);
} }
@@ -842,19 +716,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.).into()); assert_eq!(res.unwrap(), 5.);
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).into()); assert_eq!(res.unwrap(), 7.5);
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.).into()); assert_eq!(res.unwrap(), 10.);
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");
@@ -880,13 +754,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.).into()); assert_eq!(res.unwrap(), 5.);
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.).into()); assert_eq!(res.unwrap(), 15.);
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");
@@ -912,11 +786,11 @@ fn xlookup_function() {
grid.set_cell("A1", "Sarah".to_string()); grid.set_cell("A1", "Sarah".to_string());
grid.set_cell("C0", 31.); grid.set_cell("C0", 31.);
grid.set_cell("C1", 41.); grid.set_cell("C1", 41.);
grid.set_cell("B0", "=xlookup(\"Bobby\",A:A,C:C)".to_string()); grid.set_cell("B0", "=xlookup(A:A,\"Bobby\",C:C)".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!(res.is_ok()); assert!(res.is_ok());
assert_eq!(res.unwrap(), (31.).into()); assert_eq!(res.unwrap(), 31.);
} }
#[test] #[test]
@@ -970,19 +844,6 @@ 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();
@@ -994,13 +855,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.).into()); assert_eq!(res, 3.);
// 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.).into()); assert_eq!(res, 6.);
// 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());
@@ -1008,11 +869,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.).into()); assert_eq!(res, 6.);
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.).into()); assert_eq!(res, 9.);
// use function outputs as range input // use function outputs as range input
grid.set_cell("B1", 2.); grid.set_cell("B1", 2.);
@@ -1021,7 +882,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.).into()); assert_eq!(res, 5.);
// 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());
@@ -1029,7 +890,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.).into()); assert_eq!(res, 6.);
} }
#[test] #[test]
@@ -1114,66 +975,6 @@ fn insert_col_before_3() {
assert_eq!(cell.to_string(), "=B0*B1"); assert_eq!(cell.to_string(), "=B0*B1");
} }
#[test]
fn insert_col_before_static_range() {
let mut grid = Grid::new();
grid.set_cell("A0", 2.);
grid.set_cell("A1", 2.);
grid.set_cell("B0", "=sum(A:A)".to_string());
grid.mv_cursor_to(1, 0);
grid.insert_column_before(grid.cursor());
let cell = grid.get_cell("C0").as_ref().expect("Just set it");
assert_eq!(cell.to_string(), "=sum(A:A)");
}
#[test]
fn insert_col_before_move_range() {
let mut grid = Grid::new();
grid.set_cell("B0", 2.);
grid.set_cell("B1", 2.);
grid.set_cell("A0", "=sum(B:B)".to_string());
grid.mv_cursor_to(0, 0);
grid.insert_column_after(grid.cursor());
let cell = grid.get_cell("A0").as_ref().expect("Just set it");
assert_eq!(cell.to_string(), "=sum(C:C)");
}
#[test]
fn insert_row_before_static_range() {
let mut grid = Grid::new();
grid.set_cell("A0", 2.);
grid.set_cell("A1", 2.);
grid.set_cell("B0", "=sum(A:A)".to_string());
grid.mv_cursor_to(0, 0);
grid.insert_row_above(grid.cursor());
let cell = grid.get_cell("B1").as_ref().expect("Just set it");
assert_eq!(cell.to_string(), "=sum(A:A)");
}
#[test]
fn insert_row_before_move_range() {
let mut grid = Grid::new();
grid.set_cell("A0", 2.);
grid.set_cell("A1", 2.);
grid.set_cell("B0", "=sum(A:A)".to_string());
grid.mv_cursor_to(0, 0);
grid.insert_row_below(grid.cursor());
let cell = grid.get_cell("B0").as_ref().expect("Just set it");
assert_eq!(cell.to_string(), "=sum(A:A)");
}
#[test] #[test]
fn insert_row_above_1() { fn insert_row_above_1() {
let mut grid = Grid::new(); let mut grid = Grid::new();
@@ -1240,7 +1041,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());
@@ -1259,34 +1060,6 @@ 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.).into()); assert_eq!(res, 7.);
} }
#[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!();
}
}
}

View File

@@ -11,15 +11,15 @@ pub enum CellType {
Equation(String), Equation(String),
} }
impl From<f64> for CellType { impl Into<CellType> for f64 {
fn from(value: f64) -> Self { fn into(self) -> CellType {
CellType::duck_type(value.to_string()) CellType::duck_type(self.to_string())
} }
} }
impl From<String> for CellType { impl Into<CellType> for String {
fn from(value: String) -> Self { fn into(self) -> CellType {
CellType::duck_type(value) CellType::duck_type(self)
} }
} }
@@ -37,10 +37,14 @@ impl CellType {
} }
// escape string of it has a comma // escape string of it has a comma
if display.contains(CSV_DELIMITER) { format!("\"{display}\"") } else { display } if display.contains(CSV_DELIMITER) {
format!("\"{display}\"")
} else {
display
}
} }
fn duck_type(value: impl Into<String>) -> Self { fn duck_type<'a>(value: impl Into<String>) -> Self {
let value = value.into(); let value = value.into();
if let Ok(parse) = value.parse::<f64>() { if let Ok(parse) = value.parse::<f64>() {
@@ -50,58 +54,50 @@ impl CellType {
} }
} }
/// `replace_fn` takes the string, the old value, and then the new value. pub fn custom_translate_cell(&self, from: (usize, usize), to: (usize, usize), replace_fn: impl Fn(&str, &str, &str) -> String) -> CellType {
/// It can be thought of as `echo $1 | sed s/$2/$3/g`
pub fn custom_translate_cell(
&self,
from: (usize, usize),
to: (usize, usize),
replace_fn: impl Fn(&str, &str, &str) -> String,
) -> CellType {
match self { match self {
// don't translate non-equations // don't translate non-equations
CellType::Number(_) | CellType::String(_) => return self.clone(), CellType::Number(_) | CellType::String(_) => return self.clone(),
CellType::Equation(eq) => { CellType::Equation(eq) => {
// Populate the context
let ctx = ExtractionContext::new();
let _ = eval_with_context(&eq[1..], &ctx);
let mut equation = eq.clone();
// translate standard vars A0 -> A1
// extract all the variables // extract all the variables
let ctx = ExtractionContext::new();
let _ = eval_with_context(eq, &ctx);
let mut rolling = eq.clone();
// translate standard vars A0 -> A1
for old_var in ctx.dump_vars() { for old_var in ctx.dump_vars() {
let mut lock_x = false; let mut lock_x = false;
let mut lock_y = false; let mut lock_y = false;
if old_var.contains('$') { if old_var.contains('$') {
let locations = let locations = old_var.char_indices().filter(|(_, c)| *c == '$').map(|(i, _)| i).collect::<Vec<usize>>();
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 {
// locking the X axis (A,B,C...) // locking the X axis (A,B,C...)
lock_x = true; lock_x = true;
} else { } else if locations[0] < old_var.len() {
// inside the string somewhere, gonna assume this means to lock Y (1,2,3...) // inside the string somewhere, gonna assume this means to lock Y (1,2,3...)
lock_y = true; lock_y = true;
} else {
// where tf is this dollar sign?
// (It's somewhere malformed, like A0$ or something)
} }
} }
2 => { 2 => {
// Ignore this variable all together, effectively lockng X & Y // YOLO, lock both X & Y
continue; continue; // just pretend you never even saw this var
} }
_ => { _ => {
// There are 0 or >2 "$" in this string.
//
// Could probably optimize the code or something so you only go over the string // Could probably optimize the code or something so you only go over the string
// once, instead of contains() then getting the indexes of where it is. // once, instead of contains() then getting the indexes of where it is.
// You could then put your no-$ code here. // You could then put your no-$ code here.
} }
} }
} }
if let Some((src_x, src_y)) = Grid::parse_to_idx(&old_var) { if let Some((src_x, src_y)) = Grid::parse_to_idx(&old_var) {
// Use i32s instead of usize in case of negative numbers
let (x1, y1) = from; let (x1, y1) = from;
let x1 = x1 as i32; let x1 = x1 as i32;
let y1 = y1 as i32; let y1 = y1 as i32;
@@ -109,13 +105,21 @@ impl CellType {
let x2 = x2 as i32; let x2 = x2 as i32;
let y2 = y2 as i32; let y2 = y2 as i32;
let dest_x = if lock_x { src_x } else { (src_x as i32 + (x2 - x1)) as usize }; let dest_x = if lock_x {
src_x as usize
} else {
(src_x as i32 + (x2 - x1)) as usize
};
let dest_y = if lock_y { src_y } else { (src_y as i32 + (y2 - y1)) as usize }; let dest_y = if lock_y {
src_y as usize
} else {
(src_y as i32 + (y2 - y1)) as usize
};
let alpha = Grid::num_to_char(dest_x); let alpha = Grid::num_to_char(dest_x);
let alpha = alpha.trim();
// Persist the "$" locking
let new_var = if lock_x { let new_var = if lock_x {
format!("${alpha}{dest_y}") format!("${alpha}{dest_y}")
} else if lock_y { } else if lock_y {
@@ -124,46 +128,21 @@ impl CellType {
format!("{alpha}{dest_y}") format!("{alpha}{dest_y}")
}; };
// swap out vars // swap out vars
equation = replace_fn(&equation, &old_var, &new_var); rolling = replace_fn(&rolling, &old_var, &new_var);
// 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 let Some(parts) = Grid::range_as_indices(&old_var) {
// how far is the movement?
let dx = to.0 as i32 - from.0 as i32;
let xs = parts.0 as i32;
let xe = parts.1 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}:{end}"));
}
} }
} }
return equation.into(); return rolling.into();
} }
} }
} }
pub fn translate_cell(&self, from: (usize, usize), to: (usize, usize)) -> CellType { pub fn translate_cell(&self, from: (usize, usize), to: (usize, usize)) -> CellType {
self.custom_translate_cell(from, to, |a, b, c| a.replace(b, c)) self.custom_translate_cell(from, to, |a,b,c| a.replace(b, c))
} }
} }
@@ -177,14 +156,3 @@ 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,
}
}
}

View File

@@ -16,11 +16,25 @@ pub struct CallbackContext<'a> {
impl<'a> CallbackContext<'a> { impl<'a> CallbackContext<'a> {
fn expand_range(&self, range: &str) -> Option<Vec<&CellType>> { fn expand_range(&self, range: &str) -> Option<Vec<&CellType>> {
if let Some((start, end)) = Grid::range_as_indices(range) { let v = range.split(':').collect::<Vec<&str>>();
if v.len() == 2 {
let start_col = v[0];
let end_col = v[1];
let as_index = |s: &str| {
s.char_indices()
// .filter(|f| f.1 as u8 >= 97) // prevent sub with overflow errors
.map(|(idx, c)| (c.to_ascii_lowercase() as usize - 97) + (26 * idx))
.fold(0, |a, b| a + b)
};
let start_idx = as_index(start_col);
let end_idx = as_index(end_col);
let mut buf = Vec::new(); let mut buf = Vec::new();
for x in start..=end { for x in start_idx..=end_idx {
for y in 0..=self.variables.get_grid().max_y_at_x(x) { for y in 0..=self.variables.max_y_at_x(x) {
if let Some(s) = self.variables.get_cell_raw(x, y) { if let Some(s) = self.variables.get_cell_raw(x, y) {
buf.push(s); buf.push(s);
} }
@@ -90,13 +104,13 @@ impl<'a> CallbackContext<'a> {
functions.insert( functions.insert(
"xlookup".to_string(), "xlookup".to_string(),
Function::new(|arg| { Function::new(|arg| {
let expected = vec![ValueType::String, ValueType::Tuple, ValueType::Tuple]; let expected = vec![ValueType::Tuple, ValueType::String, ValueType::Tuple];
if arg.is_tuple() { if arg.is_tuple() {
let args = arg.as_tuple()?; let args = arg.as_tuple()?;
if args.len() == 3 { if args.len() == 3 {
let lookup_value = &args[0]; let lookup_array = &args[0];
let lookup_array = &args[1]; let lookup_value = &args[1];
let return_array = &args[2]; let return_array = &args[2];
if lookup_array.is_tuple() && return_array.is_tuple() { if lookup_array.is_tuple() && return_array.is_tuple() {
@@ -148,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).count(); let find = trail.iter().filter(|id| *id == identifier).collect::<Vec<&String>>();
if find > 0 { if find.len() > 0 {
// recursion detected // recursion detected
return None; return None;
} else { } else {
@@ -181,7 +195,7 @@ impl<'a> Context for CallbackContext<'a> {
}, },
Err(e) => { Err(e) => {
match e { match e {
EvalexprError::VariableIdentifierNotFound(_err) => { EvalexprError::VariableIdentifierNotFound(_) => {
// 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.
@@ -203,14 +217,8 @@ 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) => {
match eval_with_context(&eq[1..], self) { if let Ok(val) = eval_with_context(&eq[1..], self) {
Ok(val) => vals.push(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
},
} }
} }
} }
@@ -262,19 +270,10 @@ impl ExtractionContext {
} }
} }
pub fn dump_vars(&self) -> Vec<String> { pub fn dump_vars(&self) -> Vec<String> {
if let Ok(r) = self.var_registry.read() { if let Ok(r) = self.var_registry.read() { r.clone() } else { Vec::new() }
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() { if let Ok(r) = self.fn_registry.read() { r.clone() } else { Vec::new() }
r.clone()
} else {
Vec::new()
}
} }
} }
@@ -302,7 +301,8 @@ 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 {

View File

@@ -3,4 +3,5 @@ mod mode;
mod error_msg; mod error_msg;
mod screen; mod screen;
mod logic; mod logic;
mod clipboard; mod clipboard;
mod plot;

View File

@@ -2,8 +2,7 @@ use std::{
cmp::{max, min}, cmp::{max, min},
fmt::Display, fmt::Display,
fs, fs,
path::PathBuf, path::PathBuf, process::Command,
process::Command,
}; };
use ratatui::{ use ratatui::{
@@ -16,9 +15,9 @@ use crate::app::{
app::App, app::App,
error_msg::StatusMessage, error_msg::StatusMessage,
logic::{ logic::{
calc::{Grid, LEN}, calc::{CSV_EXT, CUSTOM_EXT, Grid, LEN},
cell::CellType, cell::CellType,
}, }, plot::Plot,
}; };
pub enum Mode { pub enum Mode {
@@ -71,11 +70,25 @@ impl Mode {
"w" => { "w" => {
// first try the passed argument as file // first try the passed argument as file
if let Some(arg) = args.get(1) { if let Some(arg) = args.get(1) {
let path: PathBuf = arg.into(); let mut path: PathBuf = arg.into();
match path.extension() {
Some(s) => {
match s.to_str() {
// leave the file alone, it already has
// a valid extension
Some(CSV_EXT) | Some(CUSTOM_EXT) => {}
_ => {
path.add_extension(CUSTOM_EXT);
}
}
}
None => {
path.add_extension(CUSTOM_EXT);
}
};
// TODO Check if the file we are writing to exists, since // TODO Check if the file exists, but the program wasn't opened with it. We might be accidentally overwriting something else.
// this code path already knows that we are writing to a new file. // let mut file = fs::OpenOptions::new().write(true).append(false).truncate(true).create(true).open(path)?;
// We might be accidentally overwriting something else.
if let Err(e) = app.grid.save_to(&path) { if let Err(e) = app.grid.save_to(&path) {
app.msg = StatusMessage::error(format!("{e}")); app.msg = StatusMessage::error(format!("{e}"));
@@ -87,7 +100,7 @@ impl Mode {
path.file_name().map(|f| f.to_str().unwrap_or("n/a")).unwrap_or("n/a") path.file_name().map(|f| f.to_str().unwrap_or("n/a")).unwrap_or("n/a")
)); ));
if app.file.is_none() { if let None = app.file {
app.file = Some(path) app.file = Some(path)
} }
} }
@@ -154,13 +167,11 @@ 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");
} }
@@ -174,40 +185,29 @@ 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);
} else { } else {
app.msg = StatusMessage::error("export <path.csv>") app.msg = StatusMessage::error("export <path.csv>")
} }
app.mode = Mode::Normal app.mode = Mode::Normal
} }
"plot" => { "plot" => {
app.plot_popup = Some(Plot::new(0, 1));
return;
// 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) { arg1 } else { "plot.png" }; let output_filename = if let Some(arg1) = args.get(1) {
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");
@@ -218,18 +218,10 @@ impl Mode {
let s = s.replace("$OUTPUT", "/tmp/output.png"); let s = s.replace("$OUTPUT", "/tmp/output.png");
let _ = fs::write("/tmp/plot.p", s); let _ = fs::write("/tmp/plot.p", s);
let cmd_res = Command::new("gnuplot").arg("/tmp/plot.p").output(); let _ = Command::new("gnuplot").arg("/tmp/plot.p").output();
if let Err(err) = cmd_res { let _ = fs::copy("/tmp/output.png", output_filename);
match err.kind() {
std::io::ErrorKind::NotFound => { app.msg = StatusMessage::info("Wrote gnuplot data to /tmp");
app.msg = StatusMessage::error("Error - Is gnuplot installed?")
}
_ => app.msg = StatusMessage::error(format!("{err}")),
};
} else {
let _ = fs::copy("/tmp/output.png", output_filename);
app.msg = StatusMessage::info(format!("Created {output_filename}. Artifacts are in /tmp"));
}
app.mode = Mode::Normal app.mode = Mode::Normal
} }
_ => {} _ => {}
@@ -270,23 +262,11 @@ impl Mode {
app.grid.mv_cursor_to(0, y); app.grid.mv_cursor_to(0, y);
return; return;
} }
// Go to end of row
'$' => {
let (_, y) = app.grid.cursor();
app.grid.mv_cursor_to(super::logic::calc::LEN-1, y);
return;
}
// Go to bottom of column
'G' => {
let (x, _) = app.grid.cursor();
app.grid.mv_cursor_to(x, super::logic::calc::LEN-1);
return;
}
// edit cell // edit cell
'i' | 'a' => { 'i' | 'a' => {
let (x, y) = app.grid.cursor(); let (x, y) = app.grid.cursor();
let val = app.grid.get_cell_raw(x, y).as_ref().map(|f| f.to_string()).unwrap_or_default(); let val = app.grid.get_cell_raw(x, y).as_ref().map(|f| f.to_string()).unwrap_or(String::new());
app.mode = Mode::Insert(Chord::from(val)); app.mode = Mode::Insert(Chord::from(val));
} }
@@ -322,11 +302,6 @@ 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());
@@ -400,34 +375,6 @@ impl Mode {
app.grid.mv_cursor_to(x, 0); app.grid.mv_cursor_to(x, 0);
app.mode = Mode::Normal; app.mode = Mode::Normal;
} }
// Go to the bottom of the current window
("g", 'G') => {
let (x, _) = app.grid.cursor();
let (_, y_height) = app.screen.get_screen_size(&app.vars);
let y_origin = app.screen.scroll_y();
app.grid.mv_cursor_to(x, y_origin + y_height);
app.mode = Mode::Normal;
return;
}
// Go to the right edge of the current window
("g", '$') => {
let (_, y) = app.grid.cursor();
let (x_width, _) = app.screen.get_screen_size(&app.vars);
let x_origin = app.screen.scroll_x();
app.grid.mv_cursor_to(x_origin + x_width, y);
app.mode = Mode::Normal;
}
// Go to the left edge of the current window
("g", '0') => {
let (_, y) = app.grid.cursor();
let x_origin = app.screen.scroll_x();
app.grid.mv_cursor_to(x_origin, y);
app.mode = Mode::Normal;
return;
}
// center screen to cursor // center screen to cursor
("z", 'z') => { ("z", 'z') => {
app.screen.center_x(app.grid.cursor(), &app.vars); app.screen.center_x(app.grid.cursor(), &app.vars);
@@ -477,12 +424,14 @@ impl Mode {
let len = match &self { let len = match &self {
Mode::Insert(edit) | Mode::VisualCmd(_, edit) | Mode::Command(edit) | Mode::Chord(edit) => edit.len(), Mode::Insert(edit) | Mode::VisualCmd(_, edit) | Mode::Command(edit) | Mode::Chord(edit) => edit.len(),
Mode::Normal => { Mode::Normal => {
cell.as_ref().map(|f| f.to_string().len()).unwrap_or_default() let len = cell.as_ref().map(|f| f.to_string().len()).unwrap_or_default();
len
} }
Mode::Visual(_) => 0, Mode::Visual(_) => 0,
}; };
// min 20 chars, expand if needed // min 20 chars, expand if needed
max(len as u16 + 1, 20) let len = max(len as u16 + 1, 20);
len
} }
pub fn render(&self, f: &mut ratatui::Frame, area: prelude::Rect, cell: &Option<CellType>) { pub fn render(&self, f: &mut ratatui::Frame, area: prelude::Rect, cell: &Option<CellType>) {
@@ -496,7 +445,8 @@ impl Mode {
Mode::Chord(chord) => f.render_widget(chord, area), Mode::Chord(chord) => f.render_widget(chord, area),
Mode::Normal => f.render_widget( Mode::Normal => f.render_widget(
Paragraph::new({ Paragraph::new({
cell.as_ref().map(|f| f.to_string()).unwrap_or_default() let cell = cell.as_ref().map(|f| f.to_string()).unwrap_or_default();
cell
}), }),
area, area,
), ),
@@ -513,15 +463,20 @@ 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 { buf: b } Chord {
buf: b,
}
} }
} }
impl Chord { impl Chord {
pub fn new(inital: char) -> Self { pub fn new(inital: char) -> Self {
let buf = vec![inital]; let mut buf = Vec::new();
buf.push(inital);
Self { buf } Self {
buf,
}
} }
pub fn backspace(&mut self) { pub fn backspace(&mut self) {

45
src/app/plot.rs Normal file
View File

@@ -0,0 +1,45 @@
use ratatui::{layout::{Constraint, Direction, Layout}, widgets::{Paragraph, Widget}};
pub struct Plot {
x: usize,
y: Vec<usize>,
}
impl Plot {
pub fn new(x: usize, y: usize) -> Self {
Self {
x,
y: vec![y],
}
}
pub fn add_column(&mut self) {
self.y.push(1)
}
pub fn del_column(&mut self) {
self.y.pop();
}
pub fn process_key(&mut self, c: char) {
}
}
impl Widget for &Plot {
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) {
// plus 1 for x
let columns = self.y.len() + 1;
let mut constraints = Vec::new();
for _ in 0..=columns {
constraints.push(Constraint::Min(1));
}
Paragraph::new("Foobar").render(area, buf);
let layout = Layout::default()
.direction(Direction::Vertical)
.constraints(constraints)
.split(area);
let x_space = layout[0];
}
}

View File

@@ -1,16 +1,13 @@
use std::{cmp::min, collections::HashMap, sync::RwLock}; use std::{collections::HashMap, sync::RwLock};
use ratatui::prelude; use ratatui::prelude;
use crate::app::logic::calc::LEN; use crate::app::logic::calc::LEN;
pub struct ScreenSpace { pub struct ScreenSpace {
/// This is measured in cells. /// This is measured in cells
/// This is the top-left cell index.
scroll: (usize, usize), scroll: (usize, usize),
/// In chars
default_cell_len: usize, default_cell_len: usize,
/// In chars
default_cell_hight: usize, default_cell_hight: usize,
/// This is measured in chars /// This is measured in chars
last_seen_screen_size: RwLock<(usize, usize)> last_seen_screen_size: RwLock<(usize, usize)>
@@ -20,84 +17,74 @@ impl ScreenSpace {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
scroll: (0, 0), scroll: (0, 0),
default_cell_len: 9, default_cell_len: 10,
default_cell_hight: 1, default_cell_hight: 1,
last_seen_screen_size: RwLock::new((0,0)) last_seen_screen_size: RwLock::new((0,0))
} }
} }
pub fn center_x(&mut self, (cursor_x, _): (usize, usize), vars: &HashMap<String, String>) { pub fn center_x(&mut self, (cursor_x, _): (usize, usize), vars: &HashMap<String, String>) {
let (x_cells, _) = self.get_screen_size(vars);
let x_center = self.scroll_x() + (x_cells/2);
let delta = cursor_x as isize - x_center as isize;
self.scroll.0 = self.scroll.0.saturating_add_signed(delta);
}
pub fn center_y(&mut self, (_, cursor_y): (usize, usize), vars: &HashMap<String, String>) {
let (_, y_cells) = self.get_screen_size(vars);
let y_center = self.scroll_y() + (y_cells/2);
let delta = cursor_y as isize - y_center as isize;
self.scroll.1 = self.scroll.1.saturating_add_signed(delta);
}
/// In chars
pub fn get_screen_size(&self, vars: &HashMap<String, String>) -> (usize, usize) {
if let Ok(screen_size) = self.last_seen_screen_size.read() { if let Ok(screen_size) = self.last_seen_screen_size.read() {
// ======= X ======= let x_cells = (screen_size.0 / self.get_cell_width(vars) as usize) -2;
// screen seems to be 2 cells smaller than it should be let x_center = self.scroll_x() + (x_cells/2);
// this is probably related to issue #6
let x_cells = (screen_size.0 / self.get_cell_width(vars)) -2; let delta = cursor_x as isize - x_center as isize;
// ======= Y ======= self.scroll.0 = self.scroll.0.saturating_add_signed(delta);
// screen seems to be 2 cells smaller than it should be }
// this is probably related to issue #6 }
let y_cells = (screen_size.1 / self.get_cell_height(vars)) -2; pub fn center_y(&mut self, (_, cursor_y): (usize, usize), vars: &HashMap<String, String>) {
(x_cells,y_cells) if let Ok(screen_size) = self.last_seen_screen_size.read() {
} else { let y_cells = (screen_size.1 / self.get_cell_height(vars) as usize) -2;
(0,0) let y_center = self.scroll_y() + (y_cells/2);
let delta = cursor_y as isize - y_center as isize;
self.scroll.1 = self.scroll.1.saturating_add_signed(delta);
} }
} }
pub fn scroll_based_on_cursor_location(&mut self, (cursor_x, cursor_y): (usize, usize), vars: &HashMap<String, String>) { pub fn scroll_based_on_cursor_location(&mut self, (cursor_x, cursor_y): (usize, usize), vars: &HashMap<String, String>) {
let (x_cells, y_cells) = self.get_screen_size(vars); if let Ok(screen_size) = self.last_seen_screen_size.read() {
// ======= X =======
// screen seems to be 2 cells smaller than it should be
// this is probably related to issue #6
let x_cells = (screen_size.0 / self.get_cell_width(vars) as usize) -2;
let lower_x = self.scroll_x();
let upper_x = self.scroll_x() + x_cells;
if cursor_x < lower_x {
let delta = lower_x - cursor_x;
self.scroll.0 = self.scroll.0.saturating_sub(delta);
}
if cursor_x > upper_x {
let delta = cursor_x - upper_x;
self.scroll.0 = self.scroll.0.saturating_add(delta);
}
let lower_x = self.scroll_x(); // ======= Y =======
let upper_x = self.scroll_x() + x_cells; // screen seems to be 2 cells smaller than it should be
// this is probably related to issue #6
let y_cells = (screen_size.1 / self.get_cell_height(vars) as usize) -2;
let lower_y = self.scroll_y();
let upper_y = self.scroll_y() + y_cells;
if cursor_x < lower_x { if cursor_y < lower_y {
let delta = lower_x - cursor_x; let delta = lower_y - cursor_y;
self.scroll.0 = self.scroll.0.saturating_sub(delta); self.scroll.1 = self.scroll.1.saturating_sub(delta);
} }
if cursor_x > upper_x {
let delta = cursor_x - upper_x;
self.scroll.0 = self.scroll.0.saturating_add(delta);
}
if cursor_y > upper_y {
let lower_y = self.scroll_y(); let delta = cursor_y - upper_y;
let upper_y = self.scroll_y() + y_cells; self.scroll.1 = self.scroll.1.saturating_add(delta);
}
if cursor_y < lower_y {
let delta = lower_y - cursor_y;
self.scroll.1 = self.scroll.1.saturating_sub(delta);
}
if cursor_y > upper_y {
let delta = cursor_y - upper_y;
self.scroll.1 = self.scroll.1.saturating_add(delta);
} }
} }
pub fn scroll_x(&self) -> usize { pub fn scroll_x(&self) -> usize {
self.scroll.0 self.scroll.0
} }
pub fn scroll_y(&self) -> usize{ pub fn scroll_y(&self) -> usize{
self.scroll.1 self.scroll.1
} }
pub fn get_cell_height(&self, vars: &HashMap<String, String>) -> usize { pub fn get_cell_height(&self, vars: &HashMap<String, String>) -> usize {
if let Some(h) = vars.get("height") { if let Some(h) = vars.get("height") {
if let Ok(p) = h.parse::<usize>() { if let Ok(p) = h.parse::<usize>() {
@@ -106,7 +93,6 @@ impl ScreenSpace {
} }
return self.default_cell_hight return self.default_cell_hight
} }
pub fn get_cell_width(&self, vars: &HashMap<String, String>) -> usize { pub fn get_cell_width(&self, vars: &HashMap<String, String>) -> usize {
if let Some(h) = vars.get("length") { if let Some(h) = vars.get("length") {
if let Ok(p) = h.parse::<usize>() { if let Ok(p) = h.parse::<usize>() {
@@ -114,24 +100,19 @@ impl ScreenSpace {
} }
} }
self.default_cell_len self.default_cell_len
} }
pub fn how_many_cells_fit_in(&self, area: &prelude::Rect, vars: &HashMap<String, String>) -> (u16, u16) { pub fn how_many_cells_fit_in(&self, area: &prelude::Rect, vars: &HashMap<String, String>) -> (u16, u16) {
if let Ok(mut l) = self.last_seen_screen_size.write() { if let Ok(mut l) = self.last_seen_screen_size.write() {
l.0 = area.width as usize; l.0 = area.width as usize;
l.1 = area.height as usize; l.1 = area.height as usize;
} }
let width = (area.width as usize / self.get_cell_width(vars)) + 1; let x_max =
let height = area.height as usize / self.get_cell_height(vars); if area.width as usize / self.get_cell_width(vars) > LEN { LEN - 1 } else { area.width as usize / self.get_cell_width(vars)};
let y_max =
let x_max = min(LEN-1, width); if area.height as usize / self.get_cell_height(vars) > LEN { LEN - 1 } else { area.height as usize / self.get_cell_height(vars)};
let y_max = min(LEN-1, height);
(x_max as u16, y_max as u16) (x_max as u16, y_max as u16)
} }
} }
#[test] #[test]

View File

@@ -1,10 +1,3 @@
#![allow(clippy::needless_return)]
#![allow(clippy::len_zero)]
#![allow(clippy::collapsible_if)]
#![allow(clippy::collapsible_else_if)]
#![allow(clippy::collapsible_match)]
#![allow(clippy::single_match)]
mod app; mod app;
use std::env::args; use std::env::args;

View File

@@ -14,5 +14,4 @@ set grid
set term png size 1280, 720 set term png size 1280, 720
set output '$OUTPUT' set output '$OUTPUT'
plot datafile using 1:2 with linespoints linestyle 1 plot datafile using 1:2 with linespoints linestyle 1
# plot datafile using 1:2 with linespoints linestyle 1, datafile using 1:3, etc...
replot replot