aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/input
diff options
context:
space:
mode:
authorcategorille <69397914+categorille@users.noreply.github.com>2021-02-12 14:55:22 +0100
committerGitHub <noreply@github.com>2021-02-12 14:55:22 +0100
commitc74e2ef27356257c45c6a7bdb24c51a7ae390d8f (patch)
tree011f421bef92370ba7b5e3a7be9b97b06f8e9a82 /src/common/input
parentfeat(strider): add vim h key (#182) (diff)
downloadsl0ppij-c74e2ef27356257c45c6a7bdb24c51a7ae390d8f.tar.gz
sl0ppij-c74e2ef27356257c45c6a7bdb24c51a7ae390d8f.tar.xz
sl0ppij-c74e2ef27356257c45c6a7bdb24c51a7ae390d8f.zip
feat(input): new keybindings and persistent mode as default (#181)
* added some comments in the input module * InputHandler now has InputState instead of separate InputMode and permanent bool * keybinds are now associated with a Vec<Action> instead of a single Action * removing "persistent" modes alltogether to reimplement the feature, help bar broken * locked command mode by default, fixes #161, help bar still broken * status bar fixed, still a few improvements/bugs to go * better shortcut help bar contents * fixed last bits and i think we are good * modified tests according to new controls, not working * Revert "modified tests according to new controls, not working" This reverts commit f2d9421ff0169feb83dbd9b246e59b9244cafc16. * basic.rs tests now pass, not the rest * close_pane.rs tests pass, but very slowly? * compatibility.rs tests pass, very slowly as well * {layout, {move_focus_*}}.rs all working mildly slowly * {resize_*}.rs should all work but very slowly and funky, see PR comments * {tabs,toggle_fullscreen}.rs pass. Test performance issue yet to be fixed * tests now work, with a hack :| ready for merge * rustfmt + deleted references to InputState and mode persistency
Diffstat (limited to 'src/common/input')
-rw-r--r--src/common/input/actions.rs38
-rw-r--r--src/common/input/handler.rs144
-rw-r--r--src/common/input/keybinds.rs191
3 files changed, 191 insertions, 182 deletions
diff --git a/src/common/input/actions.rs b/src/common/input/actions.rs
index 0e3243a3..ae212e32 100644
--- a/src/common/input/actions.rs
+++ b/src/common/input/actions.rs
@@ -1,5 +1,6 @@
-/// This module is for defining the set of actions that can be taken in response to a keybind
-/// and also passing actions back to the handler for dispatch.
+/// This module is for defining the set of actions that can be taken in
+/// response to a keybind and also passing actions back to the handler
+/// for dispatch.
use super::handler;
#[derive(Clone)]
@@ -12,36 +13,35 @@ pub enum Direction {
#[derive(Clone)]
pub enum Action {
- /// Quit Zellij
+ /// Quit Zellij.
Quit,
- /// Write to terminal
+ /// Write to the terminal.
Write(Vec<u8>),
- /// Switch to the specified input mode
+ /// Switch to the specified input mode.
SwitchToMode(handler::InputMode),
- TogglePersistentMode,
- /// Resize focus pane in specified direction
+ /// Resize focus pane in specified direction.
Resize(Direction),
- /// Switch focus to next pane in specified direction
+ /// Switch focus to next pane in specified direction.
SwitchFocus(Direction),
- /// Move the focus pane in specified direction
+ /// Move the focus pane in specified direction.
MoveFocus(Direction),
- /// Scroll up in focus pane
+ /// Scroll up in focus pane.
ScrollUp,
- /// Scroll down in focus pane
+ /// Scroll down in focus pane.
ScrollDown,
- /// Toggle focus pane between fullscreen and normal layout
+ /// Toggle between fullscreen focus pane and normal layout.
ToggleFocusFullscreen,
- /// Open a new pane in specified direction (relative to focus)
- /// If no direction is specified, will try to use the biggest available space
+ /// Open a new pane in the specified direction (relative to focus).
+ /// If no direction is specified, will try to use the biggest available space.
NewPane(Option<Direction>),
- /// Close focus pane
+ /// Close the focus pane.
CloseFocus,
- // Create a new tab
+ /// Create a new tab.
NewTab,
- // Go to next tab
+ /// Go to the next tab.
GoToNextTab,
- // Go to previous tab
+ /// Go to the previous tab.
GoToPreviousTab,
- // Close the current tab
+ /// Close the current tab.
CloseTab,
}
diff --git a/src/common/input/handler.rs b/src/common/input/handler.rs
index 136dacba..74235ff7 100644
--- a/src/common/input/handler.rs
+++ b/src/common/input/handler.rs
@@ -13,11 +13,12 @@ use serde::{Deserialize, Serialize};
use strum_macros::EnumIter;
use termion::input::TermReadEventsAndRaw;
-use super::keybinds::key_to_action;
+use super::keybinds::key_to_actions;
+/// Handles the dispatching of [`Action`]s according to the current
+/// [`InputMode`], as well as changes to that mode.
struct InputHandler {
mode: InputMode,
- mode_is_persistent: bool,
os_input: Box<dyn OsApi>,
command_is_executing: CommandIsExecuting,
send_screen_instructions: SenderWithContext<ScreenInstruction>,
@@ -37,7 +38,6 @@ impl InputHandler {
) -> Self {
InputHandler {
mode: InputMode::Normal,
- mode_is_persistent: false,
os_input,
command_is_executing,
send_screen_instructions,
@@ -47,7 +47,9 @@ impl InputHandler {
}
}
- /// Main event loop
+ /// Main event loop. Interprets the terminal [`Event`](termion::event::Event)s
+ /// as [`Action`]s according to the current [`InputMode`], and dispatches those
+ /// actions.
fn get_input(&mut self) {
let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
err_ctx.add_call(ContextType::StdinHandler);
@@ -56,7 +58,6 @@ impl InputHandler {
self.send_screen_instructions.update(err_ctx);
if let Ok(keybinds) = get_default_keybinds() {
'input_loop: loop {
- let entry_mode = self.mode;
//@@@ I think this should actually just iterate over stdin directly
let stdin_buffer = self.os_input.read_from_stdin();
drop(
@@ -67,20 +68,19 @@ impl InputHandler {
match key_result {
Ok((event, raw_bytes)) => match event {
termion::event::Event::Key(key) => {
- let should_break = self.dispatch_action(key_to_action(
- &key, raw_bytes, &self.mode, &keybinds,
- ));
- //@@@ This is a hack until we dispatch more than one action per key stroke
- if entry_mode == self.mode && !self.mode_is_persistent {
- self.mode = InputMode::Normal;
- update_state(&self.send_app_instructions, |_| AppState {
- input_state: InputState {
- mode: self.mode,
- persistent: self.mode_is_persistent,
- },
- });
- }
- if should_break {
+ // FIXME this explicit break is needed because the current test
+ // framework relies on it to not create dead threads that loop
+ // and eat up CPUs. Do not remove until the test framework has
+ // been revised. Sorry about this (@categorille)
+ if {
+ let mut should_break = false;
+ for action in
+ key_to_actions(&key, raw_bytes, &self.mode, &keybinds)
+ {
+ should_break |= self.dispatch_action(action);
+ }
+ should_break
+ } {
break 'input_loop;
}
}
@@ -100,7 +100,7 @@ impl InputHandler {
}
fn dispatch_action(&mut self, action: Action) -> bool {
- let mut interrupt_loop = false;
+ let mut should_break = false;
match action {
Action::Write(val) => {
@@ -113,32 +113,17 @@ impl InputHandler {
}
Action::Quit => {
self.exit();
- interrupt_loop = true;
+ should_break = true;
}
Action::SwitchToMode(mode) => {
self.mode = mode;
- if mode == InputMode::Normal {
- self.mode_is_persistent = false;
- }
update_state(&self.send_app_instructions, |_| AppState {
- input_state: InputState {
- mode: self.mode,
- persistent: self.mode_is_persistent,
- },
+ input_mode: self.mode,
});
self.send_screen_instructions
.send(ScreenInstruction::Render)
.unwrap();
}
- Action::TogglePersistentMode => {
- self.mode_is_persistent = !self.mode_is_persistent;
- update_state(&self.send_app_instructions, |_| AppState {
- input_state: InputState {
- mode: self.mode,
- persistent: self.mode_is_persistent,
- },
- });
- }
Action::Resize(direction) => {
let screen_instr = match direction {
super::actions::Direction::Left => ScreenInstruction::ResizeLeft,
@@ -231,7 +216,7 @@ impl InputHandler {
}
}
- interrupt_loop
+ should_break
}
/// Routine to be called when the input handler exits (at the moment this is the
@@ -243,28 +228,14 @@ impl InputHandler {
}
}
-#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, Serialize, Deserialize)]
-pub struct InputState {
- mode: InputMode,
- persistent: bool,
-}
-
-impl Default for InputState {
- fn default() -> InputState {
- InputState {
- mode: InputMode::Normal,
- persistent: false,
- }
- }
-}
-
-/// Dictates whether we're in command mode, persistent command mode, normal mode or exiting:
-/// - Normal mode either writes characters to the terminal, or switches to command mode
+/// Dictates the input mode, which is the way that keystrokes will be interpreted:
+/// - Normal mode either writes characters to the terminal, or switches to Command mode
/// using a particular key control
-/// - Command mode intercepts characters to control zellij itself, before switching immediately
-/// back to normal mode
-/// - Persistent command mode is the same as command mode, but doesn't return automatically to
-/// normal mode
+/// - Command mode is a menu that allows choosing another mode, like Resize or Pane
+/// - Resize mode is for resizing the different panes already present
+/// - Pane mode is for creating and closing panes in different directions
+/// - Tab mode is for creating tabs and moving between them
+/// - Scroll mode is for scrolling up and down within a pane
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone, EnumIter, Serialize, Deserialize)]
pub enum InputMode {
Normal,
@@ -276,10 +247,12 @@ pub enum InputMode {
Exiting,
}
+/// Represents the help message that is printed in the status bar, indicating
+/// the current [`InputMode`], whether that mode is persistent, and what the
+/// keybinds for that mode are.
#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Help {
pub mode: InputMode,
- pub mode_is_persistent: bool,
pub keybinds: Vec<(String, String)>, // <shortcut> => <shortcut description>
}
@@ -289,46 +262,45 @@ impl Default for InputMode {
}
}
-pub fn get_help(input_state: &InputState) -> Help {
+/// Creates a [`Help`] struct holding the current [`InputMode`] and its keybinds.
+// TODO this should probably be automatically generated in some way
+pub fn get_help(mode: InputMode) -> Help {
let mut keybinds: Vec<(String, String)> = vec![];
- match input_state.mode {
+ match mode {
InputMode::Normal | InputMode::Command | InputMode::Exiting => {
- keybinds.push((format!("p"), format!("Pane mode")));
- keybinds.push((format!("t"), format!("Tab mode")));
- keybinds.push((format!("r"), format!("Resize mode")));
+ keybinds.push((format!("p"), format!("PANE")));
+ keybinds.push((format!("t"), format!("TAB")));
+ keybinds.push((format!("r"), format!("RESIZE")));
+ keybinds.push((format!("s"), format!("SCROLL")));
}
InputMode::Resize => {
- keybinds.push((format!("←↓↑→"), format!("resize pane")));
+ keybinds.push((format!("←↓↑→"), format!("Resize")));
}
InputMode::Pane => {
- keybinds.push((format!("←↓↑→"), format!("move focus")));
- keybinds.push((format!("p"), format!("next pane")));
- keybinds.push((format!("n"), format!("new pane")));
- keybinds.push((format!("d"), format!("down split")));
- keybinds.push((format!("r"), format!("right split")));
- keybinds.push((format!("x"), format!("exit pane")));
- keybinds.push((format!("f"), format!("fullscreen pane")));
+ keybinds.push((format!("←↓↑→"), format!("Move focus")));
+ keybinds.push((format!("p"), format!("Next")));
+ keybinds.push((format!("n"), format!("New")));
+ keybinds.push((format!("d"), format!("Split down")));
+ keybinds.push((format!("r"), format!("Split right")));
+ keybinds.push((format!("x"), format!("Close")));
+ keybinds.push((format!("f"), format!("Fullscreen")));
}
InputMode::Tab => {
- keybinds.push((format!("←↓↑→"), format!("move tab focus")));
- keybinds.push((format!("n"), format!("new tab")));
- keybinds.push((format!("x"), format!("exit tab")));
+ keybinds.push((format!("←↓↑→"), format!("Move focus")));
+ keybinds.push((format!("n"), format!("New")));
+ keybinds.push((format!("x"), format!("Close")));
}
InputMode::Scroll => {
- keybinds.push((format!("↓↑"), format!("scroll up/down")));
+ keybinds.push((format!("↓↑"), format!("Scroll")));
}
}
- keybinds.push((format!("ESC"), format!("Back")));
- keybinds.push((format!("q"), format!("Quit")));
- Help {
- mode: input_state.mode,
- mode_is_persistent: input_state.persistent,
- keybinds,
- }
+ keybinds.push((format!("ESC"), format!("BACK")));
+ keybinds.push((format!("q"), format!("QUIT")));
+ Help { mode, keybinds }
}
-/// Entry point to the module that instantiates a new InputHandler and calls its
-/// reading loop
+/// Entry point to the module. Instantiates a new InputHandler and calls its
+/// input loop.
pub fn input_loop(
os_input: Box<dyn OsApi>,
command_is_executing: CommandIsExecuting,
diff --git a/src/common/input/keybinds.rs b/src/common/input/keybinds.rs
index d6ace68d..2e65ab35 100644
--- a/src/common/input/keybinds.rs
+++ b/src/common/input/keybinds.rs
@@ -1,4 +1,4 @@
-// This module is for mapping particular input keys to their corresponding actions.
+//! Mapping of inputs to sequences of actions
use super::actions::{Action, Direction};
use super::handler::InputMode;
@@ -9,9 +9,9 @@ use strum::IntoEnumIterator;
use termion::event::Key;
type Keybinds = HashMap<InputMode, ModeKeybinds>;
-type ModeKeybinds = HashMap<Key, Action>;
+type ModeKeybinds = HashMap<Key, Vec<Action>>;
-/// Populate the default hashmap of keybinds
+/// Populates the default hashmap of keybinds.
/// @@@khs26 What about an input config file?
pub fn get_default_keybinds() -> Result<Keybinds, String> {
let mut defaults = Keybinds::new();
@@ -23,106 +23,134 @@ pub fn get_default_keybinds() -> Result<Keybinds, String> {
Ok(defaults)
}
+/// Returns the default keybinds for a givent [`InputMode`].
fn get_defaults_for_mode(mode: &InputMode) -> Result<ModeKeybinds, String> {
let mut defaults = ModeKeybinds::new();
match *mode {
InputMode::Normal => {
- defaults.insert(Key::Ctrl('g'), Action::SwitchToMode(InputMode::Command));
+ defaults.insert(
+ Key::Ctrl('g'),
+ vec![Action::SwitchToMode(InputMode::Command)],
+ );
}
InputMode::Command => {
- defaults.insert(Key::Char('r'), Action::SwitchToMode(InputMode::Resize));
- defaults.insert(Key::Char('p'), Action::SwitchToMode(InputMode::Pane));
- defaults.insert(Key::Char('t'), Action::SwitchToMode(InputMode::Tab));
- defaults.insert(Key::Char('s'), Action::SwitchToMode(InputMode::Scroll));
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
- defaults.insert(Key::Char('q'), Action::Quit);
+ defaults.insert(
+ Key::Char('r'),
+ vec![Action::SwitchToMode(InputMode::Resize)],
+ );
+ defaults.insert(Key::Char('p'), vec![Action::SwitchToMode(InputMode::Pane)]);
+ defaults.insert(Key::Char('t'), vec![Action::SwitchToMode(InputMode::Tab)]);
+ defaults.insert(
+ Key::Char('s'),
+ vec![Action::SwitchToMode(InputMode::Scroll)],
+ );
+ defaults.insert(
+ Key::Ctrl('g'),
+ vec![Action::SwitchToMode(InputMode::Normal)],
+ );
+ defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Normal)]);
+ defaults.insert(Key::Char('q'), vec![Action::Quit]);
}
InputMode::Resize => {
- defaults.insert(Key::Char('h'), Action::Resize(Direction::Left));
- defaults.insert(Key::Char('j'), Action::Resize(Direction::Down));
- defaults.insert(Key::Char('k'), Action::Resize(Direction::Up));
- defaults.insert(Key::Char('l'), Action::Resize(Direction::Right));
+ defaults.insert(Key::Char('h'), vec![Action::Resize(Direction::Left)]);
+ defaults.insert(Key::Char('j'), vec![Action::Resize(Direction::Down)]);
+ defaults.insert(Key::Char('k'), vec![Action::Resize(Direction::Up)]);
+ defaults.insert(Key::Char('l'), vec![Action::Resize(Direction::Right)]);
- defaults.insert(Key::Left, Action::Resize(Direction::Left));
- defaults.insert(Key::Down, Action::Resize(Direction::Down));
- defaults.insert(Key::Up, Action::Resize(Direction::Up));
- defaults.insert(Key::Right, Action::Resize(Direction::Right));
+ defaults.insert(Key::Left, vec![Action::Resize(Direction::Left)]);
+ defaults.insert(Key::Down, vec![Action::Resize(Direction::Down)]);
+ defaults.insert(Key::Up, vec![Action::Resize(Direction::Up)]);
+ defaults.insert(Key::Right, vec![Action::Resize(Direction::Right)]);
- defaults.insert(Key::Ctrl('b'), Action::Resize(Direction::Left));
- defaults.insert(Key::Ctrl('n'), Action::Resize(Direction::Down));
- defaults.insert(Key::Ctrl('p'), Action::Resize(Direction::Up));
- defaults.insert(Key::Ctrl('f'), Action::Resize(Direction::Right));
+ defaults.insert(Key::Ctrl('b'), vec![Action::Resize(Direction::Left)]);
+ defaults.insert(Key::Ctrl('n'), vec![Action::Resize(Direction::Down)]);
+ defaults.insert(Key::Ctrl('p'), vec![Action::Resize(Direction::Up)]);
+ defaults.insert(Key::Ctrl('f'), vec![Action::Resize(Direction::Right)]);
- defaults.insert(Key::Char('q'), Action::Quit);
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
+ defaults.insert(Key::Char('q'), vec![Action::Quit]);
+ defaults.insert(
+ Key::Ctrl('g'),
+ vec![Action::SwitchToMode(InputMode::Normal)],
+ );
+ defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]);
}
InputMode::Pane => {
- defaults.insert(Key::Char('h'), Action::MoveFocus(Direction::Left));
- defaults.insert(Key::Char('j'), Action::MoveFocus(Direction::Down));
- defaults.insert(Key::Char('k'), Action::MoveFocus(Direction::Up));
- defaults.insert(Key::Char('l'), Action::MoveFocus(Direction::Right));
+ defaults.insert(Key::Char('h'), vec![Action::MoveFocus(Direction::Left)]);
+ defaults.insert(Key::Char('j'), vec![Action::MoveFocus(Direction::Down)]);
+ defaults.insert(Key::Char('k'), vec![Action::MoveFocus(Direction::Up)]);
+ defaults.insert(Key::Char('l'), vec![Action::MoveFocus(Direction::Right)]);
- defaults.insert(Key::Left, Action::MoveFocus(Direction::Left));
- defaults.insert(Key::Down, Action::MoveFocus(Direction::Down));
- defaults.insert(Key::Up, Action::MoveFocus(Direction::Up));
- defaults.insert(Key::Right, Action::MoveFocus(Direction::Right));
+ defaults.insert(Key::Left, vec![Action::MoveFocus(Direction::Left)]);
+ defaults.insert(Key::Down, vec![Action::MoveFocus(Direction::Down)]);
+ defaults.insert(Key::Up, vec![Action::MoveFocus(Direction::Up)]);
+ defaults.insert(Key::Right, vec![Action::MoveFocus(Direction::Right)]);
- defaults.insert(Key::Ctrl('b'), Action::MoveFocus(Direction::Left));
- defaults.insert(Key::Ctrl('n'), Action::MoveFocus(Direction::Down));
- defaults.insert(Key::Ctrl('p'), Action::MoveFocus(Direction::Up));
- defaults.insert(Key::Ctrl('f'), Action::MoveFocus(Direction::Right));
+ defaults.insert(Key::Ctrl('b'), vec![Action::MoveFocus(Direction::Left)]);
+ defaults.insert(Key::Ctrl('n'), vec![Action::MoveFocus(Direction::Down)]);
+ defaults.insert(Key::Ctrl('p'), vec![Action::MoveFocus(Direction::Up)]);
+ defaults.insert(Key::Ctrl('f'), vec![Action::MoveFocus(Direction::Right)]);
- defaults.insert(Key::Char('p'), Action::SwitchFocus(Direction::Right));
- defaults.insert(Key::Char('n'), Action::NewPane(None));
- defaults.insert(Key::Char('d'), Action::NewPane(Some(Direction::Down)));
- defaults.insert(Key::Char('r'), Action::NewPane(Some(Direction::Right)));
- defaults.insert(Key::Char('x'), Action::CloseFocus);
+ defaults.insert(Key::Char('p'), vec![Action::SwitchFocus(Direction::Right)]);
+ defaults.insert(Key::Char('n'), vec![Action::NewPane(None)]);
+ defaults.insert(Key::Char('d'), vec![Action::NewPane(Some(Direction::Down))]);
+ defaults.insert(
+ Key::Char('r'),
+ vec![Action::NewPane(Some(Direction::Right))],
+ );
+ defaults.insert(Key::Char('x'), vec![Action::CloseFocus]);
- defaults.insert(Key::Char('f'), Action::ToggleFocusFullscreen);
+ defaults.insert(Key::Char('f'), vec![Action::ToggleFocusFullscreen]);
- defaults.insert(Key::Char('q'), Action::Quit);
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
+ defaults.insert(Key::Char('q'), vec![Action::Quit]);
+ defaults.insert(
+ Key::Ctrl('g'),
+ vec![Action::SwitchToMode(InputMode::Normal)],
+ );
+ defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]);
}
InputMode::Tab => {
- defaults.insert(Key::Char('h'), Action::GoToPreviousTab);
- defaults.insert(Key::Char('j'), Action::GoToNextTab);
- defaults.insert(Key::Char('k'), Action::GoToPreviousTab);
- defaults.insert(Key::Char('l'), Action::GoToNextTab);
+ defaults.insert(Key::Char('h'), vec![Action::GoToPreviousTab]);
+ defaults.insert(Key::Char('j'), vec![Action::GoToNextTab]);
+ defaults.insert(Key::Char('k'), vec![Action::GoToPreviousTab]);
+ defaults.insert(Key::Char('l'), vec![Action::GoToNextTab]);
- defaults.insert(Key::Left, Action::GoToPreviousTab);
- defaults.insert(Key::Down, Action::GoToNextTab);
- defaults.insert(Key::Up, Action::GoToPreviousTab);
- defaults.insert(Key::Right, Action::GoToNextTab);
+ defaults.insert(Key::Left, vec![Action::GoToPreviousTab]);
+ defaults.insert(Key::Down, vec![Action::GoToNextTab]);
+ defaults.insert(Key::Up, vec![Action::GoToPreviousTab]);
+ defaults.insert(Key::Right, vec![Action::GoToNextTab]);
- defaults.insert(Key::Ctrl('b'), Action::GoToPreviousTab);
- defaults.insert(Key::Ctrl('n'), Action::GoToNextTab);
- defaults.insert(Key::Ctrl('p'), Action::GoToPreviousTab);
- defaults.insert(Key::Ctrl('f'), Action::GoToNextTab);
+ defaults.insert(Key::Ctrl('b'), vec![Action::GoToPreviousTab]);
+ defaults.insert(Key::Ctrl('n'), vec![Action::GoToNextTab]);
+ defaults.insert(Key::Ctrl('p'), vec![Action::GoToPreviousTab]);
+ defaults.insert(Key::Ctrl('f'), vec![Action::GoToNextTab]);
- defaults.insert(Key::Char('n'), Action::NewTab);
- defaults.insert(Key::Char('x'), Action::CloseTab);
+ defaults.insert(Key::Char('n'), vec![Action::NewTab]);
+ defaults.insert(Key::Char('x'), vec![Action::CloseTab]);
- defaults.insert(Key::Char('q'), Action::Quit);
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
+ defaults.insert(Key::Char('q'), vec![Action::Quit]);
+ defaults.insert(
+ Key::Ctrl('g'),
+ vec![Action::SwitchToMode(InputMode::Normal)],
+ );
+ defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]);
}
InputMode::Scroll => {
- defaults.insert(Key::Char('j'), Action::ScrollDown);
- defaults.insert(Key::Char('k'), Action::ScrollUp);
+ defaults.insert(Key::Char('j'), vec![Action::ScrollDown]);
+ defaults.insert(Key::Char('k'), vec![Action::ScrollUp]);
- defaults.insert(Key::Down, Action::ScrollDown);
- defaults.insert(Key::Up, Action::ScrollUp);
+ defaults.insert(Key::Down, vec![Action::ScrollDown]);
+ defaults.insert(Key::Up, vec![Action::ScrollUp]);
- defaults.insert(Key::Ctrl('n'), Action::ScrollDown);
- defaults.insert(Key::Ctrl('p'), Action::ScrollUp);
+ defaults.insert(Key::Ctrl('n'), vec![Action::ScrollDown]);
+ defaults.insert(Key::Ctrl('p'), vec![Action::ScrollUp]);
- defaults.insert(Key::Char('q'), Action::Quit);
- defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
- defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
+ defaults.insert(Key::Char('q'), vec![Action::Quit]);
+ defaults.insert(
+ Key::Ctrl('g'),
+ vec![Action::SwitchToMode(InputMode::Normal)],
+ );
+ defaults.insert(Key::Esc, vec![Action::SwitchToMode(InputMode::Command)]);
}
InputMode::Exiting => {}
}
@@ -130,14 +158,23 @@ fn get_defaults_for_mode(mode: &InputMode) -> Result<ModeKeybinds, String> {
Ok(defaults)
}
-pub fn key_to_action(key: &Key, input: Vec<u8>, mode: &InputMode, keybinds: &Keybinds) -> Action {
+/// Converts a [`Key`] terminal event to a sequence of [`Action`]s according to the current
+/// [`InputMode`] and [`Keybinds`].
+pub fn key_to_actions(
+ key: &Key,
+ input: Vec<u8>,
+ mode: &InputMode,
+ keybinds: &Keybinds,
+) -> Vec<Action> {
if let Some(mode_keybinds) = keybinds.get(mode) {
mode_keybinds
.get(key)
.cloned()
- .unwrap_or(Action::Write(input))
+ // FIXME in command mode, unbound keystrokes should probably do nothing instead of
+ // writing to the terminal. Will be easier to implement after a big refactor of the
+ // input system (@categorille)
+ .unwrap_or(vec![Action::Write(input)])
} else {
- // Unrecognized mode - panic?
- panic!("Unrecognized mode: {:?}", mode);
+ unreachable!("Unrecognized mode: {:?}", mode);
}
}