This commit is contained in:
2025-11-11 15:30:07 -07:00
parent 48186cd680
commit e92d6b309e
3 changed files with 44 additions and 18 deletions

View File

@@ -10,9 +10,9 @@ use ratatui::{
}; };
use crate::app::{ use crate::app::{
calc::{Grid, LEN}, calc::Grid,
error_msg::ErrorMessage, error_msg::ErrorMessage,
mode::{Chord, Mode}, screen::ScreenSpace, mode::Mode, screen::ScreenSpace,
}; };
pub struct App { pub struct App {
@@ -321,7 +321,7 @@ fn test_quit_cmd() {
let mut app = App::new(); let mut app = App::new();
assert!(!app.exit); 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); Mode::process_cmd(&mut app);
assert!(app.exit); assert!(app.exit);

View File

@@ -1,10 +1,10 @@
use std::{ use std::{
cmp::{max, min}, cmp::min,
fmt::Display, fmt::Display,
}; };
use ratatui::{ 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}; use crate::app::{app::App, calc::LEN, error_msg::ErrorMessage};
@@ -152,11 +152,13 @@ impl Mode {
Mode::Chord(chord) => { Mode::Chord(chord) => {
chord.add_char(key); chord.add_char(key);
// the chord starts with a :, send it over to be a comman
if chord.buf[0] == ':' { if chord.buf[0] == ':' {
app.mode = Mode::Command(Chord::new(':')); app.mode = Mode::Command(Chord::new(':'));
return; return;
} }
// Try and parse out a preceding number
match chord.as_string()[0..chord.as_string().len() - 1].parse::<usize>() { match chord.as_string()[0..chord.as_string().len() - 1].parse::<usize>() {
// For chords that can take a numeric input // For chords that can take a numeric input
Ok(num) => match key { Ok(num) => match key {
@@ -184,6 +186,11 @@ impl Mode {
app.grid.selected_cell.1 = 0; app.grid.selected_cell.1 = 0;
app.mode = Mode::Normal; 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;
}
_ => {} _ => {}
}, },
} }

View File

@@ -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 { pub struct ScreenSpace {
/// This is measured in cells /// This is measured in cells
@@ -23,14 +23,33 @@ impl ScreenSpace {
} }
} }
pub fn center_x(&mut self, (cursor_x, _): (usize, usize), vars: &HashMap<String, String>) {
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<String, String>) {
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<String, String>) { pub fn scroll_based_on_cursor_location(&mut self, (cursor_x, cursor_y): (usize, usize), vars: &HashMap<String, String>) {
if let Ok(screen_size) = self.last_seen_screen_size.read() { if let Ok(screen_size) = self.last_seen_screen_size.read() {
// ======= X ======= // ======= 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 // 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 { if cursor_x < lower_x {
let delta = lower_x - cursor_x; let delta = lower_x - cursor_x;
@@ -42,11 +61,11 @@ impl ScreenSpace {
} }
// ======= Y ======= // ======= 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 // 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 { if cursor_y < lower_y {
let delta = lower_y - cursor_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("length".to_string(), 10.to_string());
app.vars.insert("height".to_string(), 1.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!(x, 18);
assert_eq!(y, 14); assert_eq!(y, 14);
} }
@@ -117,7 +136,7 @@ fn scroll() {
// We have to check how many cells fit, because screen learns the width // We have to check how many cells fit, because screen learns the width
// of the area by rumour here. // 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!(x, 18);
assert_eq!(y, 14); assert_eq!(y, 14);