close #24
This commit is contained in:
@@ -18,7 +18,7 @@ use ratatui::{
|
|||||||
use crate::app::{
|
use crate::app::{
|
||||||
clipboard::Clipboard,
|
clipboard::Clipboard,
|
||||||
error_msg::StatusMessage,
|
error_msg::StatusMessage,
|
||||||
logic::{calc::Grid, cell::CellType},
|
logic::{self, calc::Grid, cell::CellType},
|
||||||
mode::Mode,
|
mode::Mode,
|
||||||
screen::ScreenSpace,
|
screen::ScreenSpace,
|
||||||
};
|
};
|
||||||
@@ -66,7 +66,9 @@ impl Widget for &App {
|
|||||||
let mut style = Style::new().fg(Color::White);
|
let mut style = Style::new().fg(Color::White);
|
||||||
|
|
||||||
// Custom width for the header of each row
|
// Custom width for the header of each row
|
||||||
let row_header_width: u16 = 4;
|
let row_header_width: u16 = logic::calc::LEN.to_string().len() 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_width = self.screen.get_cell_width(&self.vars) as u16;
|
||||||
let cell_height = self.screen.get_cell_height(&self.vars) as u16;
|
let cell_height = self.screen.get_cell_height(&self.vars) as u16;
|
||||||
|
|
||||||
|
|||||||
@@ -266,6 +266,18 @@ 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, y);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Go to bottom of column
|
||||||
|
'G' => {
|
||||||
|
let (x, _) = app.grid.cursor();
|
||||||
|
app.grid.mv_cursor_to(x, super::logic::calc::LEN,);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// edit cell
|
// edit cell
|
||||||
'i' | 'a' => {
|
'i' | 'a' => {
|
||||||
let (x, y) = app.grid.cursor();
|
let (x, y) = app.grid.cursor();
|
||||||
@@ -379,6 +391,34 @@ 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);
|
||||||
|
|||||||
@@ -5,9 +5,12 @@ 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)>
|
||||||
@@ -24,30 +27,41 @@ impl ScreenSpace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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>) {
|
||||||
if let Ok(screen_size) = self.last_seen_screen_size.read() {
|
let (x_cells, _) = self.get_screen_size(vars);
|
||||||
let x_cells = (screen_size.0 / self.get_cell_width(vars) as usize) -2;
|
|
||||||
let x_center = self.scroll_x() + (x_cells/2);
|
let x_center = self.scroll_x() + (x_cells/2);
|
||||||
|
|
||||||
let delta = cursor_x as isize - x_center as isize;
|
let delta = cursor_x as isize - x_center as isize;
|
||||||
self.scroll.0 = self.scroll.0.saturating_add_signed(delta);
|
self.scroll.0 = self.scroll.0.saturating_add_signed(delta);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
pub fn center_y(&mut self, (_, cursor_y): (usize, usize), vars: &HashMap<String, String>) {
|
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) = self.get_screen_size(vars);
|
||||||
let y_cells = (screen_size.1 / self.get_cell_height(vars) as usize) -2;
|
|
||||||
let y_center = self.scroll_y() + (y_cells/2);
|
let y_center = self.scroll_y() + (y_cells/2);
|
||||||
|
|
||||||
let delta = cursor_y as isize - y_center as isize;
|
let delta = cursor_y as isize - y_center as isize;
|
||||||
self.scroll.1 = self.scroll.1.saturating_add_signed(delta);
|
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>) {
|
/// 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 =======
|
// ======= X =======
|
||||||
// screen seems to be 2 cells smaller than it should be
|
// screen seems to be 2 cells smaller than it should be
|
||||||
// this is probably related to issue #6
|
// this is probably related to issue #6
|
||||||
let x_cells = (screen_size.0 / self.get_cell_width(vars) as usize) -2;
|
let x_cells = (screen_size.0 / self.get_cell_width(vars) as usize) -2;
|
||||||
|
// ======= Y =======
|
||||||
|
// 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;
|
||||||
|
(x_cells,y_cells)
|
||||||
|
} else {
|
||||||
|
(0,0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
|
||||||
let lower_x = self.scroll_x();
|
let lower_x = self.scroll_x();
|
||||||
let upper_x = self.scroll_x() + x_cells;
|
let upper_x = self.scroll_x() + x_cells;
|
||||||
|
|
||||||
@@ -60,10 +74,7 @@ impl ScreenSpace {
|
|||||||
self.scroll.0 = self.scroll.0.saturating_add(delta);
|
self.scroll.0 = self.scroll.0.saturating_add(delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ======= Y =======
|
|
||||||
// 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 lower_y = self.scroll_y();
|
||||||
let upper_y = self.scroll_y() + y_cells;
|
let upper_y = self.scroll_y() + y_cells;
|
||||||
|
|
||||||
@@ -77,7 +88,6 @@ impl ScreenSpace {
|
|||||||
self.scroll.1 = self.scroll.1.saturating_add(delta);
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user