mirror of
https://github.com/beetbox/beets.git
synced 2025-12-16 13:43:53 +01:00
Move completion_script into commands module
This commit is contained in:
parent
44c843793b
commit
c51e541ae2
2 changed files with 71 additions and 67 deletions
|
|
@ -23,6 +23,7 @@ import time
|
|||
import itertools
|
||||
import codecs
|
||||
from datetime import datetime
|
||||
from pkg_resources import resource_string
|
||||
|
||||
import beets
|
||||
from beets import ui
|
||||
|
|
@ -1300,6 +1301,76 @@ def print_completion(*args):
|
|||
log.warn(u'Warning: Unable to find the bash-completion package. '
|
||||
u'Command line completion might not work.')
|
||||
|
||||
def completion_script(commands):
|
||||
"""Yield the full completion shell script as strings.
|
||||
|
||||
``commands`` is alist of ``ui.Subcommand`` instances to generate
|
||||
completion data for.
|
||||
"""
|
||||
yield resource_string(__name__, 'completion_base.sh')
|
||||
|
||||
options = {}
|
||||
aliases = {}
|
||||
command_names = []
|
||||
|
||||
# Collect subcommands
|
||||
for cmd in commands:
|
||||
name = cmd.name
|
||||
command_names.append(name)
|
||||
|
||||
for alias in cmd.aliases:
|
||||
aliases[alias] = name
|
||||
|
||||
options[name] = {'flags': [], 'opts': []}
|
||||
for opts in cmd.parser._get_all_options()[1:]:
|
||||
if opts.action in ('store_true', 'store_false'):
|
||||
option_type = 'flags'
|
||||
else:
|
||||
option_type = 'opts'
|
||||
|
||||
options[name][option_type].extend(opts._short_opts + opts._long_opts)
|
||||
|
||||
# Add global options
|
||||
options['_global'] = {
|
||||
'flags': ['-v', '--verbose'],
|
||||
'opts': '-l --library -c --config -d --directory -h --help'.split(' ')
|
||||
}
|
||||
|
||||
# Help subcommand
|
||||
command_names.append('help')
|
||||
|
||||
# Add flags common to all commands
|
||||
options['_common'] = {
|
||||
'flags': ['-h', '--help']
|
||||
}
|
||||
|
||||
# Start generating the script
|
||||
yield "_beet() {\n"
|
||||
|
||||
# Command names
|
||||
yield " local commands='%s'\n" % ' '.join(command_names)
|
||||
yield "\n"
|
||||
|
||||
# Command aliases
|
||||
yield " local aliases='%s'\n" % ' '.join(aliases.keys())
|
||||
for alias, cmd in aliases.items():
|
||||
yield " local alias__%s=%s\n" % (alias, cmd)
|
||||
yield '\n'
|
||||
|
||||
# Fields
|
||||
yield " fields='%s'\n" % ' '.join(
|
||||
set(library.ITEM_KEYS + library.ALBUM_KEYS))
|
||||
|
||||
# Command options
|
||||
for cmd, opts in options.items():
|
||||
for option_type, option_list in opts.items():
|
||||
if option_list:
|
||||
option_list = ' '.join(option_list)
|
||||
yield " local %s__%s='%s'\n" % (option_type, cmd, option_list)
|
||||
|
||||
yield ' _beet_dispatch\n'
|
||||
yield '}\n'
|
||||
|
||||
|
||||
completion_cmd.func = print_completion
|
||||
default_commands.append(completion_cmd)
|
||||
|
|
|
|||
|
|
@ -1,67 +0,0 @@
|
|||
from pkg_resources import resource_string
|
||||
from beets import library
|
||||
|
||||
def completion_script(commands):
|
||||
yield resource_string(__name__, 'completion_base.sh')
|
||||
|
||||
options = {}
|
||||
aliases = {}
|
||||
command_names = []
|
||||
|
||||
# Collect subcommands
|
||||
for cmd in commands:
|
||||
name = cmd.name
|
||||
command_names.append(name)
|
||||
|
||||
for alias in cmd.aliases:
|
||||
aliases[alias] = name
|
||||
|
||||
options[name] = {'flags': [], 'opts': []}
|
||||
for opts in cmd.parser._get_all_options()[1:]:
|
||||
if opts.action in ('store_true', 'store_false'):
|
||||
option_type = 'flags'
|
||||
else:
|
||||
option_type = 'opts'
|
||||
|
||||
options[name][option_type].extend(opts._short_opts + opts._long_opts)
|
||||
|
||||
# Add global options
|
||||
options['_global'] = {
|
||||
'flags': ['-v', '--verbose'],
|
||||
'opts': '-l --library -c --config -d --directory -h --help'.split(' ')
|
||||
}
|
||||
|
||||
# Help subcommand
|
||||
command_names.append('help')
|
||||
|
||||
# Add flags common to all commands
|
||||
options['_common'] = {
|
||||
'flags': ['-h', '--help']
|
||||
}
|
||||
|
||||
# Start generating the script
|
||||
yield "_beet() {\n"
|
||||
|
||||
# Command names
|
||||
yield " local commands='%s'\n" % ' '.join(command_names)
|
||||
yield "\n"
|
||||
|
||||
# Command aliases
|
||||
yield " local aliases='%s'\n" % ' '.join(aliases.keys())
|
||||
for alias, cmd in aliases.items():
|
||||
yield " local alias__%s=%s\n" % (alias, cmd)
|
||||
yield '\n'
|
||||
|
||||
# Fields
|
||||
yield " fields='%s'\n" % ' '.join(
|
||||
set(library.ITEM_KEYS + library.ALBUM_KEYS))
|
||||
|
||||
# Command options
|
||||
for cmd, opts in options.items():
|
||||
for option_type, option_list in opts.items():
|
||||
if option_list:
|
||||
option_list = ' '.join(option_list)
|
||||
yield " local %s__%s='%s'\n" % (option_type, cmd, option_list)
|
||||
|
||||
yield ' _beet_dispatch\n'
|
||||
yield '}\n'
|
||||
Loading…
Reference in a new issue