37
34
class BashCodeGen(object):
38
35
"""Generate a bash script for given completion data."""
40
def __init__(self, data, function_name='_brz', debug=False):
37
def __init__(self, data, function_name='_bzr', debug=False):
42
39
self.function_name = function_name
47
# Programmable completion for the Breezy brz command under bash.
44
# Programmable completion for the Bazaar-NG bzr command under bash.
48
45
# Known to work with bash 2.05a as well as bash 4.1.2, and probably
49
46
# all versions in between as well.
55
52
# Generated using the bash_completion plugin.
56
53
# See https://launchpad.net/bzr-bash-completion for details.
58
# Commands and options of brz %(brz_version)s
55
# Commands and options of bzr %(bzr_version)s
62
complete -F %(function_name)s -o default brz
59
complete -F %(function_name)s -o default bzr
64
61
"function_name": self.function_name,
65
62
"function": self.function(),
66
"brz_version": self.brz_version(),
63
"bzr_version": self.bzr_version(),
69
66
def function(self):
128
125
if [[ ${#fixedWords[@]} -eq 0 ]] && [[ ${#optEnums[@]} -eq 0 ]] && [[ $cur != -* ]]; then
131
fixedWords=( $(brz tags 2>/dev/null | sed 's/ *[^ ]*$//; s/ /\\\\\\\\ /g;') )
128
fixedWords=( $(bzr tags 2>/dev/null | sed 's/ *[^ ]*$//; s/ /\\\\\\\\ /g;') )
136
fixedWords=( $(brz tags 2>/dev/null | sed 's/ *[^ ]*$//; s/^/tag:/') )
133
fixedWords=( $(bzr tags 2>/dev/null | sed 's/ *[^ ]*$//; s/^/tag:/') )
138
135
[\\"\\']*..tag:*)
139
fixedWords=( $(brz tags 2>/dev/null | sed 's/ *[^ ]*$//') )
136
fixedWords=( $(bzr tags 2>/dev/null | sed 's/ *[^ ]*$//') )
140
137
fixedWords=( $(for i in "${fixedWords[@]}"; do echo "${cur%%..tag:*}..tag:${i}"; done) )
186
183
echo -ne '---\e[K\e[u'
189
def brz_version(self):
190
brz_version = breezy.version_string
186
def bzr_version(self):
187
bzr_version = bzrlib.version_string
191
188
if not self.data.plugins:
194
brz_version += " and the following plugins:"
195
for name, plugin in sorted(self.data.plugins.items()):
196
brz_version += "\n# %s" % plugin
191
bzr_version += " and the following plugins:"
192
for name, plugin in sorted(self.data.plugins.iteritems()):
193
bzr_version += "\n# %s" % plugin
199
196
def global_options(self):
200
197
return " ".join(sorted(self.data.global_options))
299
296
elif selected_plugins is None:
300
297
self.selected_plugins = None
302
self.selected_plugins = {x.replace('-', '_')
303
for x in selected_plugins}
299
self.selected_plugins = set([x.replace('-', '_')
300
for x in selected_plugins])
305
302
def collect(self):
306
303
self.global_options()
317
314
self.data.global_options.add(short)
319
316
def aliases(self):
320
for alias, expansion in config.GlobalConfig().get_aliases().items():
317
for alias, expansion in config.GlobalConfig().get_aliases().iteritems():
321
318
for token in cmdline.split(expansion):
322
319
if not token.startswith("-"):
323
320
self.user_aliases.setdefault(token, set()).add(alias)
355
352
if useralias not in cmd_data.aliases]))
357
354
opts = cmd.options()
358
for optname, opt in sorted(opts.items()):
355
for optname, opt in sorted(opts.iteritems()):
359
356
cmd_data.options.extend(self.option(opt))
361
358
if 'help' == name or 'help' in cmd.aliases:
378
375
enum_data.registry_keys = opt.registry.keys()
379
except ImportError as e:
376
except ImportError, e:
380
377
enum_data.error_messages.append(
381
378
"ERROR getting registry keys for '--%s': %s"
382
379
% (opt.name, str(e).split('\n')[0]))
398
395
return self.wrap_container(optswitches, parser)
401
def bash_completion_function(out, function_name="_brz", function_only=False,
398
def bash_completion_function(out, function_name="_bzr", function_only=False,
403
400
no_plugins=False, selected_plugins=None):
404
401
dc = DataCollector(no_plugins=no_plugins, selected_plugins=selected_plugins)
419
416
the completion key (usually tab).
421
418
Commonly used like this:
422
eval "`brz bash-completion`"
419
eval "`bzr bash-completion`"
425
422
takes_options = [
426
option.Option("function-name", short_name="f", type=text_type, argname="name",
427
help="Name of the generated function (default: _brz)"),
423
option.Option("function-name", short_name="f", type=str, argname="name",
424
help="Name of the generated function (default: _bzr)"),
428
425
option.Option("function-only", short_name="o", type=None,
429
426
help="Generate only the shell function, don't enable it"),
430
427
option.Option("debug", type=None, hidden=True,
431
428
help="Enable shell code useful for debugging"),
432
option.ListOption("plugin", type=text_type, argname="name",
429
option.ListOption("plugin", type=str, argname="name",
433
430
# param_name="selected_plugins", # doesn't work, bug #387117
434
431
help="Enable completions for the selected plugin"
435
432
+ " (default: all plugins)"),
459
456
parser = optparse.OptionParser(usage="%prog [-f NAME] [-o]")
460
457
parser.add_option("--function-name", "-f", metavar="NAME",
461
help="Name of the generated function (default: _brz)")
458
help="Name of the generated function (default: _bzr)")
462
459
parser.add_option("--function-only", "-o", action="store_true",
463
460
help="Generate only the shell function, don't enable it")
464
461
parser.add_option("--debug", action="store_true",
465
462
help=optparse.SUPPRESS_HELP)
466
463
parser.add_option("--no-plugins", action="store_true",
467
help="Don't load any brz plugins")
464
help="Don't load any bzr plugins")
468
465
parser.add_option("--plugin", metavar="NAME", type="string",
469
466
dest="selected_plugins", default=[],
470
467
action="callback", callback=plugin_callback,