diff --git a/src/app/app.rs b/src/app/app.rs index 8d54ccc..3f4c4d0 100644 --- a/src/app/app.rs +++ b/src/app/app.rs @@ -10,9 +10,9 @@ use ratatui::{ }; use crate::app::{ - calc::{Grid, LEN}, + calc::Grid, error_msg::ErrorMessage, - mode::{Chord, Mode}, screen::ScreenSpace, + mode::Mode, screen::ScreenSpace, }; pub struct App { @@ -321,7 +321,7 @@ fn test_quit_cmd() { let mut app = App::new(); assert!(!app.exit); - app.mode = Mode::Command(Chord::from(":q".to_string())); + app.mode = Mode::Command(crate::app::mode::Chord::from(":q".to_string())); Mode::process_cmd(&mut app); assert!(app.exit); diff --git a/src/app/mode.rs b/src/app/mode.rs index 4a9a902..2586aaf 100644 --- a/src/app/mode.rs +++ b/src/app/mode.rs @@ -1,10 +1,10 @@ use std::{ - cmp::{max, min}, + cmp::min, fmt::Display, }; use ratatui::{ - layout::Rect, prelude, style::{Color, Style}, widgets::{Paragraph, Widget} + prelude, style::{Color, Style}, widgets::{Paragraph, Widget} }; use crate::app::{app::App, calc::LEN, error_msg::ErrorMessage}; @@ -152,11 +152,13 @@ impl Mode { Mode::Chord(chord) => { chord.add_char(key); + // the chord starts with a :, send it over to be a comman if chord.buf[0] == ':' { app.mode = Mode::Command(Chord::new(':')); return; } + // Try and parse out a preceding number match chord.as_string()[0..chord.as_string().len() - 1].parse::() { // For chords that can take a numeric input Ok(num) => match key { @@ -184,6 +186,11 @@ impl Mode { app.grid.selected_cell.1 = 0; app.mode = Mode::Normal; } + "zz" => { + app.screen.center_x(app.grid.selected_cell, &app.vars); + app.screen.center_y(app.grid.selected_cell, &app.vars); + app.mode = Mode::Normal; + } _ => {} }, } diff --git a/src/app/screen.rs b/src/app/screen.rs index 54623e6..a5ce25c 100644 --- a/src/app/screen.rs +++ b/src/app/screen.rs @@ -1,8 +1,8 @@ -use std::{collections::HashMap, sync::RwLock}; +use std::{collections::HashMap, env::VarError, sync::RwLock}; -use ratatui::{layout::Rect, prelude}; +use ratatui::prelude; -use crate::app::calc::LEN; +use crate::app::{app::App, calc::LEN}; pub struct ScreenSpace { /// This is measured in cells @@ -23,14 +23,33 @@ impl ScreenSpace { } } + pub fn center_x(&mut self, (cursor_x, _): (usize, usize), vars: &HashMap) { + if let Ok(screen_size) = self.last_seen_screen_size.read() { + let x_cells = (screen_size.0 / self.get_cell_width(vars) as usize) -2; + 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) { + if let Ok(screen_size) = self.last_seen_screen_size.read() { + let y_cells = (screen_size.1 / self.get_cell_height(vars) as usize) -2; + 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) { 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 ; - - let lower_x = self.scroll_x(); // screen seems to be 2 cells smaller than it should be - let upper_x = self.scroll_x() + x_cells -2; + // 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; @@ -42,11 +61,11 @@ impl ScreenSpace { } // ======= Y ======= - let y_cells = screen_size.1 / self.get_cell_height(vars) as usize; - - let lower_y = self.scroll_y(); // screen seems to be 2 cells smaller than it should be - let upper_y = self.scroll_y() + y_cells -2; + // 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_y < lower_y { let delta = lower_y - cursor_y; @@ -103,7 +122,7 @@ fn fit_cells() { app.vars.insert("length".to_string(), 10.to_string()); app.vars.insert("height".to_string(), 1.to_string()); - let (x,y) = app.screen.how_many_cells_fit_in(&Rect::new(0, 0, 181, 14), &app.vars); + let (x,y) = app.screen.how_many_cells_fit_in(&prelude::Rect::new(0, 0, 181, 14), &app.vars); assert_eq!(x, 18); assert_eq!(y, 14); } @@ -117,7 +136,7 @@ fn scroll() { // We have to check how many cells fit, because screen learns the width // of the area by rumour here. - let (x,y) = app.screen.how_many_cells_fit_in(&Rect::new(0, 0, 181, 14), &app.vars); + let (x,y) = app.screen.how_many_cells_fit_in(&prelude::Rect::new(0, 0, 181, 14), &app.vars); assert_eq!(x, 18); assert_eq!(y, 14);