add cursor, hjklweb;

This commit is contained in:
alice pellerin
2026-03-16 20:44:15 -05:00
parent 9f17ea00f9
commit 7a18c6cbfe
4 changed files with 418 additions and 46 deletions
+65 -8
View File
@@ -1,7 +1,7 @@
use std::{cmp::min, env, fs::File, io::Read, process::exit};
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use crate::BYTES_PER_LINE;
use crate::{BYTES_PER_LINE, cursor::Cursor};
mod widget;
@@ -9,7 +9,7 @@ mod widget;
pub struct App {
pub contents: Vec<u8>,
pub scroll_position: usize,
// pub cursor_position: usize,
pub cursor: Cursor,
pub should_quit: bool
}
@@ -33,25 +33,82 @@ impl App {
Self {
contents,
scroll_position: 0,
// cursor_position: 0,
should_quit: false,
cursor: Cursor::default(),
should_quit: false
}
}
pub fn handle_events(&mut self) {
#[allow(clippy::collapsible_match)]
match event::read().unwrap() {
Event::Key(key_event) if key_event.code == KeyCode::Char('q') => {
self.should_quit = true;
}
Event::Key(key_event) if key_event.code == KeyCode::Char('e') &&
key_event.modifiers.contains(KeyModifiers::CONTROL) => {
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);
}
Event::Key(key_event) if key_event.code == KeyCode::Char('y') &&
key_event.modifiers.contains(KeyModifiers::CONTROL) => {
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.code == KeyCode::Char('i') => {
if self.cursor.head >= 0x10 {
self.cursor.head -= 0x10;
self.cursor.collapse();
}
}
Event::Key(key_event) if key_event.code == KeyCode::Char('j') => {
if self.cursor.head >= 1 {
self.cursor.head -= 1;
self.cursor.collapse();
}
}
Event::Key(key_event) if key_event.code == KeyCode::Char('k') => {
if self.contents.len() - 1 - self.cursor.head >= 0x10 {
self.cursor.head += 0x10;
self.cursor.collapse();
}
}
Event::Key(key_event) if key_event.code == KeyCode::Char('l') => {
if self.contents.len() - 1 - self.cursor.head >= 1 {
self.cursor.head += 1;
self.cursor.collapse();
}
}
Event::Key(key_event) if key_event.code == KeyCode::Char('I') => {
if self.cursor.head >= 0x10 {
self.cursor.head -= 0x10;
}
}
Event::Key(key_event) if key_event.code == KeyCode::Char('J') => {
if self.cursor.head >= 1 {
self.cursor.head -= 1;
}
}
Event::Key(key_event) if key_event.code == KeyCode::Char('K') => {
if self.contents.len() - 1 - self.cursor.head >= 0x10 {
self.cursor.head += 0x10;
}
}
Event::Key(key_event) if key_event.code == KeyCode::Char('L') => {
if self.contents.len() - 1 - self.cursor.head >= 1 {
self.cursor.head += 1;
}
}
Event::Key(key_event) if key_event.code == KeyCode::Char('w') => {
self.cursor.to_next_word(self.contents.len() - 1);
}
Event::Key(key_event) if key_event.code == KeyCode::Char('e') => {
self.cursor.to_next_end(self.contents.len() - 1);
}
Event::Key(key_event) if key_event.code == KeyCode::Char('b') => {
self.cursor.to_previous_beginning();
}
Event::Key(key_event) if key_event.code == KeyCode::Char(';') => {
self.cursor.collapse();
}
_ => {}
}
}