rewrite zsh completion generation
This commit is contained in:
parent
b8b622fbeb
commit
b3ab97c05e
@ -3,26 +3,8 @@
|
|||||||
__youtube_dl() {
|
__youtube_dl() {
|
||||||
local curcontext="$curcontext" fileopts diropts cur prev
|
local curcontext="$curcontext" fileopts diropts cur prev
|
||||||
typeset -A opt_args
|
typeset -A opt_args
|
||||||
fileopts="{{fileopts}}"
|
_arguments {{args}} \
|
||||||
diropts="{{diropts}}"
|
'*: :(::ytfavorites ::ytrecommended ::ytsubscriptions ::ytwatchlater ::ythistory)'
|
||||||
cur=$words[CURRENT]
|
|
||||||
case $cur in
|
|
||||||
:)
|
|
||||||
_arguments '*: :(::ytfavorites ::ytrecommended ::ytsubscriptions ::ytwatchlater ::ythistory)'
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
prev=$words[CURRENT-1]
|
|
||||||
if [[ ${prev} =~ ${fileopts} ]]; then
|
|
||||||
_path_files
|
|
||||||
elif [[ ${prev} =~ ${diropts} ]]; then
|
|
||||||
_path_files -/
|
|
||||||
elif [[ ${prev} == "--recode-video" ]]; then
|
|
||||||
_arguments '*: :(mp4 flv ogg webm mkv)'
|
|
||||||
else
|
|
||||||
_arguments '*: :({{flags}})'
|
|
||||||
fi
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
}
|
||||||
|
|
||||||
__youtube_dl
|
__youtube_dl
|
||||||
|
@ -4,6 +4,7 @@ from __future__ import unicode_literals
|
|||||||
import os
|
import os
|
||||||
from os.path import dirname as dirn
|
from os.path import dirname as dirn
|
||||||
import sys
|
import sys
|
||||||
|
import optparse
|
||||||
|
|
||||||
sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
|
sys.path.insert(0, dirn(dirn((os.path.abspath(__file__)))))
|
||||||
import youtube_dl
|
import youtube_dl
|
||||||
@ -15,31 +16,45 @@ ZSH_COMPLETION_TEMPLATE = "devscripts/zsh-completion.in"
|
|||||||
def build_completion(opt_parser):
|
def build_completion(opt_parser):
|
||||||
opts = [opt for group in opt_parser.option_groups
|
opts = [opt for group in opt_parser.option_groups
|
||||||
for opt in group.option_list]
|
for opt in group.option_list]
|
||||||
opts_file = [opt for opt in opts if opt.metavar == "FILE"]
|
|
||||||
opts_dir = [opt for opt in opts if opt.metavar == "DIR"]
|
|
||||||
|
|
||||||
fileopts = []
|
# escaping is hard:
|
||||||
for opt in opts_file:
|
# - help may contain colons
|
||||||
|
# - metavar must have colons : escaped
|
||||||
|
# - single quotes must be removed
|
||||||
|
# - help must have square brackets [] escaped
|
||||||
|
def metaparse(opt):
|
||||||
|
if "--recode-video" == opt.get_opt_string():
|
||||||
|
return ":{}:(mp4 flv ogg webm mkv)".format(opt.metavar)
|
||||||
|
if opt.metavar is None:
|
||||||
|
return ""
|
||||||
|
if opt.metavar == "FILE":
|
||||||
|
return ":FILE:_files"
|
||||||
|
if opt.metavar == "DIR":
|
||||||
|
return ":DIR:_directories"
|
||||||
|
else:
|
||||||
|
return ":{}:".format(opt.metavar.replace(":", "\\:"))
|
||||||
|
|
||||||
|
def helpescape(opthelp):
|
||||||
|
if opthelp == optparse.SUPPRESS_HELP:
|
||||||
|
return ""
|
||||||
|
return "[{}]".format(opthelp.replace("'", "\"").replace("]", "\\]").replace("[", "\\["))
|
||||||
|
|
||||||
|
def optionexclude(opt):
|
||||||
|
# When an argument has a long and short version, the arguments entry shall be
|
||||||
|
# _arguments \
|
||||||
|
# "(-t --thing)"{-t,--thing}"[do things]:WHAT_THING:"
|
||||||
|
# i.e. in parentheses with space the explanation of redundancy and in curly braces
|
||||||
|
# regular shell expansion to create two mostly identical entries.
|
||||||
if opt._short_opts:
|
if opt._short_opts:
|
||||||
fileopts.extend(opt._short_opts)
|
return "({0} {1})'{{{0},{1}}}'".format(opt._short_opts[0], opt.get_opt_string())
|
||||||
if opt._long_opts:
|
return "{}".format(opt.get_opt_string())
|
||||||
fileopts.extend(opt._long_opts)
|
|
||||||
|
|
||||||
diropts = []
|
mytest = ["'{}{}{}'".format(optionexclude(opt), helpescape(opt.help), metaparse(opt)) for opt in opts]
|
||||||
for opt in opts_dir:
|
|
||||||
if opt._short_opts:
|
|
||||||
diropts.extend(opt._short_opts)
|
|
||||||
if opt._long_opts:
|
|
||||||
diropts.extend(opt._long_opts)
|
|
||||||
|
|
||||||
flags = [opt.get_opt_string() for opt in opts]
|
|
||||||
|
|
||||||
with open(ZSH_COMPLETION_TEMPLATE) as f:
|
with open(ZSH_COMPLETION_TEMPLATE) as f:
|
||||||
template = f.read()
|
template = f.read()
|
||||||
|
|
||||||
template = template.replace("{{fileopts}}", "|".join(fileopts))
|
template = template.replace("{{args}}", " \\\n ".join(mytest))
|
||||||
template = template.replace("{{diropts}}", "|".join(diropts))
|
|
||||||
template = template.replace("{{flags}}", " ".join(flags))
|
|
||||||
|
|
||||||
with open(ZSH_COMPLETION_FILE, "w") as f:
|
with open(ZSH_COMPLETION_FILE, "w") as f:
|
||||||
f.write(template)
|
f.write(template)
|
||||||
|
Loading…
Reference in New Issue
Block a user