diff --git a/src/action.rs b/src/action.rs index b4f0c81..8c23f83 100644 --- a/src/action.rs +++ b/src/action.rs @@ -57,12 +57,18 @@ pub enum Action { Redo, Save, + + PreviousBuffer, + NextBuffer, } // actions that act on the app as a whole, not just one buffer pub enum AppAction { QuitIfSaved, Quit, + + PreviousBuffer, + NextBuffer, } impl Buffer { @@ -122,6 +128,9 @@ impl Buffer { Action::Redo => self.redo(), Action::Save => self.save(), + + Action::PreviousBuffer => return Some(AppAction::PreviousBuffer), + Action::NextBuffer => return Some(AppAction::NextBuffer), } None diff --git a/src/app/mod.rs b/src/app/mod.rs index 0c23eac..c4449c8 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,5 +1,5 @@ use std::{env, process::exit}; -use crossterm::{event::{self, Event, KeyEvent}, terminal::window_size}; +use crossterm::{event::{self, Event, KeyCode, KeyEvent, KeyModifiers}, terminal::window_size}; use ratatui::{style::Stylize, text::Span}; use crate::{BYTES_PER_LINE, action::AppAction, buffer::Buffer, config::Config}; @@ -71,6 +71,14 @@ impl App { } fn handle_key(&mut self, key_event: KeyEvent) { + if key_event.modifiers == KeyModifiers::CONTROL && + key_event.code == KeyCode::Char('c') + { + crossterm::terminal::disable_raw_mode().unwrap(); + ratatui::restore(); + exit(130); + } + let maybe_app_action = self.buffers[self.current_buffer_index].handle_key( key_event, &self.config, @@ -81,6 +89,9 @@ impl App { match app_action { AppAction::QuitIfSaved => self.quit_if_saved(), AppAction::Quit => self.quit(), + + AppAction::PreviousBuffer => self.previous_buffer(), + AppAction::NextBuffer => self.next_buffer(), } } } @@ -99,6 +110,22 @@ impl App { self.should_quit = true; } + const fn previous_buffer(&mut self) { + if self.current_buffer_index == 0 { + self.current_buffer_index = self.buffers.len() - 1; + } else { + self.current_buffer_index -= 1; + } + } + + const fn next_buffer(&mut self) { + if self.current_buffer_index == self.buffers.len() - 1 { + self.current_buffer_index = 0; + } else { + self.current_buffer_index += 1; + } + } + pub fn current_buffer(&self) -> &Buffer { &self.buffers[self.current_buffer_index] } diff --git a/src/config.rs b/src/config.rs index 45b06c0..75db82f 100644 --- a/src/config.rs +++ b/src/config.rs @@ -138,6 +138,9 @@ impl Default for Config { ("u".try_into().unwrap(), Action::Undo), ("U".try_into().unwrap(), Action::Redo), + + ("C-j".try_into().unwrap(), Action::PreviousBuffer), + ("C-l".try_into().unwrap(), Action::NextBuffer), ].into()), (Some(PartialAction::Goto), [ ("j".try_into().unwrap(), Action::GotoLineStart), diff --git a/src/main.rs b/src/main.rs index 9df8616..7b532c4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,6 +57,7 @@ const CHUNKS_PER_LINE: usize = BYTES_PER_LINE / BYTES_PER_CHUNK; fn main() { let mut app = App::new(); let mut terminal = ratatui::init(); + crossterm::terminal::enable_raw_mode().unwrap(); while !app.should_quit { terminal.draw(|frame| { @@ -66,6 +67,7 @@ fn main() { app.handle_events(); } + crossterm::terminal::disable_raw_mode().unwrap(); ratatui::restore(); // dbg!(app.edit_history);