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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::collections::HashMap;
use super::actions::Action;
use super::config;
use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;
use zellij_tile::data::*;
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Keybinds(HashMap<InputMode, ModeKeybinds>);
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct ModeKeybinds(HashMap<Key, Vec<Action>>);
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct KeybindsFromYaml {
#[serde(flatten)]
keybinds: HashMap<InputMode, Vec<KeyActionUnbind>>,
#[serde(default)]
unbind: Unbind,
}
#[derive(Clone, Debug, PartialEq, Deserialize)]
#[serde(untagged)]
enum KeyActionUnbind {
KeyAction(KeyActionFromYaml),
Unbind(UnbindFromYaml),
}
#[derive(Clone, Debug, PartialEq, Deserialize)]
struct KeyActionUnbindFromYaml {
keybinds: Vec<KeyActionFromYaml>,
unbind: Unbind,
}
#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct KeyActionFromYaml {
action: Vec<Action>,
key: Vec<Key>,
}
#[derive(Clone, Debug, PartialEq, Deserialize)]
struct UnbindFromYaml {
unbind: Unbind,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize)]
#[serde(untagged)]
enum Unbind {
Keys(Vec<Key>),
All(bool),
}
impl Default for Keybinds {
fn default() -> Keybinds {
Self::from_default_assets()
}
}
impl Keybinds {
pub fn new() -> Keybinds {
Keybinds(HashMap::<InputMode, ModeKeybinds>::new())
}
fn from_default_assets() -> Keybinds {
config::Config::from_default_assets()
.expect("Keybinds from default assets Error!")
.keybinds
}
pub fn get_default_keybinds_with_config(from_yaml: Option<KeybindsFromYaml>) -> Keybinds {
let default_keybinds = match from_yaml.clone() {
Some(keybinds) => match keybinds.unbind {
Unbind::All(true) => Keybinds::new(),
Unbind::All(false) | Unbind::Keys(_) => Keybinds::unbind(keybinds),
},
None => Keybinds::default(),
};
if let Some(keybinds) = from_yaml {
default_keybinds.merge_keybinds(Keybinds::from(keybinds))
} else {
default_keybinds
}
}
fn unbind(from_yaml: KeybindsFromYaml) -> Keybinds {
let mut keybind_config = Self::new();
let mut unbind_config: HashMap<InputMode, Unbind> = HashMap::new();
let keybinds_from_yaml = from_yaml.keybinds;
for mode in InputMode::iter() {
if let Some(keybinds) = keybinds_from_yaml.get(&mode) {
for keybind in keybinds.iter() {
match keybind {
KeyActionUnbind::Unbind(unbind) => {
unbind_config.insert(mode, unbind.unbind.clone());
}
KeyActionUnbind::KeyAction(key_action_from_yaml) => {
keybind_config
.0
.insert(mode, ModeKeybinds::from(key_action_from_yaml.clone()));
}
}
}
}
}
let mut default = Self::default().unbind_mode(unbind_config);
if let Unbind::Keys(_) = from_yaml.unbind {
let mut unbind_config: HashMap<InputMode, Unbind> = HashMap::new();
for mode in InputMode::iter() {
unbind_config.insert(mode, from_yaml.unbind.clone());
}
default = default.unbind_mode(unbind_config);
};
default.merge_keybinds(keybind_config)
}
fn unbind_mode(&self, unbind: HashMap<InputMode, Unbind>) -> Keybinds {
let mut keybinds = Keybinds::new();
for mode in InputMode::iter() {
if let Some(unbind) = unbind.get(&mode) {
match unbind {
Unbind::All(true) => {}
Unbind::Keys(keys) => {
if let Some(defaults) = self.0.get(&mode) {
keybinds
.0
.insert(mode, defaults.clone().unbind_keys(keys.to_vec()));
}
}
Unbind::All(false) => {
if let Some(defaults) = self.0.get(&mode) {
keybinds.0.insert(mode, defaults.clone());
}
}
}
} else if let Some(defaults) = self.0.get(&mode) {
keybinds.0.insert(mode, defaults.clone());
}
}
keybinds
}
fn merge_keybinds(&self, other: Keybinds) -> Keybinds {
let mut keybinds = Keybinds::new();
for mode in InputMode::iter() {
let mut mode_keybinds = ModeKeybinds::new();
if let Some(keybind) = self.0.get(&mode) {
mode_keybinds.0.extend(keybind.0.clone());
};
if let Some(keybind) = other.0.get(&mode) {
mode_keybinds.0.extend(keybind.0.clone());
}
if !mode_keybinds.0.is_empty() {
keybinds.0.insert(mode, mode_keybinds);
}
}
keybinds
}
pub fn key_to_actions(
key: &Key,
input: Vec<u8>,
mode: &InputMode,
keybinds: &Keybinds,
) -> Vec<Action> {
let mode_keybind_or_action = |action: Action| {
keybinds
.0
.get(mode)
.unwrap_or_else(|| unreachable!("Unrecognized mode: {:?}", mode))
.0
.get(key)
.cloned()
.unwrap_or_else(|| vec![action])
};
match *mode {
InputMode::Normal | InputMode::Locked => mode_keybind_or_action(Action::Write(input)),
InputMode::RenameTab => mode_keybind_or_action(Action::TabNameInput(input)),
_ => mode_keybind_or_action(Action::NoOp),
}
}
}
impl ModeKeybinds {
fn new() -> ModeKeybinds {
ModeKeybinds(HashMap::<Key, Vec<Action>>::new())
}
fn merge(self, other: ModeKeybinds) -> ModeKeybinds {
let mut merged = self;
merged.0.extend(other.0);
merged
}
fn unbind_keys(self, unbind: Vec<Key>) -> Self {
let mut keymap = self;
for key in unbind {
keymap.0.remove(&key);
}
keymap
}
}
impl From<KeybindsFromYaml> for Keybinds {
fn from(keybinds_from_yaml: KeybindsFromYaml) -> Keybinds {
let mut keybinds = Keybinds::new();
for mode in InputMode::iter() {
let mut mode_keybinds = ModeKeybinds::new();
for key_action in keybinds_from_yaml.keybinds.get(&mode).iter() {
for keybind in key_action.iter() {
mode_keybinds = mode_keybinds.merge(ModeKeybinds::from(keybind.clone()));
}
}
keybinds.0.insert(mode, mode_keybinds);
}
keybinds
}
}
impl From<KeyActionFromYaml> for ModeKeybinds {
fn from(key_action: KeyActionFromYaml) -> ModeKeybinds {
let actions = key_action.action;
ModeKeybinds(
key_action
.key
.into_iter()
.map(|k| (k, actions.clone()))
.collect::<HashMap<Key, Vec<Action>>>(),
)
}
}
impl From<KeyActionUnbind> for ModeKeybinds {
fn from(key_action_unbind: KeyActionUnbind) -> ModeKeybinds {
match key_action_unbind {
KeyActionUnbind::KeyAction(key_action) => ModeKeybinds::from(key_action),
KeyActionUnbind::Unbind(_) => ModeKeybinds::new(),
}
}
}
impl From<Vec<KeyActionFromYaml>> for ModeKeybinds {
fn from(key_action_from_yaml: Vec<KeyActionFromYaml>) -> ModeKeybinds {
let mut mode_keybinds = ModeKeybinds::new();
for keybind in key_action_from_yaml {
for key in keybind.key {
mode_keybinds.0.insert(key, keybind.action.clone());
}
}
mode_keybinds
}
}
impl Default for Unbind {
fn default() -> Unbind {
Unbind::All(false)
}
}
#[cfg(test)]
#[path = "./unit/keybinds_test.rs"]
mod keybinds_test;