Remove NormalState + simplify event-handling

This commit is contained in:
mars 2023-04-14 20:59:59 -04:00
parent e47275cb88
commit b2b69bf7b3
2 changed files with 78 additions and 121 deletions

View File

@ -19,7 +19,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::{Cursor, Direction, InsertState, Mode, NormalState, State}; use crate::{Cursor, Direction, InsertState, Mode, State};
pub type Action = fn(&mut State); pub type Action = fn(&mut State);
@ -119,7 +119,7 @@ pub fn normal_mode(state: &mut State) {
_ => {} _ => {}
} }
state.mode = Mode::Normal(NormalState::default()); state.mode = Mode::Normal;
} }
pub fn visual_mode(state: &mut State) { pub fn visual_mode(state: &mut State) {

View File

@ -54,11 +54,6 @@ pub struct Cursor {
pub line: usize, pub line: usize,
} }
#[derive(Clone, Debug, Default)]
pub struct NormalState {
pub error: Option<String>,
}
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct CommandState { pub struct CommandState {
pub buf: String, pub buf: String,
@ -70,25 +65,20 @@ pub struct InsertState {
append: bool, append: bool,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, Default)]
pub enum Mode { pub enum Mode {
Normal(NormalState), #[default]
Normal,
Command(CommandState), Command(CommandState),
Visual, Visual,
Insert(InsertState), Insert(InsertState),
} }
impl Default for Mode {
fn default() -> Self {
Mode::Normal(Default::default())
}
}
impl Mode { impl Mode {
pub fn cursor_style(&self) -> cursor::SetCursorStyle { pub fn cursor_style(&self) -> cursor::SetCursorStyle {
use cursor::SetCursorStyle as Style; use cursor::SetCursorStyle as Style;
match self { match self {
Mode::Normal(_) => Style::SteadyBlock, Mode::Normal => Style::SteadyBlock,
Mode::Visual => Style::BlinkingBlock, Mode::Visual => Style::BlinkingBlock,
Mode::Insert(_) => Style::BlinkingBar, Mode::Insert(_) => Style::BlinkingBar,
Mode::Command(_) => Style::SteadyUnderScore, Mode::Command(_) => Style::SteadyUnderScore,
@ -116,6 +106,7 @@ pub struct State {
pub scroll: Cursor, pub scroll: Cursor,
pub size: (usize, usize), pub size: (usize, usize),
pub quit: bool, pub quit: bool,
pub error: Option<String>,
pub theme_tx: Sender<PathBuf>, pub theme_tx: Sender<PathBuf>,
} }
@ -139,6 +130,7 @@ impl State {
scroll: Cursor::default(), scroll: Cursor::default(),
size: (cols as usize, rows as usize), size: (cols as usize, rows as usize),
quit: false, quit: false,
error: None,
theme_tx, theme_tx,
}) })
} }
@ -154,22 +146,18 @@ impl State {
let mut set_cursor_pos = None; let mut set_cursor_pos = None;
let mut show_status_bar = false; let mut show_status_bar = false;
match &self.mode { if let Mode::Command(CommandState { buf, cursor }) = &self.mode {
Mode::Command(CommandState { buf, cursor }) => { let col = *cursor as u16 + 1;
let col = *cursor as u16 + 1; let row = rows - 1;
let row = rows - 1; out.queue(cursor::MoveTo(0, row))?;
out.queue(cursor::MoveTo(0, row))?; write!(out, ":{}", buf)?;
write!(out, ":{}", buf)?; set_cursor_pos = Some((col, row));
set_cursor_pos = Some((col, row)); show_status_bar = true;
show_status_bar = true; } else if let Some(error) = self.error.as_ref() {
} let error_style = styles.get_scope("error");
Mode::Normal(NormalState { error: Some(error) }) => { out.queue(cursor::MoveTo(0, rows - 1))?;
let error_style = styles.get_scope("error"); error_style.print_styled(out, error)?;
out.queue(cursor::MoveTo(0, rows - 1))?; show_status_bar = true;
error_style.print_styled(out, error)?;
show_status_bar = true;
}
_ => {}
} }
// done with styles // done with styles
@ -207,105 +195,82 @@ impl State {
Ok(()) Ok(())
} }
/// Sets the state to `Mode::Normal` with an error message. /// Sets the current error message.
pub fn set_error(&mut self, error: impl ToString) { pub fn set_error(&mut self, error: impl ToString) {
self.mode = Mode::Normal(NormalState { self.error = Some(error.to_string());
error: Some(error.to_string()),
});
} }
pub fn on_event(&mut self, event: Event) { pub fn on_event(&mut self, event: Event) {
match &self.mode {
Mode::Normal(state) => self.on_normal_event(event, state.clone()),
Mode::Command(state) => self.on_command_event(event, state.clone()),
Mode::Visual => self.on_visual_event(event),
Mode::Insert(state) => self.on_insert_event(event, state.clone()),
}
}
fn on_normal_event(&mut self, event: Event, mut state: NormalState) {
// reset the error from the last event // reset the error from the last event
state.error = None; self.error = None;
self.mode = Mode::Normal(state);
match event { match event {
Event::Key(KeyEvent { code, .. }) => { Event::Resize(cols, rows) => {
self.on_key(code); self.size = (cols as usize, rows as usize);
} }
event => self.on_any_event(event), Event::Key(KeyEvent { code, .. }) => match &self.mode {
} Mode::Normal | Mode::Visual => {
} self.on_key(code);
fn on_command_event(&mut self, event: Event, mut state: CommandState) {
match event {
Event::Key(KeyEvent { code, .. }) => match code {
KeyCode::Char(c) => {
state.buf.insert(state.cursor, c);
state.cursor += 1;
} }
KeyCode::Backspace => { Mode::Command(state) => self.on_command_key(code, state.clone()),
if state.cursor > 0 { Mode::Insert(_) => {
state.cursor -= 1; if !self.on_key(code) {
state.buf.remove(state.cursor); if let KeyCode::Char(c) = code {
self.buffer.insert_char(self.cursor, c);
self.move_cursor(Direction::Right);
}
} }
} }
KeyCode::Delete if state.cursor < state.buf.len() => { },
_ => {}
}
}
fn on_command_key(&mut self, code: KeyCode, mut state: CommandState) {
match code {
KeyCode::Char(c) => {
state.buf.insert(state.cursor, c);
state.cursor += 1;
}
KeyCode::Backspace => {
if state.cursor > 0 {
state.cursor -= 1;
state.buf.remove(state.cursor); state.buf.remove(state.cursor);
} }
KeyCode::Left if state.cursor > 0 => { }
state.cursor -= 1; KeyCode::Delete if state.cursor < state.buf.len() => {
} state.buf.remove(state.cursor);
KeyCode::Right if state.cursor < state.buf.len() => { }
state.cursor += 1; KeyCode::Left if state.cursor > 0 => {
} state.cursor -= 1;
KeyCode::Enter => { }
// TODO add to command history KeyCode::Right if state.cursor < state.buf.len() => {
let result = self.execute_command(&state.buf); state.cursor += 1;
let error = result.err(); }
self.mode = Mode::Normal(NormalState { error }); KeyCode::Enter => {
return; // TODO add to command history
} let _ = self
KeyCode::Esc => { .execute_command(&state.buf)
self.mode = Mode::default(); .map_err(|err| self.set_error(err));
return; self.mode = Mode::Normal;
} return;
_ => {} }
}, KeyCode::Esc => {
event => return self.on_any_event(event), self.mode = Mode::default();
return;
}
_ => {}
} }
self.mode = Mode::Command(state); self.mode = Mode::Command(state);
} }
fn on_visual_event(&mut self, event: Event) {
match event {
Event::Key(KeyEvent { code, .. }) => {
self.on_key(code);
}
event => self.on_any_event(event),
}
}
fn on_insert_event(&mut self, event: Event, _state: InsertState) {
match event {
Event::Key(KeyEvent { code, .. }) => {
if !self.on_key(code) {
if let KeyCode::Char(c) = code {
self.buffer.insert_char(self.cursor, c);
self.move_cursor(Direction::Right);
}
}
}
event => self.on_any_event(event),
}
}
/// Processes a key press event. /// Processes a key press event.
/// ///
/// Returns `true` if the key was handled by a keybind, `false` otherwise. /// Returns `true` if the key was handled by a keybind, `false` otherwise.
fn on_key(&mut self, key: Key) -> bool { fn on_key(&mut self, key: Key) -> bool {
let keybinds = match &self.mode { let keybinds = match &self.mode {
Mode::Normal(_) => &self.config.keybinds.normal, Mode::Normal => &self.config.keybinds.normal,
Mode::Insert(_) => &self.config.keybinds.insert, Mode::Insert(_) => &self.config.keybinds.insert,
Mode::Visual => &self.config.keybinds.visual, Mode::Visual => &self.config.keybinds.visual,
Mode::Command(_) => return false, // command mode is handled in [on_command_event] Mode::Command(_) => return false, // command mode is handled in [on_command_event]
@ -335,15 +300,6 @@ impl State {
true true
} }
fn on_any_event(&mut self, event: Event) {
match event {
Event::Resize(cols, rows) => {
self.size = (cols as usize, rows as usize);
}
_ => {}
}
}
fn write_buffer(&mut self, file: OsString) -> Result<()> { fn write_buffer(&mut self, file: OsString) -> Result<()> {
self.last_saved = self.buffer.as_ref().clone(); self.last_saved = self.buffer.as_ref().clone();
let out = self.buffer.as_ref().bytes().collect::<Vec<u8>>(); let out = self.buffer.as_ref().bytes().collect::<Vec<u8>>();
@ -365,7 +321,7 @@ impl State {
let file = OsString::from(part); let file = OsString::from(part);
self.file = Some(file.clone()); self.file = Some(file.clone());
file file
}, }
None => { None => {
return Err(format!("{}: No file name.", command)); return Err(format!("{}: No file name.", command));
} }
@ -379,9 +335,10 @@ impl State {
match keybind { match keybind {
Keybind::Action(action) => action(self), Keybind::Action(action) => action(self),
Keybind::Command(command) => { Keybind::Command(command) => {
let result = self.execute_command(&command); let _ = self
let error = result.err(); .execute_command(&command)
self.mode = Mode::Normal(NormalState { error }); .map_err(|err| self.set_error(err));
self.mode = Mode::Normal;
return; return;
} }
} }