diff --git a/src/actions.rs b/src/actions.rs new file mode 100644 index 0000000..1e7e98f --- /dev/null +++ b/src/actions.rs @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2023 Marceline Cramer + * Copyright (c) 2023 Emma Tebibyte + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + +use std::collections::HashMap; + +use crate::{Direction, InsertState, Mode, State}; + +pub type Action = fn(&mut State); + +pub fn load_actions() -> HashMap { + let list: &'static [(&'static str, fn(&mut State))] = &[ + ("move_char_left", move_char_left), + ("move_line_down", move_line_down), + ("move_line_up", move_line_up), + ("move_char_right", move_char_right), + ]; + + let mut actions = HashMap::with_capacity(list.len()); + for (name, action) in list.iter() { + actions.insert(name.to_string(), *action); + } + + actions +} + +pub fn move_char_left(state: &mut State) { + state.move_cursor(Direction::Left); +} + +pub fn move_char_right(state: &mut State) { + state.move_cursor(Direction::Right); +} + +pub fn move_line_down(state: &mut State) { + state.move_cursor(Direction::Down); +} + +pub fn move_line_up(state: &mut State) { + state.move_cursor(Direction::Up); +} + +pub fn insert_mode(state: &mut State) { + state.mode = Mode::Insert(InsertState { append: false }); +} + +pub fn append_mode(state: &mut State) { + state.mode = Mode::Insert(InsertState { append: true }); +} + +pub fn open_below(state: &mut State) { + state.cursor.line += 1; + state.cursor.column = 0; + state.buffer.insert_char(state.cursor, '\n'); + state.mode = Mode::Insert(InsertState { append: false }); +} + +pub fn open_above(state: &mut State) { + state.cursor.column = 0; + state.buffer.insert_char(state.cursor, '\n'); + state.mode = Mode::Insert(InsertState { append: false }); +} diff --git a/src/main.rs b/src/main.rs index b92ec14..e7450c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,7 @@ use crossterm::{ use parking_lot::Mutex; use yacexits::{exit, EX_DATAERR, EX_UNAVAILABLE}; +mod actions; mod buffer; mod theme; @@ -95,7 +96,7 @@ pub enum Direction { Right, } -struct State { +pub struct State { pub styles: Arc>, pub buffer: Buffer, pub cursor: Cursor, @@ -190,34 +191,16 @@ impl State { match event { Event::Key(KeyEvent { code, .. }) => match code { - KeyCode::Char('i') => { - let state = InsertState { append: false }; - self.mode = Mode::Insert(state); - } - KeyCode::Char('a') => { - let state = InsertState { append: true }; - self.move_cursor(Direction::Right); - self.mode = Mode::Insert(state); - } - KeyCode::Char('o') => { - let state = InsertState { append: false }; - self.cursor.line += 1; - self.cursor.column = 0; - self.buffer.insert_char(self.cursor, '\n'); - self.mode = Mode::Insert(state); - } - KeyCode::Char('O') => { - let state = InsertState { append: false }; - self.cursor.column = 0; - self.buffer.insert_char(self.cursor, '\n'); - self.mode = Mode::Insert(state); - } KeyCode::Char(':') => { self.mode = Mode::Command(Default::default()); } KeyCode::Char('v') => { self.mode = Mode::Visual; } + KeyCode::Char('i') => actions::insert_mode(self), + KeyCode::Char('a') => actions::append_mode(self), + KeyCode::Char('o') => actions::open_below(self), + KeyCode::Char('O') => actions::open_above(self), code => self.on_any_key(code), }, event => self.on_any_event(event), @@ -323,10 +306,10 @@ impl State { fn on_any_key(&mut self, code: KeyCode) { match code { KeyCode::Esc => self.mode = Mode::default(), - KeyCode::Char('h') | KeyCode::Left => self.move_cursor(Direction::Left), - KeyCode::Char('j') | KeyCode::Down => self.move_cursor(Direction::Down), - KeyCode::Char('k') | KeyCode::Up => self.move_cursor(Direction::Up), - KeyCode::Char('l') | KeyCode::Right => self.move_cursor(Direction::Right), + KeyCode::Char('h') | KeyCode::Left => actions::move_char_left(self), + KeyCode::Char('j') | KeyCode::Down => actions::move_line_down(self), + KeyCode::Char('k') | KeyCode::Up => actions::move_line_up(self), + KeyCode::Char('l') | KeyCode::Right => actions::move_char_right(self), _ => {} } }