-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelp_doc_gen.py
executable file
·57 lines (41 loc) · 1.84 KB
/
help_doc_gen.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
#!/usr/bin/env python3
"""Script for parsing a .tmux.conf file and pretty-printing all lines that are help key (C-h prefix)
bindings to STDOUT.
Used to dynamically generate the help-for-help (C-h ?) display, which mimics the emacs command /
binding of the same name.
"""
import collections
import re
import sys
# Config help binding extraction regex
CONF_HELP_BINDING_RE = r'bind -T HELP (\w) (.*)\s+#\s+(.*)'
# Output formatters
HELP_PAGE_LINE_FMT = '{key:<8}{doc}'
HELP_PAGE_HEADER = ('You have typed C-h, the help character. Type a Help option:\n'
'(Type q to exit the Help command.)')
HelpBinding = collections.namedtuple('HelpBinding', 'key command docstring')
def extract_help_bindings(tmux_conf_lines):
"""Extract all lines that are help key bindings in an iterable of tmux config lines and return
them as a list of HelpBinding namedtuples."""
return [HelpBinding(*re.match(CONF_HELP_BINDING_RE, line).groups()) for line in tmux_conf_lines
if re.match(CONF_HELP_BINDING_RE, line)]
def sort_help_bindings_by_key(help_bindings):
"""Sort a list of HelpBinding namedtuples alphabetically by key."""
return sorted(help_bindings, key=lambda help_binding: help_binding.key.lower())
def format_help_binding(help_binding):
"""Format a HelpBinding namedtuple for output to the hmux help page."""
return HELP_PAGE_LINE_FMT.format(
key=help_binding.key,
doc=help_binding.docstring
)
def main():
"""Main"""
with open(sys.argv[1]) as tmux_conf_file:
tmux_conf_lines = [line for line in tmux_conf_file]
help_bindings = extract_help_bindings(tmux_conf_lines)
help_bindings = sort_help_bindings_by_key(help_bindings)
print(HELP_PAGE_HEADER + '\n')
for help_binding in help_bindings:
print(format_help_binding(help_binding))
if __name__ == '__main__':
main()