/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/bash_completion/bashcomp.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python3
 
1
#!/usr/bin/env python
2
2
 
3
3
# Copyright (C) 2009, 2010 Canonical Ltd
4
4
#
16
16
# along with this program; if not, write to the Free Software
17
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
from ... import (
20
22
    cmdline,
21
23
    commands,
55
57
shopt -s progcomp
56
58
%(function)s
57
59
complete -F %(function_name)s -o default brz
58
 
""" % {
 
60
"""     % {
59
61
            "function_name": self.function_name,
60
62
            "function": self.function(),
61
63
            "brz_version": self.brz_version(),
65
67
        return ("""\
66
68
%(function_name)s ()
67
69
{
68
 
    local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
69
 
    local curOpt optEnums
70
 
    local IFS=$' \\n'
71
 
 
72
 
    COMPREPLY=()
73
 
    cur=${COMP_WORDS[COMP_CWORD]}
74
 
 
75
 
    cmds='%(cmds)s'
76
 
    globalOpts=( %(global_options)s )
77
 
 
78
 
    # do ordinary expansion if we are anywhere after a -- argument
79
 
    for ((i = 1; i < COMP_CWORD; ++i)); do
80
 
        [[ ${COMP_WORDS[i]} == "--" ]] && return 0
81
 
    done
82
 
 
83
 
    # find the command; it's the first word not starting in -
84
 
    cmd=
85
 
    for ((cmdIdx = 1; cmdIdx < ${#COMP_WORDS[@]}; ++cmdIdx)); do
86
 
        if [[ ${COMP_WORDS[cmdIdx]} != -* ]]; then
87
 
            cmd=${COMP_WORDS[cmdIdx]}
88
 
            break
89
 
        fi
90
 
    done
91
 
 
92
 
    # complete command name if we are not already past the command
93
 
    if [[ $COMP_CWORD -le cmdIdx ]]; then
94
 
        COMPREPLY=( $( compgen -W "$cmds ${globalOpts[*]}" -- $cur ) )
95
 
        return 0
96
 
    fi
97
 
 
98
 
    # find the option for which we want to complete a value
99
 
    curOpt=
100
 
    if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
101
 
        curOpt=${COMP_WORDS[COMP_CWORD - 1]}
102
 
        if [[ $curOpt == = ]]; then
103
 
            curOpt=${COMP_WORDS[COMP_CWORD - 2]}
104
 
        elif [[ $cur == : ]]; then
105
 
            cur=
106
 
            curOpt="$curOpt:"
107
 
        elif [[ $curOpt == : ]]; then
108
 
            curOpt=${COMP_WORDS[COMP_CWORD - 2]}:
109
 
        fi
110
 
    fi
 
70
        local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
 
71
        local curOpt optEnums
 
72
        local IFS=$' \\n'
 
73
 
 
74
        COMPREPLY=()
 
75
        cur=${COMP_WORDS[COMP_CWORD]}
 
76
 
 
77
        cmds='%(cmds)s'
 
78
        globalOpts=( %(global_options)s )
 
79
 
 
80
        # do ordinary expansion if we are anywhere after a -- argument
 
81
        for ((i = 1; i < COMP_CWORD; ++i)); do
 
82
                [[ ${COMP_WORDS[i]} == "--" ]] && return 0
 
83
        done
 
84
 
 
85
        # find the command; it's the first word not starting in -
 
86
        cmd=
 
87
        for ((cmdIdx = 1; cmdIdx < ${#COMP_WORDS[@]}; ++cmdIdx)); do
 
88
                if [[ ${COMP_WORDS[cmdIdx]} != -* ]]; then
 
89
                        cmd=${COMP_WORDS[cmdIdx]}
 
90
                        break
 
91
                fi
 
92
        done
 
93
 
 
94
        # complete command name if we are not already past the command
 
95
        if [[ $COMP_CWORD -le cmdIdx ]]; then
 
96
                COMPREPLY=( $( compgen -W "$cmds ${globalOpts[*]}" -- $cur ) )
 
97
                return 0
 
98
        fi
 
99
 
 
100
        # find the option for which we want to complete a value
 
101
        curOpt=
 
102
        if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
 
103
                curOpt=${COMP_WORDS[COMP_CWORD - 1]}
 
104
                if [[ $curOpt == = ]]; then
 
105
                        curOpt=${COMP_WORDS[COMP_CWORD - 2]}
 
106
                elif [[ $cur == : ]]; then
 
107
                        cur=
 
108
                        curOpt="$curOpt:"
 
109
                elif [[ $curOpt == : ]]; then
 
110
                        curOpt=${COMP_WORDS[COMP_CWORD - 2]}:
 
111
                fi
 
112
        fi
111
113
%(debug)s
112
 
    cmdOpts=( )
113
 
    optEnums=( )
114
 
    fixedWords=( )
115
 
    case $cmd in
 
114
        cmdOpts=( )
 
115
        optEnums=( )
 
116
        fixedWords=( )
 
117
        case $cmd in
116
118
%(cases)s\
117
 
    *)
118
 
        cmdOpts=(--help -h)
119
 
        ;;
120
 
    esac
121
 
 
122
 
    IFS=$'\\n'
123
 
    if [[ ${#fixedWords[@]} -eq 0 ]] && [[ ${#optEnums[@]} -eq 0 ]] && [[ $cur != -* ]]; then
124
 
        case $curOpt in
125
 
            tag:|*..tag:)
126
 
                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/ /\\\\\\\\ /g;') )
127
 
                ;;
128
 
        esac
129
 
        case $cur in
130
 
            [\\"\\']tag:*)
131
 
                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/^/tag:/') )
132
 
                ;;
133
 
            [\\"\\']*..tag:*)
134
 
                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//') )
135
 
                fixedWords=( $(for i in "${fixedWords[@]}"; do echo "${cur%%..tag:*}..tag:${i}"; done) )
136
 
                ;;
137
 
        esac
138
 
    elif [[ $cur == = ]] && [[ ${#optEnums[@]} -gt 0 ]]; then
139
 
        # complete directly after "--option=", list all enum values
140
 
        COMPREPLY=( "${optEnums[@]}" )
141
 
        return 0
142
 
    else
143
 
        fixedWords=( "${cmdOpts[@]}"
144
 
                     "${globalOpts[@]}"
145
 
                     "${optEnums[@]}"
146
 
                     "${fixedWords[@]}" )
147
 
    fi
148
 
 
149
 
    if [[ ${#fixedWords[@]} -gt 0 ]]; then
150
 
        COMPREPLY=( $( compgen -W "${fixedWords[*]}" -- $cur ) )
151
 
    fi
152
 
 
153
 
    return 0
 
119
        *)
 
120
                cmdOpts=(--help -h)
 
121
                ;;
 
122
        esac
 
123
 
 
124
        IFS=$'\\n'
 
125
        if [[ ${#fixedWords[@]} -eq 0 ]] && [[ ${#optEnums[@]} -eq 0 ]] && [[ $cur != -* ]]; then
 
126
                case $curOpt in
 
127
                        tag:|*..tag:)
 
128
                                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/ /\\\\\\\\ /g;') )
 
129
                                ;;
 
130
                esac
 
131
                case $cur in
 
132
                        [\\"\\']tag:*)
 
133
                                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/^/tag:/') )
 
134
                                ;;
 
135
                        [\\"\\']*..tag:*)
 
136
                                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//') )
 
137
                                fixedWords=( $(for i in "${fixedWords[@]}"; do echo "${cur%%..tag:*}..tag:${i}"; done) )
 
138
                                ;;
 
139
                esac
 
140
        elif [[ $cur == = ]] && [[ ${#optEnums[@]} -gt 0 ]]; then
 
141
                # complete directly after "--option=", list all enum values
 
142
                COMPREPLY=( "${optEnums[@]}" )
 
143
                return 0
 
144
        else
 
145
                fixedWords=( "${cmdOpts[@]}"
 
146
                             "${globalOpts[@]}"
 
147
                             "${optEnums[@]}"
 
148
                             "${fixedWords[@]}" )
 
149
        fi
 
150
 
 
151
        if [[ ${#fixedWords[@]} -gt 0 ]]; then
 
152
                COMPREPLY=( $( compgen -W "${fixedWords[*]}" -- $cur ) )
 
153
        fi
 
154
 
 
155
        return 0
154
156
}
155
 
""" % {
 
157
"""     % {
156
158
            "cmds": self.command_names(),
157
159
            "function_name": self.function_name,
158
160
            "cases": self.command_cases(),
169
171
            return ''
170
172
        else:
171
173
            return (r"""
172
 
    # Debugging code enabled using the --debug command line switch.
173
 
    # Will dump some variables to the top portion of the terminal.
174
 
    echo -ne '\e[s\e[H'
175
 
    for (( i=0; i < ${#COMP_WORDS[@]}; ++i)); do
176
 
        echo "\$COMP_WORDS[$i]='${COMP_WORDS[i]}'"$'\e[K'
177
 
    done
178
 
    for i in COMP_CWORD COMP_LINE COMP_POINT COMP_TYPE COMP_KEY cur curOpt; do
179
 
        echo "\$${i}=\"${!i}\""$'\e[K'
180
 
    done
181
 
    echo -ne '---\e[K\e[u'
 
174
        # Debugging code enabled using the --debug command line switch.
 
175
        # Will dump some variables to the top portion of the terminal.
 
176
        echo -ne '\e[s\e[H'
 
177
        for (( i=0; i < ${#COMP_WORDS[@]}; ++i)); do
 
178
                echo "\$COMP_WORDS[$i]='${COMP_WORDS[i]}'"$'\e[K'
 
179
        done
 
180
        for i in COMP_CWORD COMP_LINE COMP_POINT COMP_TYPE COMP_KEY cur curOpt; do
 
181
                echo "\$${i}=\"${!i}\""$'\e[K'
 
182
        done
 
183
        echo -ne '---\e[K\e[u'
182
184
""")
183
185
 
184
186
    def brz_version(self):
283
285
    def __cmp__(self, other):
284
286
        return cmp(self.name, other.name)
285
287
 
286
 
    def __lt__(self, other):
287
 
        return self.name < other.name
288
 
 
289
288
 
290
289
class DataCollector(object):
291
290
 
298
297
            self.selected_plugins = None
299
298
        else:
300
299
            self.selected_plugins = {x.replace('-', '_')
301
 
                                     for x in selected_plugins}
 
300
                                         for x in selected_plugins}
302
301
 
303
302
    def collect(self):
304
303
        self.global_options()
332
331
        plugin_name = cmd.plugin_name()
333
332
        if plugin_name is not None:
334
333
            if (self.selected_plugins is not None and
335
 
                    plugin not in self.selected_plugins):
 
334
                plugin not in self.selected_plugins):
336
335
                return None
337
336
            plugin_data = self.data.plugins.get(plugin_name)
338
337
            if plugin_data is None:
347
346
        # ones while maintaining the actual command name unchanged.
348
347
        cmd_data.aliases.extend(cmd.aliases)
349
348
        cmd_data.aliases.extend(sorted([useralias
350
 
                                        for cmdalias in cmd_data.aliases
351
 
                                        if cmdalias in self.user_aliases
352
 
                                        for useralias in self.user_aliases[cmdalias]
353
 
                                        if useralias not in cmd_data.aliases]))
 
349
            for cmdalias in cmd_data.aliases
 
350
            if cmdalias in self.user_aliases
 
351
            for useralias in self.user_aliases[cmdalias]
 
352
            if useralias not in cmd_data.aliases]))
354
353
 
355
354
        opts = cmd.options()
356
355
        for optname, opt in sorted(opts.items()):
358
357
 
359
358
        if 'help' == name or 'help' in cmd.aliases:
360
359
            cmd_data.fixed_words = ('($cmds %s)' %
361
 
                                    " ".join(sorted(help_topics.topic_registry.keys())))
 
360
                " ".join(sorted(help_topics.topic_registry.keys())))
362
361
 
363
362
        return cmd_data
364
363
 
365
364
    def option(self, opt):
366
365
        optswitches = {}
367
 
        parser = option.get_optparser([opt])
 
366
        parser = option.get_optparser({opt.name: opt})
368
367
        parser = self.wrap_parser(optswitches, parser)
369
368
        optswitches.clear()
370
369
        opt.add_option(parser, opt.short_name())
389
388
 
390
389
    def wrap_parser(self, optswitches, parser):
391
390
        orig_add_option_group = parser.add_option_group
392
 
 
393
391
        def tweaked_add_option_group(*opts, **attrs):
394
392
            return self.wrap_container(optswitches,
395
 
                                       orig_add_option_group(*opts, **attrs))
 
393
                orig_add_option_group(*opts, **attrs))
396
394
        parser.add_option_group = tweaked_add_option_group
397
395
        return self.wrap_container(optswitches, parser)
398
396
 
400
398
def bash_completion_function(out, function_name="_brz", function_only=False,
401
399
                             debug=False,
402
400
                             no_plugins=False, selected_plugins=None):
403
 
    dc = DataCollector(no_plugins=no_plugins,
404
 
                       selected_plugins=selected_plugins)
 
401
    dc = DataCollector(no_plugins=no_plugins, selected_plugins=selected_plugins)
405
402
    data = dc.collect()
406
403
    cg = BashCodeGen(data, function_name=function_name, debug=debug)
407
404
    if function_only:
417
414
    This command generates a shell function which can be used by bash to
418
415
    automatically complete the currently typed command when the user presses
419
416
    the completion key (usually tab).
420
 
 
 
417
    
421
418
    Commonly used like this:
422
419
        eval "`brz bash-completion`"
423
420
    """
424
421
 
425
422
    takes_options = [
426
423
        option.Option("function-name", short_name="f", type=str, argname="name",
427
 
                      help="Name of the generated function (default: _brz)"),
 
424
               help="Name of the generated function (default: _brz)"),
428
425
        option.Option("function-only", short_name="o", type=None,
429
 
                      help="Generate only the shell function, don't enable it"),
 
426
               help="Generate only the shell function, don't enable it"),
430
427
        option.Option("debug", type=None, hidden=True,
431
 
                      help="Enable shell code useful for debugging"),
 
428
               help="Enable shell code useful for debugging"),
432
429
        option.ListOption("plugin", type=str, argname="name",
433
 
                          # param_name="selected_plugins", # doesn't work, bug #387117
434
 
                          help="Enable completions for the selected plugin"
435
 
                          + " (default: all plugins)"),
 
430
                # param_name="selected_plugins", # doesn't work, bug #387117
 
431
                help="Enable completions for the selected plugin"
 
432
                + " (default: all plugins)"),
436
433
        ]
437
434
 
438
435
    def run(self, **kwargs):