From 813e48920c5513c90590a7381ea8c3260c348b1d Mon Sep 17 00:00:00 2001 From: alice pellerin Date: Tue, 17 Mar 2026 00:12:36 -0500 Subject: [PATCH] add up/down/forward/backward scroll commands --- src/app/mod.rs | 40 +++++++++++++++++++++++++++++++++++++--- src/app/widget.rs | 2 +- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index 0656628..4feb1e7 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,5 +1,5 @@ use std::{cmp::min, env, fs::File, io::Read, process::exit}; -use crossterm::event::{self, Event, KeyCode, KeyModifiers}; +use crossterm::{event::{self, Event, KeyCode, KeyModifiers}, terminal::window_size}; use crate::{BYTES_PER_LINE, cursor::Cursor}; @@ -8,6 +8,7 @@ mod widget; #[derive(Debug)] pub struct App { pub contents: Vec, + pub window_rows: u16, pub scroll_position: usize, pub cursor: Cursor, pub should_quit: bool @@ -32,27 +33,60 @@ impl App { Self { contents, + window_rows: window_size().unwrap().rows, scroll_position: 0, cursor: Cursor::default(), should_quit: false } } + #[allow(clippy::too_many_lines)] pub fn handle_events(&mut self) { #[allow(clippy::collapsible_match)] match event::read().unwrap() { + Event::Resize(_, height) => { + self.window_rows = height; + } Event::Key(key_event) if key_event.code == KeyCode::Char('q') => { self.should_quit = true; } Event::Key(key_event) if key_event.modifiers.contains(KeyModifiers::CONTROL) && key_event.code == KeyCode::Char('e') => { - let max_scroll_position = self.contents.len() - 0x50; - self.scroll_position = min(self.scroll_position + BYTES_PER_LINE, max_scroll_position); + self.scroll_position = min( + self.scroll_position + BYTES_PER_LINE, + self.contents.len() - (5 * BYTES_PER_LINE) + ); } Event::Key(key_event) if key_event.modifiers.contains(KeyModifiers::CONTROL) && key_event.code == KeyCode::Char('y') => { self.scroll_position = self.scroll_position.saturating_sub(BYTES_PER_LINE); } + Event::Key(key_event) if key_event.modifiers.contains(KeyModifiers::CONTROL) && + key_event.code == KeyCode::Char('d') => { + self.scroll_position = min( + self.scroll_position + ((self.window_rows / 2) as usize * BYTES_PER_LINE), + self.contents.len() - (5 * BYTES_PER_LINE) + ); + } + Event::Key(key_event) if key_event.modifiers.contains(KeyModifiers::CONTROL) && + key_event.code == KeyCode::Char('u') => { + self.scroll_position = self.scroll_position.saturating_sub( + (self.window_rows / 2) as usize * BYTES_PER_LINE + ); + } + Event::Key(key_event) if key_event.modifiers.contains(KeyModifiers::CONTROL) && + key_event.code == KeyCode::Char('f') => { + self.scroll_position = min( + self.scroll_position + (self.window_rows as usize * BYTES_PER_LINE), + self.contents.len() - (5 * BYTES_PER_LINE) + ); + } + Event::Key(key_event) if key_event.modifiers.contains(KeyModifiers::CONTROL) && + key_event.code == KeyCode::Char('b') => { + self.scroll_position = self.scroll_position.saturating_sub( + self.window_rows as usize * BYTES_PER_LINE + ); + } Event::Key(key_event) if key_event.code == KeyCode::Char('i') => { if self.cursor.head >= 0x10 { self.cursor.head -= 0x10; diff --git a/src/app/widget.rs b/src/app/widget.rs index cd44183..d96c90e 100644 --- a/src/app/widget.rs +++ b/src/app/widget.rs @@ -53,7 +53,7 @@ mod address { } mod hex { - use std::{borrow::Cow, iter, mem}; + use std::{borrow::Cow, mem}; use itertools::Itertools; use ratatui::{style::{Color, Style, Stylize}, text::Span};