Handle and render submodes
This commit is contained in:
parent
1496a39a1a
commit
da33dcce1d
39
src/main.rs
39
src/main.rs
|
@ -111,6 +111,7 @@ pub struct State {
|
||||||
pub cursor: Cursor,
|
pub cursor: Cursor,
|
||||||
pub file: Option<OsString>,
|
pub file: Option<OsString>,
|
||||||
pub mode: Mode,
|
pub mode: Mode,
|
||||||
|
pub submode: Option<Key>,
|
||||||
pub last_saved: Rope,
|
pub last_saved: Rope,
|
||||||
pub scroll: Cursor,
|
pub scroll: Cursor,
|
||||||
pub size: (usize, usize),
|
pub size: (usize, usize),
|
||||||
|
@ -133,6 +134,7 @@ impl State {
|
||||||
cursor: Cursor::default(),
|
cursor: Cursor::default(),
|
||||||
file: file_name,
|
file: file_name,
|
||||||
mode: Mode::default(),
|
mode: Mode::default(),
|
||||||
|
submode: None,
|
||||||
last_saved,
|
last_saved,
|
||||||
scroll: Cursor::default(),
|
scroll: Cursor::default(),
|
||||||
size: (cols as usize, rows as usize),
|
size: (cols as usize, rows as usize),
|
||||||
|
@ -179,6 +181,12 @@ impl State {
|
||||||
.buffer
|
.buffer
|
||||||
.draw(cols, buffer_rows, self.scroll, self.cursor, out)?;
|
.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
|
// draw cursor
|
||||||
let cursor_pos = set_cursor_pos.unwrap_or_else(|| {
|
let cursor_pos = set_cursor_pos.unwrap_or_else(|| {
|
||||||
// calculate cursor position on buffer
|
// calculate cursor position on buffer
|
||||||
|
@ -222,9 +230,7 @@ impl State {
|
||||||
|
|
||||||
match event {
|
match event {
|
||||||
Event::Key(KeyEvent { code, .. }) => {
|
Event::Key(KeyEvent { code, .. }) => {
|
||||||
if let Some(keybind) = self.config.keybinds.visual.map.get(&code) {
|
self.on_key(code);
|
||||||
self.execute_keybind(keybind.clone());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
event => self.on_any_event(event),
|
event => self.on_any_event(event),
|
||||||
}
|
}
|
||||||
|
@ -305,13 +311,28 @@ impl State {
|
||||||
Mode::Command(_) => return false, // command mode is handled in [on_command_event]
|
Mode::Command(_) => return false, // command mode is handled in [on_command_event]
|
||||||
};
|
};
|
||||||
|
|
||||||
match keybinds.map.get(&key) {
|
if let Some(submode) = self.submode {
|
||||||
None => false,
|
// if we're currently in a submode, try to run this submode's keybind
|
||||||
Some(keybind) => {
|
keybinds
|
||||||
self.execute_keybind(keybind.clone());
|
.submodes
|
||||||
return true;
|
.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) {
|
fn on_any_event(&mut self, event: Event) {
|
||||||
|
|
Loading…
Reference in New Issue