-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate_funckeys_alias.py
794 lines (708 loc) · 19.5 KB
/
generate_funckeys_alias.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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
import humps
import itertools
from lib import json_pretty_print
import json
# import string
# libraries providing naming style conversion
# dedicated: pip install pyhumps
# included: pip install flashback (lazero like utils inside)
# TODO: record commit activity everyday, count for work time. LOC. "git dashboard"
# TODO: integrate input-method like sequence inference capability to agent execution interface
# TODO: use json schema constrained or GBNF constrained grammer to restrict agent generation
# TODO: stepwise decoding in ollama: https://github.com/ollama/ollama/issues/6302
FUNC_KEYS = [
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"F10",
"F11",
"F12",
]
EXTRA_KEYS_WITH_ARROW_KEYS = [
"IC",
"Insert",
"DC",
"Delete",
"Home",
"End",
"NPage",
"PageDown",
"PgDn",
"PPage",
"PageUp",
"PgUp",
"Tab",
"BTab",
"Space",
"BSpace",
"Enter",
"Escape",
"Up",
"Down",
"Left",
"Right",
]
CTRL_HOTKEYS = [
"C-Tab",
"C-Space",
"C-a",
"C-b",
"C-c",
"C-d",
"C-e",
"C-f",
"C-g",
"C-h",
"C-i",
"C-j",
"C-k",
"C-l",
"C-m",
"C-n",
"C-o",
"C-p",
"C-q",
"C-r",
"C-s",
"C-t",
"C-u",
"C-v",
"C-w",
"C-x",
"C-y",
"C-z",
"C-\\",
"C-]",
"C-[",
"C-^",
"C-_",
"C-/",
"C-,",
"C-.",
"C-?",
]
CTRL_SHIFT_HOTKEYS = ["C-S-Tab"]
META_HOTKEYS = [
"M-Escape",
"M-Space",
"M-!",
'M-"',
"M-#",
"M-$",
"M-%",
"M-&",
"M-'",
"M-(",
"M-)",
"M-*",
"M-+",
"M-,",
"M--",
"M-.",
"M-/",
"M-0",
"M-1",
"M-2",
"M-3",
"M-4",
"M-5",
"M-6",
"M-7",
"M-8",
"M-9",
"M-:",
"M-;",
"M-<",
"M-=",
"M->",
"M-?",
"M-@",
"M-A",
"M-B",
"M-C",
"M-D",
"M-E",
"M-F",
"M-G",
"M-H",
"M-I",
"M-J",
"M-K",
"M-L",
"M-M",
"M-N",
"M-O",
"M-P",
"M-Q",
"M-R",
"M-S",
"M-T",
"M-U",
"M-V",
"M-W",
"M-X",
"M-Y",
"M-Z",
"M-[",
"M-\\",
"M-]",
"M-^",
"M-_",
"M-`",
"M-a",
"M-b",
"M-c",
"M-d",
"M-e",
"M-f",
"M-g",
"M-h",
"M-i",
"M-j",
"M-k",
"M-l",
"M-m",
"M-n",
"M-o",
"M-p",
"M-q",
"M-r",
"M-s",
"M-t",
"M-u",
"M-v",
"M-w",
"M-x",
"M-y",
"M-z",
"M-{",
"M-|",
"M-}",
"M-~",
"M-Tab",
"M-BSpace",
"M-KP*",
"M-KP+",
"M-KP-",
"M-KP.",
"M-KP/",
"M-KP0",
"M-KP1",
"M-KP2",
"M-KP3",
"M-KP4",
"M-KP5",
"M-KP6",
"M-KP7",
"M-KP8",
"M-KP9",
]
KEYPAD_KEYS = [
"KP*",
"KP+",
"KP-",
"KP.",
"KP/",
"KP0",
"KP1",
"KP2",
"KP3",
"KP4",
"KP5",
"KP6",
"KP7",
"KP8",
"KP9",
]
META_CONTROL_HOTKEYS = [
"M-C-a",
"M-C-b",
"M-C-c",
"M-C-d",
"M-C-e",
"M-C-f",
"M-C-g",
"M-C-h",
"M-C-i",
"M-C-j",
"M-C-k",
"M-C-l",
"M-C-m",
"M-C-n",
"M-C-o",
"M-C-p",
"M-C-q",
"M-C-r",
"M-C-s",
"M-C-t",
"M-C-u",
"M-C-v",
"M-C-w",
"M-C-x",
"M-C-y",
"M-C-z",
"M-C-\\",
"M-C-]",
"M-C-^",
"M-C-_",
]
EXTENDED_KEY_PREFIXES = [
"S",
"M",
"S-M",
"C",
"S-C",
"C-M",
"S-C-M",
]
EXTENDED_KEY_SUFFIXES = [
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F8",
"F9",
"F10",
"F11",
"F12",
"Up",
"Down",
"Right",
"Left",
"Home",
"End",
"PPage",
"PageUp",
"PgUp",
"NPage",
"PageDown",
"PgDn",
"IC",
"Insert",
"DC",
"Delete",
]
PREDEFINED_ALIASES = {
"BSpace": ["BackSpace"],
"BTab": ["BackTab"],
"Ctrl": ["Control"],
"Esc": ["Escape"],
"Insert": ["IC", "InsertCharacter"],
"Delete": ["DC", "DeleteCharacter"],
"PgDn": ["PageDown", "NPage"],
"PgUp": ["PageUp", "PPage"],
"Enter": ["Return", "CarriageReturn", "CR", "LineFeed", "LF", "CRLF"],
"Up": [
"ArrowUp",
"CursorUp",
],
"Down": [
"ArrowDown",
"CursorDown",
],
"Left": [
"ArrowLeft",
"CursorLeft",
],
"Right": [
"ArrowRight",
"CursorRight",
],
"Meta": ["Alt"],
"\\": ["BackSlash"],
"[": ["LeftSquareBracket"],
"]": ["RightSquareBracket"],
"{": ["LeftCurlyBracket"],
"}": ["RightCurlyBracket"],
"(": ["LeftParenthesis", "LP"],
")": ["RightParenthesis", "RP"],
"<": ["LessThan", "Lt"],
">": ["GreaterThan", "Gt"],
"|": ["VerticalBar", "VB"],
"-": ["Hyphen"],
"+": ["Plus"],
"=": ["Equals", "Eq"],
"*": ["Asterisk"],
"/": ["Slash"],
"%": ["Percent"],
"#": ["Hash"],
"&": ["Ampersand", "Amp"],
"$": ["Dollar"],
"@": ["At"],
"!": ["Exclamation"],
"?": ["Question"],
"^": ["Caret"],
"~": ["Tilde"],
"`": ["Backtick"],
"'": ["SingleQuote", "SQ"],
'"': ["DoubleQuote", "DQ"],
":": ["Colon"],
";": ["Semicolon"],
".": ["Period"],
",": ["Comma"],
"_": ["Underscore"],
"0": ["DigitZero", "Zero"],
"1": ["DigitOne", "One"],
"2": ["DigitTwo", "Two"],
"3": ["DigitThree", "Three"],
"4": ["DigitFour", "Four"],
"5": ["DigitFive", "Five"],
"6": ["DigitSix", "Six"],
"7": ["DigitSeven", "Seven"],
"8": ["DigitEight", "Eight"],
"9": ["DigitNine", "Nine"],
"Space": ["SpaceKey"],
"Tab": ["TabKey"],
}
PREDEFINED_ALIASES_REVERSE_MAP = {
it: k for k, v in PREDEFINED_ALIASES.items() for it in v
}
EXTENDED_MODIFIER_KEY_COMBOS = ("S-M", "S-C", "C-M", "S-C-M")
MODIFIER_KEY_LETTER_TO_NAME = {"C": "Ctrl", "S": "Shift", "M": "Meta"}
KEYPAD_SHORTHAND = "KP"
KEYPAD_FULLNAME_LIST = ["KeyPad", "KeyPadKey"]
# TODO: imitate `/usr/lib/command-not-found` and handle inaccurate keystrokes from agent
def generate_multiforms(key: str):
baseform = humps.depascalize(key)
camelform = humps.camelize(baseform)
kebaform = humps.kebabize(baseform)
return baseform, camelform, kebaform
def generate_case_aliases(key: str):
return key.lower(), key.upper(), key.title()
def generate_hotkey_with_plus_connector(hotkey: str):
ret = hotkey
for it in MODIFIER_KEY_LETTER_TO_NAME.keys():
ret = ret.replace(f"{it}-", f"{it}+")
return ret
def generate_all_permutation_hotkeys(key: str):
components = key.split("-")
ret = []
prefixes = components[:-1]
suffix = components[-1]
for it in itertools.permutations(prefixes):
ret.append("-".join(list(it) + [suffix]))
return ret
def generate_funckey_aliases():
ret = {}
for standard_key in FUNC_KEYS:
aliases = set()
derived_keys = [
standard_key.replace("F", "F-"),
standard_key.replace("F", "F_"),
]
for key in [standard_key, *derived_keys]:
fullkey_list = [
key.replace("F", "FuncKey"),
key.replace("F", "FunctionKey"),
key.replace("F", "Function"),
key.replace("F", "Func"),
key.replace("F", "Fn"),
]
for fullkey in fullkey_list:
baseform, camelform, kebaform = generate_multiforms(fullkey)
candidates = [key.lower(), fullkey, baseform, camelform, kebaform]
aliases.update(candidates)
for it in candidates:
case_aliases = generate_case_aliases(it)
aliases.update(case_aliases)
ret[standard_key] = list(aliases)
return ret
def generate_additionalkey_aliases():
ret = {}
for key in EXTRA_KEYS_WITH_ARROW_KEYS:
candidates = [key]
aliases = set()
if key in PREDEFINED_ALIASES_REVERSE_MAP.keys():
continue
if key in PREDEFINED_ALIASES.keys():
candidates.extend(PREDEFINED_ALIASES[key])
for it in candidates:
baseform, camelform, kebaform = generate_multiforms(it)
all_forms = [it, baseform, camelform, kebaform]
aliases.update(all_forms)
for form in all_forms:
aliases.update(generate_case_aliases(form))
ret[key] = list(aliases)
return ret
def generate_display_and_update_aliases(candidates: dict, name: str, ret: dict):
print(f"[*] {name} aliases:")
json_pretty_print(candidates)
ret.update(candidates)
def generate_modifier_key_aliases(modifier_letter: str):
aliases = set()
modifier_name = MODIFIER_KEY_LETTER_TO_NAME[modifier_letter]
modifier_predefined_aliases = PREDEFINED_ALIASES.get(modifier_name, [])
fullnames = [modifier_name, *modifier_predefined_aliases]
aliases.update(fullnames)
for it in fullnames:
extended_alias = f"{it}Key"
aliases.add(extended_alias)
ret = list(aliases)
return ret
def filter_invalid_modifier_keys(modkeys: list[str]):
valid_keys = set()
for it in modkeys:
prefix = it[:-1]
if "-" not in prefix and "+" not in prefix:
continue
else:
if len(it) > 3:
for invalid_connector in ["-_", "+-", "+_", "--", "__"]:
if it.endswith(invalid_connector):
it = (
it[:-2].replace(invalid_connector, invalid_connector[0])
+ invalid_connector
)
else:
it = it.replace(invalid_connector, invalid_connector[0])
valid_keys.add(it)
ret = list(valid_keys)
return ret
def filter_invalid_keypad_keys(modkeys: list[str]):
valid_keys = set()
for it in modkeys:
prefix = it[:-1]
if "-" not in prefix and "+" not in prefix:
pass
else:
if len(it) > 3:
for invalid_connector in ["-_", "+-", "+_", "--", "__"]:
if it.endswith(invalid_connector):
it = (
it[:-2].replace(invalid_connector, invalid_connector[0])
+ invalid_connector
)
else:
it = it.replace(invalid_connector, invalid_connector[0])
valid_keys.add(it)
ret = list(valid_keys)
return ret
def generate_ctrl_hotkey_aliases():
ret = {}
for standard_key in CTRL_HOTKEYS:
aliases = set()
suffix = standard_key.split("C-")[-1]
suffix_aliases = PREDEFINED_ALIASES.get(suffix, [])
derived_keys = [f"C-{it}" for it in suffix_aliases]
for key in [standard_key, *derived_keys]:
plus_connected_key = generate_hotkey_with_plus_connector(key)
ctrl_key_aliases = generate_modifier_key_aliases("C")
fullkey_list = [(key, False), (plus_connected_key, True)]
for it in ctrl_key_aliases:
fullkey_list.append((key.replace("C-", f"{it}-"), False))
fullkey_list.append((plus_connected_key.replace("C+", f"{it}+"), True))
for fullkey, _ in fullkey_list:
baseform, camelform, kebaform = generate_multiforms(fullkey)
candidates = [fullkey, baseform, camelform, kebaform]
aliases.update(candidates)
for it in candidates:
case_aliases = generate_case_aliases(it)
aliases.update(case_aliases)
ret[standard_key] = filter_invalid_modifier_keys(list(aliases))
return ret
def generate_keypadkey_aliases():
ret = {}
for key, derived_key in [(it, it.replace("KP", "KP-")) for it in KEYPAD_KEYS]:
suffix = key[-1]
suffix_derivatives = [suffix, *PREDEFINED_ALIASES.get(suffix, [])]
aliases = set()
fullkey_list = []
for new_suffix in suffix_derivatives:
new_key = f"{key[:-1]}{new_suffix}"
new_derived_key = f"{derived_key[:-1]}{new_suffix}"
for it in KEYPAD_FULLNAME_LIST:
fullkey_list.append(new_key.replace("KP", it))
fullkey_list.append(new_derived_key.replace("KP", it))
for fullkey in fullkey_list:
baseform, camelform, kebaform = generate_multiforms(fullkey)
candidates = [fullkey, baseform, camelform, kebaform]
aliases.update(candidates)
for it in candidates:
case_aliases = generate_case_aliases(it)
aliases.update(case_aliases)
key_aliases = list(aliases)
key_aliases = filter_invalid_keypad_keys(key_aliases)
ret[key] = key_aliases
return ret
def check_if_is_invalid_meta_key(meta_key: str):
meta_key = meta_key.replace("-", "_")
items = meta_key.split("_")
for it in items[:-1]:
if len(it) == 1 and it.isalpha():
return True
return False
def filter_invalid_meta_keys(aliases: list[str]):
ret = []
for it in aliases:
invalid = check_if_is_invalid_meta_key(it)
if invalid:
continue
ret.append(it)
return ret
def generate_meta_hotkey_aliases(current_aliases: dict):
ret = {}
for standard_key in META_HOTKEYS:
aliases = set()
suffix = standard_key.split("M-")[-1]
suffix_is_alpha = suffix.isalpha()
suffix_aliases = current_aliases.get(suffix, [])
derived_keys = [f"M-{it}" for it in suffix_aliases]
for key in [standard_key, *derived_keys]:
plus_connected_key = generate_hotkey_with_plus_connector(key)
ctrl_key_aliases = generate_modifier_key_aliases("M")
fullkey_list = [(key, False), (plus_connected_key, True)]
for it in ctrl_key_aliases:
fullkey_list.append((key.replace("M-", f"{it}-"), False))
fullkey_list.append((plus_connected_key.replace("M+", f"{it}+"), True))
for fullkey, _ in fullkey_list:
baseform, camelform, kebaform = generate_multiforms(fullkey)
candidates = [fullkey, baseform, camelform, kebaform]
aliases.update(candidates)
for it in candidates:
case_aliases = generate_case_aliases(it)
aliases.update(case_aliases)
key_aliases = filter_invalid_modifier_keys(list(aliases))
key_aliases = list(
set([it[:-1] + suffix if suffix_is_alpha else it for it in key_aliases])
)
key_aliases = filter_invalid_meta_keys(key_aliases)
ret[standard_key] = key_aliases
return ret
def get_modifier_letter_aliases(modkey_letter: str):
letter_fullname = MODIFIER_KEY_LETTER_TO_NAME[modkey_letter]
predefined_aliases = PREDEFINED_ALIASES.get(letter_fullname, [])
ret = [letter_fullname, *predefined_aliases]
ret = ret + [f"{it}Key" for it in ret]
return ret
def generate_hotkey_fullkeys(hotkey: str, hotkey_letters: list[str], connector: str):
fullkeys = set([hotkey])
for i in range(1, len(hotkey_letters) + 1):
for letters in itertools.combinations(hotkey_letters, i):
product_params = []
for it in letters:
letter_alias_list = get_modifier_letter_aliases(it)
product_params.append(letter_alias_list)
source_prefix = connector.join(letters) + connector
for prod_result in itertools.product(*product_params):
candidate = hotkey
target_prefix = connector.join(prod_result) + connector
candidate = candidate.replace(source_prefix, target_prefix)
fullkeys.add(candidate)
ret = list(fullkeys)
return ret
def generate_meta_ctrl_hotkey_aliases():
ret = generate_multi_hotkey_aliases(META_CONTROL_HOTKEYS, ["M", "C"])
return ret
def generate_ctrl_shift_hotkey_aliases():
ret = generate_multi_hotkey_aliases(CTRL_SHIFT_HOTKEYS, ["S", "C"])
return ret
def generate_multi_hotkey_aliases(init_hotkeys: list[str], hotkey_letters: list[str]):
ret = {}
for key in init_hotkeys:
aliases = set()
permuted_keys = generate_all_permutation_hotkeys(key)
fullkey_list = set()
for it in permuted_keys:
fullkey_list.update(generate_hotkey_fullkeys(it, hotkey_letters, "-"))
fullkey_list.update(
generate_hotkey_fullkeys(
generate_hotkey_with_plus_connector(it), hotkey_letters, "+"
)
)
fullkey_list = list(fullkey_list)
for fullkey in fullkey_list:
baseform, camelform, kebaform = generate_multiforms(fullkey)
candidates = [fullkey, baseform, camelform, kebaform]
aliases.update(candidates)
for it in candidates:
case_aliases = generate_case_aliases(it)
aliases.update(case_aliases)
hotkey_aliases = list(aliases)
hotkey_aliases = filter_invalid_modifier_keys(hotkey_aliases)
ret[key] = list(set(hotkey_aliases))
return ret
def generate_extended_hotkey_aliases():
ret = {}
for key_combo, suffix in itertools.product(
EXTENDED_MODIFIER_KEY_COMBOS, EXTENDED_KEY_SUFFIXES
):
hotkey_letters = key_combo.split("-")
hotkey = "-".join([key_combo, suffix])
aliases = generate_multi_hotkey_aliases([hotkey], hotkey_letters) # type: ignore
ret.update(aliases)
return ret
def generate_all_aliases():
ret = {}
generate_display_and_update_aliases(generate_funckey_aliases(), "FuncKey", ret)
generate_display_and_update_aliases(
generate_additionalkey_aliases(), "AdditionalKey", ret
)
generate_display_and_update_aliases(generate_keypadkey_aliases(), "KeypadKey", ret)
generate_display_and_update_aliases(
generate_ctrl_hotkey_aliases(), "CtrlHotKey", ret
)
generate_display_and_update_aliases(
generate_meta_hotkey_aliases(ret), "MetaHotKey", ret
)
generate_display_and_update_aliases(
generate_meta_ctrl_hotkey_aliases(), "MetaCtrlHotKey", ret
)
generate_display_and_update_aliases(
generate_ctrl_shift_hotkey_aliases(), "CtrlShiftHotKey", ret
)
generate_display_and_update_aliases(
generate_extended_hotkey_aliases(), "ExtendedHotKey", ret
)
return ret
def main():
all_aliases = generate_all_aliases()
key_aliases_lut = {it: k for k, v in all_aliases.items() for it in v}
for k in all_aliases.keys():
key_aliases_lut[k] = k
output_path = "key_aliases_lut.json"
print("[*] Write to:", output_path)
with open(output_path, "w+") as f:
f.write(json.dumps(key_aliases_lut, ensure_ascii=False, indent=4))
def test():
test_cases = [
(
"C-Tab",
[
"Ctrl-Tab",
"Control-Tab",
"c-tab",
"c+tab",
"ctrl-tab",
"ctrl+tab",
"control-tab",
"control+tab",
"C-TAB",
"C+TAB",
"CTRL+TAB",
"CTRL-TAB",
"CONTROL+TAB",
"CONTROL-TAB",
],
),
]
aliases = generate_all_aliases()
for key, value in test_cases:
key_aliases = aliases[key]
for it in value:
assert it in key_aliases, f"Alias '{it}' not found for key '{key}'"
remained_aliases = [it for it in key_aliases if it not in value]
assert (
remained_aliases == []
), f"Containing unexpected aliases for key '{key}': {remained_aliases}"
if __name__ == "__main__":
# test()
main()
# generate_all_aliases()