-
Notifications
You must be signed in to change notification settings - Fork 16
/
gen_completions
executable file
·215 lines (180 loc) · 6.54 KB
/
gen_completions
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
#!/usr/bin/env python3
"""
truckersmp-cli completions generator.
This script generates completions for bash and zsh.
"""
import argparse
import json
import locale
import sys
from string import Template
from truckersmp_cli.args import ACTIONS, GAMES, create_arg_parser
from truckersmp_cli.variables import AppId, File
class CompletionTemplate(Template):
"""Template class that uses "%" as delimiter."""
delimiter = "%"
TMPL_BASH = CompletionTemplate(
"""# bash completion for truckersmp-cli -*- shell-script -*-
_truckersmp_cli() {
local cur prev
_get_comp_words_by_ref -n : cur prev
if [[ "${cur}" == -* ]]; then
COMPREPLY=( $(compgen -W "%{tmpl_bash_options}" -- "${cur}") )
return
fi
local diropts="%{tmpl_dir_options_regex}"
if [[ "${prev}" =~ ${diropts} ]]; then
local IFS=$'\\n'
compopt -o filenames
COMPREPLY=( $(compgen -d -- "${cur}") )
return
fi
local fileopts="%{tmpl_file_options_regex}"
if [[ "${prev}" =~ ${fileopts} ]]; then
local IFS=$'\\n'
compopt -o filenames
COMPREPLY=( $(compgen -f -- "${cur}") )
return
fi
local games="%{tmpl_games_regex}"
for ((i=1; i < ${COMP_CWORD}; i++)); do
if [[ "${COMP_WORDS[i]}" =~ ${games} ]]; then
COMPREPLY=()
return
fi
done
local acts="%{tmpl_actions_regex}"
for ((i=1; i < ${COMP_CWORD}; i++)); do
if [[ "${COMP_WORDS[i]}" =~ ${acts} ]]; then
COMPREPLY=( $(compgen -W "%{tmpl_games}" -- "${cur}") )
return
fi
done
COMPREPLY=( $(compgen -W "%{tmpl_actions}" -- "${cur}") )
} &&
complete -F _truckersmp_cli truckersmp-cli
""")
TMPL_ZSH = CompletionTemplate(
"""#compdef truckersmp-cli -*- shell-script -*-
# zsh completion for truckersmp-cli
__truckersmp_cli() {
typeset -A opt_args
local prev="${words[CURRENT-1]}"
local diropts="%{tmpl_dir_options_regex}"
local fileopts="%{tmpl_file_options_regex}"
if [[ "${prev}" =~ ${diropts} ]]; then
_path_files -/
return
elif [[ "${prev}" =~ ${fileopts} ]]; then
_path_files
return
fi
_arguments -s -S "1: :->action" "2: :->game" %{tmpl_zsh_options}
case "${state}" in
action)
_values action '%{tmpl_zsh_actions}'
;;
game)
_values game '%{tmpl_zsh_games}'
;;
esac
}
__truckersmp_cli
""")
def write_shell_completion_file(shellname, path, content):
"""
Write given content to specified completion file.
If path is None, this function does nothing and returns True.
If it successfully writes data to the file,
this function returns True.
If it fails to write, it prints an error message
and returns False.
shellname: Shell name, used in messages
path: Path to output completion file
content: Content of completion file
"""
if path is None:
return True
try:
with open(path, "w", encoding="utf-8") as f_out:
f_out.write(content)
except OSError as ex:
print(
f"Failed to write {shellname} completion file {path}: {ex}",
file=sys.stderr)
return False
print(f"Wrote {shellname} completion file {path}")
return True
def main():
"""Generate completions for bash and zsh."""
try:
with open(File.proton_json, encoding="utf-8") as f_in:
AppId.proton = json.load(f_in)
except (OSError, ValueError) as ex:
sys.exit(f"Failed to load proton.json: {ex}")
parser = argparse.ArgumentParser(
description="Shell completions generator")
parser.add_argument(
"-b", "--bash-completion", metavar="FILE",
help="write bash completion file.")
parser.add_argument(
"-z", "--zsh-completion", metavar="FILE",
help="write zsh completion file.")
config = parser.parse_args()
if config.bash_completion is None and config.zsh_completion is None:
return "At least --bash-completion(-b) or --zsh-completion(-z) option needed."
comp_data = dict(
action_names=[act[0] for act in ACTIONS],
bash_options=["-h --help"],
dir_options=[],
file_options=[],
game_names=[game[0] for game in GAMES],
zsh_actions=[],
zsh_games=[],
zsh_options=['{-h,--help}"[show help message and exit]:"', ],
)
for act in create_arg_parser()[1]:
if act.help.startswith("**DEPRECATED** "):
continue
if len(act.option_strings) > 1:
zsh_arg_optnames = "{" + ",".join(act.option_strings) + "}"
else:
zsh_arg_optnames = act.option_strings[0]
for opt in act.option_strings:
comp_data["bash_options"].append(opt)
if opt.endswith("dir"):
comp_data["dir_options"].append(opt)
elif opt.endswith("file"):
comp_data["file_options"].append(opt)
desc = act.help.replace("[", "\\[").replace("]", "\\]").replace('"', '\\"')
comp_data["zsh_options"].append(
f'{zsh_arg_optnames}"[{" ".join(desc.split())}]"')
for name, desc in ACTIONS:
comp_data["zsh_actions"].append(f"{name}[{desc}]")
for name, desc in GAMES:
comp_data["zsh_games"].append(f"{name}[{desc}]")
return (
write_shell_completion_file(
"bash", config.bash_completion, TMPL_BASH.substitute(
tmpl_actions=" ".join(comp_data["action_names"]),
tmpl_actions_regex="|".join(comp_data["action_names"]),
tmpl_bash_options=" ".join(comp_data["bash_options"]),
tmpl_dir_options_regex="|".join(comp_data["dir_options"]),
tmpl_file_options_regex="|".join(comp_data["file_options"]),
tmpl_games=" ".join(comp_data["game_names"]),
tmpl_games_regex="|".join(comp_data["game_names"]),
)
) is False or write_shell_completion_file(
"zsh", config.zsh_completion, TMPL_ZSH.substitute(
tmpl_dir_options_regex="|".join(comp_data["dir_options"]),
tmpl_file_options_regex="|".join(comp_data["file_options"]),
tmpl_zsh_actions="' '".join(comp_data["zsh_actions"]),
tmpl_zsh_games="' '".join(comp_data["zsh_games"]),
tmpl_zsh_options=" ".join(comp_data["zsh_options"]),
)
) is False
)
if __name__ == "__main__":
locale.setlocale(locale.LC_MESSAGES, "")
locale.setlocale(locale.LC_TIME, "C")
sys.exit(main())