aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/input/keybinds.rs
blob: d6ace68d0531be8bc89fe29e2baa0bf22e27ad9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// This module is for mapping particular input keys to their corresponding actions.

use super::actions::{Action, Direction};
use super::handler::InputMode;

use std::collections::HashMap;

use strum::IntoEnumIterator;
use termion::event::Key;

type Keybinds = HashMap<InputMode, ModeKeybinds>;
type ModeKeybinds = HashMap<Key, Action>;

/// Populate 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();

    for mode in InputMode::iter() {
        defaults.insert(mode, get_defaults_for_mode(&mode)?);
    }

    Ok(defaults)
}

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));
        }
        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);
        }
        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::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::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::Char('q'), Action::Quit);
            defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
            defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
        }
        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::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::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::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('f'), Action::ToggleFocusFullscreen);

            defaults.insert(Key::Char('q'), Action::Quit);
            defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
            defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
        }
        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::Left, Action::GoToPreviousTab);
            defaults.insert(Key::Down, Action::GoToNextTab);
            defaults.insert(Key::Up, Action::GoToPreviousTab);
            defaults.insert(Key::Right, 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::Char('n'), Action::NewTab);
            defaults.insert(Key::Char('x'), Action::CloseTab);

            defaults.insert(Key::Char('q'), Action::Quit);
            defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
            defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
        }
        InputMode::Scroll => {
            defaults.insert(Key::Char('j'), Action::ScrollDown);
            defaults.insert(Key::Char('k'), Action::ScrollUp);

            defaults.insert(Key::Down, Action::ScrollDown);
            defaults.insert(Key::Up, Action::ScrollUp);

            defaults.insert(Key::Ctrl('n'), Action::ScrollDown);
            defaults.insert(Key::Ctrl('p'), Action::ScrollUp);

            defaults.insert(Key::Char('q'), Action::Quit);
            defaults.insert(Key::Ctrl('g'), Action::TogglePersistentMode);
            defaults.insert(Key::Esc, Action::SwitchToMode(InputMode::Normal));
        }
        InputMode::Exiting => {}
    }

    Ok(defaults)
}

pub fn key_to_action(key: &Key, input: Vec<u8>, mode: &InputMode, keybinds: &Keybinds) -> Action {
    if let Some(mode_keybinds) = keybinds.get(mode) {
        mode_keybinds
            .get(key)
            .cloned()
            .unwrap_or(Action::Write(input))
    } else {
        // Unrecognized mode - panic?
        panic!("Unrecognized mode: {:?}", mode);
    }
}