-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathkeys.py
676 lines (645 loc) · 20.3 KB
/
keys.py
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
"""
Keybindings and their helper functions
"""
from typing import Dict, List, Tuple
from typing_extensions import NotRequired, TypedDict
from urwid.command_map import (
ACTIVATE,
CURSOR_DOWN,
CURSOR_LEFT,
CURSOR_MAX_RIGHT,
CURSOR_PAGE_DOWN,
CURSOR_PAGE_UP,
CURSOR_RIGHT,
CURSOR_UP,
command_map,
)
class KeyBinding(TypedDict):
keys: List[str]
help_text: str
excluded_from_random_tips: NotRequired[bool]
key_category: str
key_contexts: List[str]
# fmt: off
KEY_BINDINGS: Dict[str, KeyBinding] = {
# Key that is displayed in the UI is determined by the method
# primary_key_for_command. (Currently the first key in the list)
'HELP': {
'keys': ['?'],
'help_text': 'Show/hide Help Menu',
'excluded_from_random_tips': True,
'key_category': 'general',
'key_contexts': ['general'],
},
'MARKDOWN_HELP': {
'keys': ['meta m'],
'help_text': 'Show/hide Markdown Help Menu',
'key_category': 'general',
'key_contexts': ['general'],
},
'ABOUT': {
'keys': ['meta ?'],
'help_text': 'Show/hide About Menu',
'key_category': 'general',
'key_contexts': ['general'],
},
'OPEN_DRAFT': {
'keys': ['d'],
'help_text': 'Open draft message saved in this session',
'key_category': 'open_compose',
'key_contexts': ['general'],
},
'COPY_ABOUT_INFO': {
'keys': ['c'],
'help_text': 'Copy information from About Menu to clipboard',
'key_category': 'general',
'key_contexts': ['about'],
},
'EXIT_POPUP': {
'keys': ['esc'],
'help_text': 'Close popup',
'key_category': 'navigation',
'key_contexts': ['popup'],
},
'GO_UP': {
'keys': ['up', 'k'],
'help_text': 'Go up / Previous message',
'key_category': 'navigation',
'key_contexts': ['general'],
},
'GO_DOWN': {
'keys': ['down', 'j'],
'help_text': 'Go down / Next message',
'key_category': 'navigation',
'key_contexts': ['general'],
},
'GO_LEFT': {
'keys': ['left', 'h'],
'help_text': 'Go left',
'key_category': 'navigation',
'key_contexts': ['general'],
},
'GO_RIGHT': {
'keys': ['right', 'l'],
'help_text': 'Go right',
'key_category': 'navigation',
'key_contexts': ['general'],
},
'SCROLL_UP': {
'keys': ['page up', 'K'],
'help_text': 'Scroll up',
'key_category': 'navigation',
'key_contexts': ['general'],
},
'SCROLL_DOWN': {
'keys': ['page down', 'J'],
'help_text': 'Scroll down',
'key_category': 'navigation',
'key_contexts': ['general'],
},
'GO_TO_BOTTOM': {
'keys': ['end', 'G'],
'help_text': 'Go to bottom / Last message',
'key_category': 'navigation',
'key_contexts': ['general'],
},
'ACTIVATE_BUTTON': {
'keys': ['enter', ' '],
'help_text': 'Trigger the selected entry',
'key_category': 'navigation',
'key_contexts': ['button'],
},
'REPLY_MESSAGE': {
'keys': ['r', 'enter'],
'help_text': 'Reply to the current message',
'key_category': 'open_compose',
'key_contexts': ['message'],
},
'MENTION_REPLY': {
'keys': ['@'],
'help_text': 'Reply mentioning the sender of the current message',
'key_category': 'open_compose',
'key_contexts': ['message'],
},
'QUOTE_REPLY': {
'keys': ['>'],
'help_text': 'Reply quoting the current message text',
'key_category': 'open_compose',
'key_contexts': ['message'],
},
'REPLY_AUTHOR': {
'keys': ['R'],
'help_text': 'Reply directly to the sender of the current message',
'key_category': 'open_compose',
'key_contexts': ['message'],
},
'EDIT_MESSAGE': {
'keys': ['e'],
'help_text': "Edit message's content or topic",
'key_category': 'msg_actions',
'key_contexts': ['message'],
},
'STREAM_MESSAGE': {
'keys': ['c'],
'help_text': 'New message to a stream',
'key_category': 'open_compose',
'key_contexts': ['general'],
},
'PRIVATE_MESSAGE': {
'keys': ['x'],
'help_text': 'New message to a person or group of people',
'key_category': 'open_compose',
'key_contexts': ['general'],
},
'CYCLE_COMPOSE_FOCUS': {
'keys': ['tab'],
'help_text': 'Cycle through recipient and content boxes',
'key_category': 'compose_box',
'key_contexts': ['compose_box'],
},
'SEND_MESSAGE': {
'keys': ['ctrl d', 'meta enter'],
'help_text': 'Send a message',
'key_category': 'compose_box',
'key_contexts': ['compose_box'],
},
'SAVE_AS_DRAFT': {
'keys': ['meta s'],
'help_text': 'Save current message as a draft',
'key_category': 'compose_box',
'key_contexts': ['compose_box'],
},
'AUTOCOMPLETE': {
'keys': ['ctrl f'],
'help_text': ('Autocomplete @mentions, #stream_names, :emoji:'
' and topics'),
'key_category': 'compose_box',
'key_contexts': ['compose_box'],
},
'AUTOCOMPLETE_REVERSE': {
'keys': ['ctrl r'],
'help_text': 'Cycle through autocomplete suggestions in reverse',
'key_category': 'compose_box',
'key_contexts': ['compose_box'],
},
'ADD_REACTION': {
'keys': [':'],
'help_text': 'Show/hide emoji picker for current message',
'key_category': 'msg_actions',
'key_contexts': ['message'],
},
'STREAM_NARROW': {
'keys': ['s'],
'help_text': 'View the stream of the current message',
'key_category': 'narrowing',
'key_contexts': ['message'],
},
'TOPIC_NARROW': {
'keys': ['S'],
'help_text': 'View the topic of the current message',
'key_category': 'narrowing',
'key_contexts': ['message'],
},
'TOGGLE_NARROW': {
'keys': ['z'],
'help_text':
"Zoom in/out the message's conversation context",
'key_category': 'narrowing',
'key_contexts': ['message'],
},
'NARROW_MESSAGE_RECIPIENT': {
'keys': ['meta .'],
'help_text': 'Switch message view to the compose box target',
'key_category': 'narrowing',
'key_contexts': ['compose_box'],
},
'EXIT_COMPOSE': {
'keys': ['esc'],
'help_text': 'Exit message compose box',
'key_category': 'compose_box',
'key_contexts': ['compose_box'],
},
'REACTION_AGREEMENT': {
'keys': ['='],
'help_text': 'Toggle first emoji reaction on selected message',
'key_category': 'msg_actions',
'key_contexts': ['message'],
},
'TOGGLE_TOPIC': {
'keys': ['t'],
'help_text': 'Toggle topics in a stream',
'key_category': 'stream_list',
'key_contexts': ['stream', 'topic'],
},
'ALL_MESSAGES': {
'keys': ['a', 'esc'],
'help_text': 'View all messages',
'key_category': 'narrowing',
'key_contexts': ['message'],
},
'ALL_PM': {
'keys': ['P'],
'help_text': 'View all direct messages',
'key_category': 'narrowing',
'key_contexts': ['general'],
},
'ALL_STARRED': {
'keys': ['f'],
'help_text': 'View all starred messages',
'key_category': 'narrowing',
'key_contexts': ['general'],
},
'ALL_MENTIONS': {
'keys': ['#'],
'help_text': "View all messages in which you're mentioned",
'key_category': 'narrowing',
'key_contexts': ['general'],
},
'NEXT_UNREAD_TOPIC': {
'keys': ['n'],
'help_text': 'Next unread topic',
'key_category': 'narrowing',
'key_contexts': ['general'],
},
'NEXT_UNREAD_PM': {
'keys': ['p'],
'help_text': 'Next unread direct message',
'key_category': 'narrowing',
'key_contexts': ['general'],
},
'SEARCH_PEOPLE': {
'keys': ['w'],
'help_text': 'Search users',
'key_category': 'searching',
'key_contexts': ['general'],
},
'SEARCH_MESSAGES': {
'keys': ['/'],
'help_text': 'Search messages',
'key_category': 'searching',
'key_contexts': ['general'],
},
'SEARCH_STREAMS': {
'keys': ['q'],
'help_text': 'Search streams',
'key_category': 'searching',
'key_contexts': ['general'],
'excluded_from_random_tips': True,
# TODO: condition check required
},
'SEARCH_TOPICS': {
'keys': ['q'],
'help_text': 'Search topics in a stream',
'key_category': 'searching',
'key_contexts': ['general'],
'excluded_from_random_tips': True,
# TODO: condition check required
},
'SEARCH_EMOJIS': {
'keys': ['p'],
'help_text': 'Search emojis from emoji picker',
'key_category': 'searching',
'key_contexts': ['emoji_list'],
},
'EXECUTE_SEARCH': {
'keys': ['enter'],
'help_text': 'Submit search and browse results',
'key_category': 'searching',
'key_contexts': ['search_box'],
},
'CLEAR_SEARCH': {
'keys': ['esc'],
'help_text': 'Clear search in current panel',
'key_category': 'searching',
'key_contexts': ['message', 'stream', 'topic', 'user'],
'excluded_from_random_tips': True,
# TODO: condition check required
},
'TOGGLE_MUTE_STREAM': {
'keys': ['m'],
'help_text': 'Mute/unmute streams',
'key_category': 'stream_list',
'key_contexts': ['stream'],
},
'THUMBS_UP': {
'keys': ['+'],
'help_text': 'Toggle thumbs-up reaction to the current message',
'key_category': 'msg_actions',
'key_contexts': ['message'],
},
'TOGGLE_STAR_STATUS': {
'keys': ['ctrl s', '*'],
'help_text': 'Toggle star status of the current message',
'key_category': 'msg_actions',
'key_contexts': ['message'],
},
'MSG_INFO': {
'keys': ['i'],
'help_text': 'Show/hide message information',
'key_category': 'msg_actions',
'key_contexts': ['message', 'msg_info'],
},
'MSG_SENDER_INFO': {
'keys': ['u'],
'help_text': 'Show/hide message sender information',
'key_category': 'msg_actions',
'key_contexts': ['msg_info'],
},
'EDIT_HISTORY': {
'keys': ['e'],
'help_text': 'Show/hide edit history (from message information)',
'key_category': 'msg_actions',
'key_contexts': ['msg_info'],
},
'VIEW_IN_BROWSER': {
'keys': ['v'],
'help_text':
'View current message in browser (from message information)',
'key_category': 'msg_actions',
'key_contexts': ['msg_info'],
},
'STREAM_INFO': {
'keys': ['i'],
'help_text': 'Show/hide stream information & modify settings',
'key_category': 'stream_list',
'key_contexts': ['stream', 'stream_info'],
},
'STREAM_MEMBERS': {
'keys': ['m'],
'help_text': 'Show/hide stream members (from stream information)',
'key_category': 'stream_list',
'key_contexts': ['stream_info'],
},
'COPY_STREAM_EMAIL': {
'keys': ['c'],
'help_text':
'Copy stream email to clipboard (from stream information)',
'key_category': 'stream_list',
'key_contexts': ['stream_info'],
},
'REDRAW': {
'keys': ['ctrl l'],
'help_text': 'Redraw screen',
'key_category': 'general',
'key_contexts': ['global'],
},
'QUIT': {
'keys': ['ctrl c'],
'help_text': 'Quit',
'key_category': 'general',
'key_contexts': ['global'],
},
'USER_INFO': {
'keys': ['i'],
'help_text': 'Show/hide user information (from users list)',
'key_category': 'general',
'key_contexts': ['user'],
},
'BEGINNING_OF_LINE': {
'keys': ['ctrl a', 'home'],
'help_text': 'Start of line',
'key_category': 'editor_navigation',
'key_contexts': ['editor'],
},
'END_OF_LINE': {
'keys': ['ctrl e', 'end'],
'help_text': 'End of line',
'key_category': 'editor_navigation',
'key_contexts': ['editor'],
},
'ONE_WORD_BACKWARD': {
'keys': ['meta b', 'shift left'],
'help_text': 'Start of current or previous word',
'key_category': 'editor_navigation',
'key_contexts': ['editor'],
},
'ONE_WORD_FORWARD': {
'keys': ['meta f', 'shift right'],
'help_text': 'Start of next word',
'key_category': 'editor_navigation',
'key_contexts': ['editor'],
},
'PREV_LINE': {
'keys': ['up', 'ctrl p'],
'help_text': 'Previous line',
'key_category': 'editor_navigation',
'key_contexts': ['editor'],
},
'NEXT_LINE': {
'keys': ['down', 'ctrl n'],
'help_text': 'Next line',
'key_category': 'editor_navigation',
'key_contexts': ['editor'],
},
'UNDO_LAST_ACTION': {
'keys': ['ctrl _'],
'help_text': 'Undo last action',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'CLEAR_MESSAGE': {
'keys': ['ctrl l'],
'help_text': 'Clear text box',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'CUT_TO_END_OF_LINE': {
'keys': ['ctrl k'],
'help_text': 'Cut forwards to the end of the line',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'CUT_TO_START_OF_LINE': {
'keys': ['ctrl u'],
'help_text': 'Cut backwards to the start of the line',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'CUT_TO_END_OF_WORD': {
'keys': ['meta d'],
'help_text': 'Cut forwards to the end of the current word',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'CUT_TO_START_OF_WORD': {
'keys': ['ctrl w', 'meta backspace'],
'help_text': 'Cut backwards to the start of the current word',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'CUT_WHOLE_LINE': {
'keys': ['meta x'],
'help_text': 'Cut the current line',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'PASTE_LAST_CUT': {
'keys': ['ctrl y'],
'help_text': 'Paste last cut section',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'DELETE_LAST_CHARACTER': {
'keys': ['ctrl h'],
'help_text': 'Delete previous character',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'TRANSPOSE_CHARACTERS': {
'keys': ['ctrl t'],
'help_text': 'Swap with previous character',
'key_category': 'editor_text_manipulation',
'key_contexts': ['editor'],
},
'NEW_LINE': {
# urwid_readline's command
# This obvious hotkey is added to clarify against 'enter' to send
# and to differentiate from other hotkeys using 'enter'.
'keys': ['enter'],
'help_text': 'Insert new line',
'key_category': 'compose_box',
'key_contexts': ['compose_box'],
},
'OPEN_EXTERNAL_EDITOR': {
'keys': ['ctrl o'],
'help_text': 'Open an external editor to edit the message content',
'key_category': 'compose_box',
'key_contexts': ['compose_box'],
},
'FULL_RENDERED_MESSAGE': {
'keys': ['f'],
'help_text': 'Show/hide full rendered message (from message information)',
'key_category': 'msg_actions',
'key_contexts': ['msg_info'],
},
'FULL_RAW_MESSAGE': {
'keys': ['r'],
'help_text': 'Show/hide full raw message (from message information)',
'key_category': 'msg_actions',
'key_contexts': ['msg_info'],
},
'NEW_HINT': {
'keys': ['tab'],
'help_text': 'New footer hotkey hint',
'key_category': 'general',
'key_contexts': ['general'],
},
}
# fmt: on
HELP_CATEGORIES = {
"general": "General",
"navigation": "Navigation",
"narrowing": "Switching Messages View",
"searching": "Searching",
"msg_actions": "Message actions",
"stream_list": "Stream list actions",
"open_compose": "Begin composing a message",
"compose_box": "Writing a message",
"editor_navigation": "Editor: Navigation",
"editor_text_manipulation": "Editor: Text Manipulation",
}
HELP_CONTEXTS: Dict[str, str] = {
"global": "Global",
"general": "General", # not in an editor or a popup
"editor": "Editor",
"compose_box": "Compose box",
"stream": "Stream list",
"topic": "Topic list",
"user": "User list",
"message": "Message",
"stream_info": "Stream information",
"msg_info": "Message information",
"emoji_list": "Emoji list",
"about": "About information",
"popup": "Popup",
"button": "Button",
"search_box": "Search box",
}
ZT_TO_URWID_CMD_MAPPING = {
"GO_UP": CURSOR_UP,
"GO_DOWN": CURSOR_DOWN,
"GO_LEFT": CURSOR_LEFT,
"GO_RIGHT": CURSOR_RIGHT,
"SCROLL_UP": CURSOR_PAGE_UP,
"SCROLL_DOWN": CURSOR_PAGE_DOWN,
"GO_TO_BOTTOM": CURSOR_MAX_RIGHT,
"ACTIVATE_BUTTON": ACTIVATE,
}
class InvalidCommand(Exception):
pass
def is_command_key(command: str, key: str) -> bool:
"""
Returns the mapped binding for a key if mapped
or the key otherwise.
"""
try:
return key in KEY_BINDINGS[command]["keys"]
except KeyError as exception:
raise InvalidCommand(command) from exception
def keys_for_command(command: str) -> List[str]:
"""
Returns the actual keys for a given mapped command
"""
try:
return list(KEY_BINDINGS[command]["keys"])
except KeyError as exception:
raise InvalidCommand(command) from exception
def primary_key_for_command(command: str) -> str:
"""
Primary Key is the key that will be displayed eg. in the UI
"""
return keys_for_command(command).pop(0)
URWID_KEY_TO_DISPLAY_KEY_MAPPING = {
"page up": "PgUp",
"page down": "PgDn",
}
def display_key_for_urwid_key(urwid_key: str) -> str:
"""
Returns a displayable user-centric format of the urwid key.
"""
if urwid_key == " ":
return "Space"
for urwid_map_key, display_map_key in URWID_KEY_TO_DISPLAY_KEY_MAPPING.items():
if urwid_map_key in urwid_key:
urwid_key = urwid_key.replace(urwid_map_key, display_map_key)
display_key = [
keyboard_key.capitalize()
if len(keyboard_key) > 1 and keyboard_key[0].islower()
else keyboard_key
for keyboard_key in urwid_key.split()
]
return " ".join(display_key)
def display_keys_for_command(command: str) -> List[str]:
"""
Returns the user-friendly display keys for a given mapped command
"""
return [
display_key_for_urwid_key(urwid_key) for urwid_key in keys_for_command(command)
]
def primary_display_key_for_command(command: str) -> str:
"""
Primary Display Key is the formatted display version of the primary key
"""
return display_key_for_urwid_key(primary_key_for_command(command))
def commands_for_random_tips(context: str = "") -> Tuple[List[KeyBinding], str]:
"""
Return list of commands which may be displayed as a random tip,
and their associated user-facing context name
"""
if not context or context not in HELP_CONTEXTS:
context = "global"
random_tips: List[KeyBinding] = [
key_binding
for key_binding in KEY_BINDINGS.values()
if not key_binding.get("excluded_from_random_tips", False)
and context in key_binding["key_contexts"]
]
if len(random_tips) == 0:
return commands_for_random_tips("global")
context_display_name = HELP_CONTEXTS.get(context, "Global")
return random_tips, context_display_name
# Refer urwid/command_map.py
# Adds alternate keys for standard urwid navigational commands.
for zt_cmd, urwid_cmd in ZT_TO_URWID_CMD_MAPPING.items():
for key in keys_for_command(zt_cmd):
command_map[key] = urwid_cmd