From da33dcce1d7428fe1230f3155351e1f1e57b9a44 Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 14 Apr 2023 15:10:11 -0400 Subject: [PATCH] Handle and render submodes --- src/main.rs | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 66cde13..d1a65ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,6 +111,7 @@ pub struct State { pub cursor: Cursor, pub file: Option, pub mode: Mode, + pub submode: Option, pub last_saved: Rope, pub scroll: Cursor, pub size: (usize, usize), @@ -133,6 +134,7 @@ impl State { cursor: Cursor::default(), file: file_name, mode: Mode::default(), + submode: None, last_saved, scroll: Cursor::default(), size: (cols as usize, rows as usize), @@ -179,6 +181,12 @@ impl State { .buffer .draw(cols, buffer_rows, self.scroll, self.cursor, out)?; + // draw submode + if let Some(submode) = self.submode { + out.queue(cursor::MoveTo(cols - 10, rows - 1))?; + write!(out, "{:?}", submode)?; + } + // draw cursor let cursor_pos = set_cursor_pos.unwrap_or_else(|| { // calculate cursor position on buffer @@ -222,9 +230,7 @@ impl State { match event { Event::Key(KeyEvent { code, .. }) => { - if let Some(keybind) = self.config.keybinds.visual.map.get(&code) { - self.execute_keybind(keybind.clone()); - } + self.on_key(code); } event => self.on_any_event(event), } @@ -305,13 +311,28 @@ impl State { Mode::Command(_) => return false, // command mode is handled in [on_command_event] }; - match keybinds.map.get(&key) { - None => false, - Some(keybind) => { - self.execute_keybind(keybind.clone()); - return true; - } + if let Some(submode) = self.submode { + // if we're currently in a submode, try to run this submode's keybind + keybinds + .submodes + .get(&submode) + .and_then(|submode| submode.get(&key)) + .cloned() + .map(|keybind| self.execute_keybind(keybind)); + // whether or not there is a keybind we exit the submode + self.submode = None; + } else if keybinds.submodes.contains_key(&key) { + // enter a submode if available + self.submode = Some(key); + } else if let Some(keybind) = keybinds.map.get(&key) { + // run the keybind if available + self.execute_keybind(keybind.clone()); + } else { + // key is not bound; don't handle + return false; } + + true } fn on_any_event(&mut self, event: Event) {