add cursor, hjklweb;
This commit is contained in:
+65
-8
@@ -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();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user