From 626b8ff4eb41df702c39596619e7aa17ba46f7b8 Mon Sep 17 00:00:00 2001 From: Rushmore75 Date: Tue, 27 Jan 2026 10:52:18 -0700 Subject: [PATCH] code cleanup --- src/app/app.rs | 7 ++++--- src/app/logic/calc.rs | 5 +++++ src/app/screen.rs | 27 +++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/app/app.rs b/src/app/app.rs index 457a80f..d26ef13 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -18,7 +18,7 @@ use ratatui::{ use crate::app::{ clipboard::Clipboard, error_msg::StatusMessage, - logic::{self, calc::Grid, cell::CellType}, + logic::{self, calc::{Grid, get_header_size}, cell::CellType}, mode::Mode, screen::ScreenSpace, }; @@ -66,7 +66,7 @@ impl Widget for &App { let mut style = Style::new().fg(Color::White); // Custom width for the header of each row - let row_header_width: u16 = logic::calc::LEN.to_string().len() as u16; + 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; @@ -78,7 +78,7 @@ impl Widget for &App { let mut x_idx: usize = 0; let mut y_idx: usize = 0; if x != 0 { - x_idx = x as usize - 1 + self.screen.scroll_x(); + x_idx = x as usize -1 + self.screen.scroll_x(); } if y != 0 { y_idx = y as usize - 1 + self.screen.scroll_y(); @@ -199,6 +199,7 @@ impl Widget for &App { } } } + if should_render { let mut x_off = area.x + (x * cell_width); let y_off = area.y + (y * cell_height); diff --git a/src/app/logic/calc.rs b/src/app/logic/calc.rs index 60849ca..851c4c7 100644 --- a/src/app/logic/calc.rs +++ b/src/app/logic/calc.rs @@ -17,6 +17,11 @@ use crate::app::{ #[cfg(test)] use crate::app::app::App; +pub fn get_header_size() -> usize { + let row_header_width = LEN.to_string().len(); + row_header_width +} + pub const LEN: usize = 1001; pub const CSV_EXT: &str = "csv"; pub const CUSTOM_EXT: &str = "nscim"; diff --git a/src/app/screen.rs b/src/app/screen.rs index 7d76d36..90524d7 100644 --- a/src/app/screen.rs +++ b/src/app/screen.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, sync::RwLock}; use ratatui::prelude; -use crate::app::logic::calc::LEN; +use crate::app::logic::calc::{self, LEN}; pub struct ScreenSpace { /// This is measured in cells. @@ -20,7 +20,7 @@ impl ScreenSpace { pub fn new() -> Self { Self { scroll: (0, 0), - default_cell_len: 10, + default_cell_len: 9, default_cell_hight: 1, last_seen_screen_size: RwLock::new((0,0)) } @@ -33,6 +33,7 @@ impl ScreenSpace { 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) { let (_, y_cells) = self.get_screen_size(vars); let y_center = self.scroll_y() + (y_cells/2); @@ -92,9 +93,11 @@ impl ScreenSpace { pub fn scroll_x(&self) -> usize { self.scroll.0 } + pub fn scroll_y(&self) -> usize{ self.scroll.1 } + pub fn get_cell_height(&self, vars: &HashMap) -> usize { if let Some(h) = vars.get("height") { if let Ok(p) = h.parse::() { @@ -103,6 +106,7 @@ impl ScreenSpace { } return self.default_cell_hight } + pub fn get_cell_width(&self, vars: &HashMap) -> usize { if let Some(h) = vars.get("length") { if let Ok(p) = h.parse::() { @@ -110,19 +114,34 @@ impl ScreenSpace { } } self.default_cell_len + } + pub fn how_many_cells_fit_in(&self, area: &prelude::Rect, vars: &HashMap) -> (u16, u16) { if let Ok(mut l) = self.last_seen_screen_size.write() { l.0 = area.width as usize; l.1 = area.height as usize; } + // let width = (area.width as usize + calc::get_header_size() -1) / self.get_cell_width(vars); + let width = area.width as usize / self.get_cell_width(vars); + let height = area.height as usize / self.get_cell_height(vars); + let x_max = - if area.width as usize / self.get_cell_width(vars) > LEN { LEN - 1 } else { area.width as usize / self.get_cell_width(vars)}; + if width > LEN { + LEN - 1 + } else { + width + }; let y_max = - if area.height as usize / self.get_cell_height(vars) > LEN { LEN - 1 } else { area.height as usize / self.get_cell_height(vars)}; + if height > LEN { + LEN - 1 + } else { + height + }; (x_max as u16, y_max as u16) } + } #[test]