next/previous buffer

This commit is contained in:
alice pellerin
2026-03-19 04:02:06 -05:00
parent 4f61f2fb93
commit 5908895ef9
4 changed files with 42 additions and 1 deletions
+9
View File
@@ -57,12 +57,18 @@ pub enum Action {
Redo, Redo,
Save, Save,
PreviousBuffer,
NextBuffer,
} }
// actions that act on the app as a whole, not just one buffer // actions that act on the app as a whole, not just one buffer
pub enum AppAction { pub enum AppAction {
QuitIfSaved, QuitIfSaved,
Quit, Quit,
PreviousBuffer,
NextBuffer,
} }
impl Buffer { impl Buffer {
@@ -122,6 +128,9 @@ impl Buffer {
Action::Redo => self.redo(), Action::Redo => self.redo(),
Action::Save => self.save(), Action::Save => self.save(),
Action::PreviousBuffer => return Some(AppAction::PreviousBuffer),
Action::NextBuffer => return Some(AppAction::NextBuffer),
} }
None None
+28 -1
View File
@@ -1,5 +1,5 @@
use std::{env, process::exit}; 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 ratatui::{style::Stylize, text::Span};
use crate::{BYTES_PER_LINE, action::AppAction, buffer::Buffer, config::Config}; 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) { 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( let maybe_app_action = self.buffers[self.current_buffer_index].handle_key(
key_event, key_event,
&self.config, &self.config,
@@ -81,6 +89,9 @@ impl App {
match app_action { match app_action {
AppAction::QuitIfSaved => self.quit_if_saved(), AppAction::QuitIfSaved => self.quit_if_saved(),
AppAction::Quit => self.quit(), AppAction::Quit => self.quit(),
AppAction::PreviousBuffer => self.previous_buffer(),
AppAction::NextBuffer => self.next_buffer(),
} }
} }
} }
@@ -99,6 +110,22 @@ impl App {
self.should_quit = true; 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 { pub fn current_buffer(&self) -> &Buffer {
&self.buffers[self.current_buffer_index] &self.buffers[self.current_buffer_index]
} }
+3
View File
@@ -138,6 +138,9 @@ impl Default for Config {
("u".try_into().unwrap(), Action::Undo), ("u".try_into().unwrap(), Action::Undo),
("U".try_into().unwrap(), Action::Redo), ("U".try_into().unwrap(), Action::Redo),
("C-j".try_into().unwrap(), Action::PreviousBuffer),
("C-l".try_into().unwrap(), Action::NextBuffer),
].into()), ].into()),
(Some(PartialAction::Goto), [ (Some(PartialAction::Goto), [
("j".try_into().unwrap(), Action::GotoLineStart), ("j".try_into().unwrap(), Action::GotoLineStart),
+2
View File
@@ -57,6 +57,7 @@ const CHUNKS_PER_LINE: usize = BYTES_PER_LINE / BYTES_PER_CHUNK;
fn main() { fn main() {
let mut app = App::new(); let mut app = App::new();
let mut terminal = ratatui::init(); let mut terminal = ratatui::init();
crossterm::terminal::enable_raw_mode().unwrap();
while !app.should_quit { while !app.should_quit {
terminal.draw(|frame| { terminal.draw(|frame| {
@@ -66,6 +67,7 @@ fn main() {
app.handle_events(); app.handle_events();
} }
crossterm::terminal::disable_raw_mode().unwrap();
ratatui::restore(); ratatui::restore();
// dbg!(app.edit_history); // dbg!(app.edit_history);