forked from mars/breed
78 lines
2.3 KiB
Rust
78 lines
2.3 KiB
Rust
/*
|
|
* Copyright (c) 2023 Marceline Cramer
|
|
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
|
* 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<String, Action> {
|
|
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 });
|
|
}
|