Handle and render submodes

This commit is contained in:
mars 2023-04-14 15:10:11 -04:00
parent 1496a39a1a
commit da33dcce1d
1 changed files with 30 additions and 9 deletions

View File

@ -111,6 +111,7 @@ pub struct State {
pub cursor: Cursor,
pub file: Option<OsString>,
pub mode: Mode,
pub submode: Option<Key>,
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) {