summaryrefslogtreecommitdiffstats
path: root/src/config/key_bindings.rs
blob: f60b58020a3e1b067fec79998c7d7f6926542532 (plain)
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
use crate::{
	config::{errors::ConfigError, utils::get_input},
	git::Config,
};

fn map_single_ascii_to_lower(s: &str) -> String {
	if s.is_ascii() && s.len() == 1 {
		s.to_lowercase()
	}
	else {
		String::from(s)
	}
}

/// Represents the key binding configuration options.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub(crate) struct KeyBindings {
	/// Key bindings for aborting.
	pub(crate) abort: Vec<String>,
	/// Key bindings for the break action.
	pub(crate) action_break: Vec<String>,
	/// Key bindings for the drop action.
	pub(crate) action_drop: Vec<String>,
	/// Key bindings for the edit action.
	pub(crate) action_edit: Vec<String>,
	/// Key bindings for the fixup action.
	pub(crate) action_fixup: Vec<String>,
	/// Key bindings for the pick action.
	pub(crate) action_pick: Vec<String>,
	/// Key bindings for the reword action.
	pub(crate) action_reword: Vec<String>,
	/// Key bindings for the squash action.
	pub(crate) action_squash: Vec<String>,
	/// Key bindings for negative confirmation.
	pub(crate) confirm_no: Vec<String>,
	/// Key bindings for positive confirmation.
	pub(crate) confirm_yes: Vec<String>,
	/// Key bindings for editing.
	pub(crate) edit: Vec<String>,
	/// Key bindings for forcing a abort.
	pub(crate) force_abort: Vec<String>,
	/// Key bindings for forcing a rebase.
	pub(crate) force_rebase: Vec<String>,
	/// Key bindings for showing help.
	pub(crate) help: Vec<String>,
	/// Key bindings for inserting a line.
	pub(crate) insert_line: Vec<String>,

	/// Key bindings for moving down.
	pub(crate) move_down: Vec<String>,
	/// Key bindings for moving to the end.
	pub(crate) move_end: Vec<String>,
	/// Key bindings for moving to the start.
	pub(crate) move_home: Vec<String>,
	/// Key bindings for moving to the left.
	pub(crate) move_left: Vec<String>,
	/// Key bindings for moving to the right.
	pub(crate) move_right: Vec<String>,
	/// Key bindings for moving up.
	pub(crate) move_up: Vec<String>,
	/// Key bindings for moving down a step.
	pub(crate) move_down_step: Vec<String>,
	/// Key bindings for moving up a step.
	pub(crate) move_up_step: Vec<String>,
	/// Key bindings for moving the selection down.
	pub(crate) move_selection_down: Vec<String>,
	/// Key bindings for moving the selection up.
	pub(crate) move_selection_up: Vec<String>,

	/// Key bindings for scrolling down.
	pub(crate) scroll_down: Vec<String>,
	/// Key bindings for scrolling to the end.
	pub(crate) scroll_end: Vec<String>,
	/// Key bindings for scrolling to the start.
	pub(crate) scroll_home: Vec<String>,
	/// Key bindings for scrolling to the left.
	pub(crate) scroll_left: Vec<String>,
	/// Key bindings for scrolling to the right.
	pub(crate) scroll_right: Vec<String>,
	/// Key bindings for scrolling up.
	pub(crate) scroll_up: Vec<String>,
	/// Key bindings for scrolling down a step.
	pub(crate) scroll_step_down: Vec<String>,
	/// Key bindings for scrolling up a step.
	pub(crate) scroll_step_up: Vec<String>,

	/// Key bindings for opening the external editor.
	pub(crate) open_in_external_editor: Vec<String>,
	/// Key bindings for rebasing.
	pub(crate) rebase: Vec<String>,
	/// Key bindings for redoing a change.
	pub(crate) redo: Vec<String>,
	/// Key bindings for removing a line.
	pub(crate) remove_line: Vec<String>,
	/// Key bindings for starting search.
	pub(crate) search_start: Vec<String>,
	/// Key bindings for next search match.
	pub(crate) search_next: Vec<String>,
	/// Key bindings for previous search match.
	pub(crate) search_previous: Vec<String>,
	/// Key bindings for showing a commit.
	pub(crate) show_commit: Vec<String>,
	/// Key bindings for showing a diff.
	pub(crate) show_diff: Vec<String>,
	/// Key bindings for toggling visual mode.
	pub(crate) toggle_visual_mode: Vec<String>,
	/// Key bindings for undoing a change.
	pub(crate) undo: Vec<String>,
	/// Key bindings for the fixup specific action to toggle the c option.
	pub(crate) fixup_keep_message_with_editor: Vec<String>,
	/// Key bindings for the fixup specific action to toggle the c option.
	pub(crate) fixup_keep_message: Vec<String>,
}

impl KeyBindings {
	/// Create a new configuration with default values.
	#[must_use]
	#[allow(clippy::missing_panics_doc)]
	pub(crate) fn new() -> Self {
		Self::new_with_config(None).unwrap() // should never error with None config
	}

	pub(super) fn new_with_config(git_config: Option<&Config>) -> Result<Self, ConfigError> {
		let confirm_no = get_input(git_config, "interactive-rebase-tool.inputConfirmNo", "n")?
			.iter()
			.map(|s| map_single_ascii_to_lower(s))
			.collect();
		let confirm_yes = get_input(git_config, "interactive-rebase-tool.inputConfirmYes", "y")?
			.iter()
			.map(|s| map_single_ascii_to_lower(s))
			.collect();
		Ok(Self {
			abort: get_input(git_config, "interactive-rebase-tool.inputAbort", "q")?,
			action_break: get_input(git_config, "interactive-rebase-tool.inputActionBreak", "b")?,
			action_drop: get_input(git_config, "interactive-rebase-tool.inputActionDrop", "d")?,
			action_edit: get_input(git_config, "interactive-rebase-tool.inputActionEdit", "e")?,
			action_fixup: get_input(git_config, "interactive-rebase-tool.inputActionFixup", "f")?,
			action_pick: get_input(git_config, "interactive-rebase-tool.inputActionPick", "p")?,
			action_reword: get_input(git_config, "interactive-rebase-tool.inputActionReword", "r")?,
			action_squash: get_input(git_config, "interactive-rebase-tool.inputActionSquash", "s")?,
			confirm_no,
			confirm_yes,
			edit: get_input(git_config, "interactive-rebase-tool.inputEdit", "E")?,
			force_abort: get_input(git_config, "interactive-rebase-tool.inputForceAbort", "Q")?,
			force_rebase: get_input(git_config, "interactive-rebase-tool.inputForceRebase", "W")?,
			help: get_input(git_config, "interactive-rebase-tool.inputHelp", "?")?,
			insert_line: get_input(git_config, "interactive-rebase-tool.insertLine", "I")?,
			move_down: get_input(git_config, "interactive-rebase-tool.inputMoveDown", "Down")?,
			move_end: get_input(git_config, "interactive-rebase-tool.inputMoveEnd", "End")?,
			move_home: get_input(git_config, "interactive-rebase-tool.inputMoveHome", "Home")?,
			move_left: get_input(git_config, "interactive-rebase-tool.inputMoveLeft", "Left")?,
			move_right: get_input(git_config, "interactive-rebase-tool.inputMoveRight", "Right")?,
			move_down_step: get_input(git_config, "interactive-rebase-tool.inputMoveStepDown", "PageDown")?,
			move_up_step: get_input(git_config, "interactive-rebase-tool.inputMoveStepUp", "PageUp")?,
			move_up: get_input(git_config, "interactive-rebase-tool.inputMoveUp", "Up")?,
			move_selection_down: get_input(git_config, "interactive-rebase-tool.inputMoveSelectionDown", "j")?,
			move_selection_up: get_input(git_config, "interactive-rebase-tool.inputMoveSelectionUp", "k")?,
			scroll_down: get_input(git_config, "interactive-rebase-tool.inputScrollDown", "Down")?,
			scroll_end: get_input(git_config, "interactive-rebase-tool.inputScrollEnd", "End")?,
			scroll_home: get_input(git_config, "interactive-rebase-tool.inputScrollHome", "Home")?,
			scroll_left: get_input(git_config, "interactive-rebase-tool.inputScrollLeft", "Left")?,
			scroll_right: get_input(git_config, "interactive-rebase-tool.inputScrollRight", "Right")?,
			scroll_up: get_input(git_config, "interactive-rebase-tool.inputScrollUp", "Up")?,
			scroll_step_down: get_input(git_config, "interactive-rebase-tool.inputScrollStepDown", "PageDown")?,
			scroll_step_up: get_input(git_config, "interactive-rebase-tool.inputScrollStepUp", "PageUp")?,
			open_in_external_editor: get_input(git_config, "interactive-rebase-tool.inputOpenInExternalEditor", "!")?,
			rebase: get_input(git_config, "interactive-rebase-tool.inputRebase", "w")?,
			redo: get_input(git_config, "interactive-rebase-tool.inputRedo", "control+y")?,
			remove_line: get_input(git_config, "interactive-rebase-tool.removeLine", "delete")?,
			search_start: get_input(git_config, "interactive-rebase-tool.searchStart", "/")?,
			search_next: get_input(git_config, "interactive-rebase-tool.searchNext", "n")?,
			search_previous: get_input(git_config, "interactive-rebase-tool.searchPrevious", "N")?,
			show_commit: get_input(git_config, "interactive-rebase-tool.inputShowCommit", "c")?,
			show_diff: get_input(git_config, "interactive-rebase-tool.inputShowDiff", "d")?,
			toggle_visual_mode: get_input(git_config, "interactive-rebase-tool.inputToggleVisualMode", "v")?,
			undo: get_input(git_config, "interactive-rebase-tool.inputUndo", "control+z")?,
			fixup_keep_message_with_editor: get_input(
				git_config,
				"interactive-rebase-tool.fixupKeepMessageWithEditor",
				"U",
			)?,
			fixup_keep_message: get_input(git_config, "interactive-rebase-tool.fixupKeepMessage", "u")?,
		})
	}
}

impl TryFrom<&Config> for KeyBindings {
	type Error = ConfigError;

	fn try_from(config: &Config) -> Result<Self, Self::Error> {
		Self::new_with_config(Some(config))
	}
}