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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
use std::fmt::{Display, Formatter, Result as FmtResult};
use tui::style::{Color, Modifier, Style};
use tui::text::{Span, Spans, Text};
use tui::widgets::ListItem;

/// Key bindings of the application.
pub const KEY_BINDINGS: &[KeyBinding] = &[
	KeyBinding {
		key: "?",
		action: "show help",
		description: r#"
        Use arrow keys / hjkl to navigate through the key bindings.
        Corresponding commands and additional information will be shown here.
        :help
        "#,
	},
	KeyBinding {
		key: "o,space,enter",
		action: "show options",
		description: r#"
        Shows the options menu for the current tab.
        :options
        "#,
	},
	KeyBinding {
		key: "hjkl,arrows,pgkeys",
		action: "navigate",
		description: r#"
        Scrolls the current widget or selects the next/previous tab.
        M-<key>: scroll the table rows
        C-<key>,pgup,pgdown: scroll to top/bottom
        :scroll (row) up/down/left/right <amount>
        "#,
	},
	KeyBinding {
		key: "n",
		action: "switch to normal mode",
		description: r#"
        Resets the application mode.
        :normal
        "#,
	},
	KeyBinding {
		key: "v",
		action: "switch to visual mode",
		description: r#"
        Disables the mouse capture.
        :visual
        "#,
	},
	KeyBinding {
		key: "c",
		action: "switch to copy mode",
		description: r#"
        x: Copy the exported key
        i: Copy the key id
        f: Copy the key fingerprint
        u: Copy the user id
        1,2: Copy the content of the row
        :copy
        "#,
	},
	KeyBinding {
		key: "p,C-v",
		action: "paste from clipboard",
		description: ":paste",
	},
	KeyBinding {
		key: "x",
		action: "export key",
		description: r#"
        Exports the key to "$GNUPGHOME/out" or specified path via `--outdir`
        :export <pub/sec> <keyids>
        "#,
	},
	KeyBinding {
		key: "s",
		action: "sign key",
		description: r#"
        Signs the key with the default secret key.
        Same as `gpg --sign-key`
        :sign <keyid>
        "#,
	},
	KeyBinding {
		key: "e",
		action: "edit key",
		description: r#"
        Presents a menu for key management.
        Same as `gpg --edit-key`
        :edit <keyid>
        "#,
	},
	KeyBinding {
		key: "i",
		action: "import key(s)",
		description: r#"
        Imports the keys from given files.
        :import <file1> <file2>
        "#,
	},
	KeyBinding {
		key: "f",
		action: "receive key",
		description: r#"
        Imports the keys with the given key IDs from default keyserver.
        Same as `gpg --receive-keys`
        :receive <keyids>
        "#,
	},
	KeyBinding {
		key: "u",
		action: "send key",
		description: r#"
        Sends the key to the default keyserver.
        :send <keyid>
        "#,
	},
	KeyBinding {
		key: "g",
		action: "generate key",
		description: r#"
        Generates a new key pair with dialogs for all options.
        Same as `gpg --full-generate-key`
        :generate
        "#,
	},
	KeyBinding {
		key: "d,backspace",
		action: "delete key",
		description: r#"
        Removes the public/secret key from the keyring.
        :delete <pub/sec> <keyid>
        "#,
	},
	KeyBinding {
		key: "C-r",
		action: "refresh keys",
		description: r#"
        Requests updates for keys on the local keyring.
        Same as `gpg --refresh-keys`
        :refresh keys
        "#,
	},
	KeyBinding {
		key: "a",
		action: "toggle armored output",
		description: r#"
        Toggles ASCII armored output.
        The default is to create the binary OpenPGP format.
        :set armor <true/false>
        "#,
	},
	KeyBinding {
		key: "1,2,3",
		action: "set detail level",
		description: r#"
        1: Minimum
        2: Standard
        3: Full
        :set detail <level>
        "#,
	},
	KeyBinding {
		key: "t,tab",
		action: "toggle detail (all/selected)",
		description: ":toggle detail (all)",
	},
	KeyBinding {
		key: "`",
		action: "toggle table margin",
		description: ":set margin <0/1>",
	},
	KeyBinding {
		key: "m",
		action: "toggle table size",
		description: ":toggle",
	},
	KeyBinding {
		key: "C-s",
		action: "toggle style",
		description: ":set colored <true/false>",
	},
	KeyBinding {
		key: "/",
		action: "search",
		description: ":search <query>",
	},
	KeyBinding {
		key: ":",
		action: "run command",
		description: "Switches to command mode for running commands.",
	},
	KeyBinding {
		key: "r,f5",
		action: "refresh application",
		description: ":refresh",
	},
	KeyBinding {
		key: "q,C-c/d,escape",
		action: "quit application",
		description: ":quit",
	},
];

/// Representation of an individual key binding.
#[derive(Clone, Copy, Debug)]
pub struct KeyBinding<'a> {
	/// Key binding.
	key: &'a str,
	/// Brief description of the key binding action.
	action: &'a str,
	/// Full description of the action along with the commands.
	pub description: &'a str,
}

impl<'a> Display for KeyBinding<'a> {
	fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
		write!(
			f,
			"{}\n └─{}\n ",
			self.key
				.split(',')
				.fold(String::new(), |acc, v| format!("{}[{}] ", acc, v)),
			self.action
		)
	}
}

impl<'a> KeyBinding<'a> {
	/// Constructs a new instance of `KeyBinding`.
	pub fn new(key: &'a str, action: &'a str, description: &'a str) -> Self {
		Self {
			key,
			action,
			description,
		}
	}

	/// Returns the description text of the key binding.
	pub fn get_description_text(&self, command_style: Style) -> Text<'a> {
		let mut lines = Vec::new();
		for line in self.description.lines().map(|v| format!("{}\n", v.trim()))
		{
			lines.push(if line.starts_with(':') {
				Spans::from(Span::styled(line, command_style))
			} else {
				Spans::from(line)
			})
		}
		Text::from(lines)
	}

	/// Returns the key binding as a list item.
	pub fn as_list_item(
		&self,
		colored: bool,
		highlighted: bool,
	) -> ListItem<'a> {
		let highlight_style = if highlighted {
			Style::default().fg(Color::Reset)
		} else {
			Style::default()
		};
		ListItem::new(if colored {
			Text::from(vec![
				Spans::from(self.key.split(',').fold(
					Vec::new(),
					|mut keys, key| {
						keys.push(Span::styled("[", highlight_style));
						keys.push(Span::styled(
							key,
							Style::default()
								.fg(Color::Green)
								.add_modifier(Modifier::BOLD),
						));
						keys.push(Span::styled("] ", highlight_style));
						keys
					},
				)),
				Spans::from(vec![
					Span::styled(" └─", Style::default().fg(Color::DarkGray)),
					Span::styled(self.action, highlight_style),
				]),
				Spans::default(),
			])
		} else {
			Text::raw(self.to_string())
		})
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use pretty_assertions::assert_eq;
	use std::borrow::Cow::Borrowed;
	#[test]
	fn test_app_keys() {
		let key_binding =
			KeyBinding::new("q,esc", "quit", "quits the application\n:quit");
		assert_eq!("quits the application\n:quit", key_binding.description);
		assert_eq!(
			Text {
				lines: vec![
					Spans(vec![Span {
						content: Borrowed("quits the application\n"),
						style: Style::default(),
					}]),
					Spans(vec![Span {
						content: Borrowed(":quit\n"),
						style: Style::default().fg(Color::Red),
					}]),
				],
			},
			key_binding.get_description_text(Style::default().fg(Color::Red))
		);
		assert_eq!(
			ListItem::new(Text {
				lines: vec![
					Spans(vec![Span {
						content: Borrowed("[q] [esc] "),
						style: Style::default(),
					}]),
					Spans(vec![Span {
						content: Borrowed(" └─quit"),
						style: Style::default(),
					}]),
					Spans(vec![Span {
						content: Borrowed(" "),
						style: Style::default(),
					}]),
				],
			}),
			key_binding.as_list_item(false, false)
		);
		assert_eq!(
			ListItem::new(Text {
				lines: vec![
					Spans(vec![
						Span {
							content: Borrowed("["),
							style: Style {
								fg: Some(Color::Reset),
								..Style::default()
							},
						},
						Span {
							content: Borrowed("q"),
							style: Style {
								fg: Some(Color::Green),
								bg: None,
								add_modifier: Modifier::BOLD,
								sub_modifier: Modifier::empty(),
							},
						},
						Span {
							content: Borrowed("] "),
							style: Style {
								fg: Some(Color::Reset),
								..Style::default()
							},
						},
						Span {
							content: Borrowed("["),
							style: Style {
								fg: Some(Color::Reset),
								..Style::default()
							},
						},
						Span {
							content: Borrowed("esc"),
							style: Style {
								fg: Some(Color::Green),
								bg: None,
								add_modifier: Modifier::BOLD,
								sub_modifier: Modifier::empty(),
							},
						},
						Span {
							content: Borrowed("] "),
							style: Style {
								fg: Some(Color::Reset),
								..Style::default()
							},
						},
					]),
					Spans(vec![
						Span {
							content: Borrowed(" └─"),
							style: Style {
								fg: Some(Color::DarkGray),
								..Style::default()
							},
						},
						Span {
							content: Borrowed("quit"),
							style: Style {
								fg: Some(Color::Reset),
								..Style::default()
							},
						},
					]),
					Spans::default(),
				]
			}),
			key_binding.as_list_item(true, true)
		);
	}
}