/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

Merge test-run support.

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
#
60
60
shopt -s progcomp
61
61
%(function)s
62
62
complete -F %(function_name)s -o default brz
63
 
""" % {
 
63
"""     % {
64
64
            "function_name": self.function_name,
65
65
            "function": self.function(),
66
66
            "brz_version": self.brz_version(),
70
70
        return ("""\
71
71
%(function_name)s ()
72
72
{
73
 
    local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
74
 
    local curOpt optEnums
75
 
    local IFS=$' \\n'
76
 
 
77
 
    COMPREPLY=()
78
 
    cur=${COMP_WORDS[COMP_CWORD]}
79
 
 
80
 
    cmds='%(cmds)s'
81
 
    globalOpts=( %(global_options)s )
82
 
 
83
 
    # do ordinary expansion if we are anywhere after a -- argument
84
 
    for ((i = 1; i < COMP_CWORD; ++i)); do
85
 
        [[ ${COMP_WORDS[i]} == "--" ]] && return 0
86
 
    done
87
 
 
88
 
    # find the command; it's the first word not starting in -
89
 
    cmd=
90
 
    for ((cmdIdx = 1; cmdIdx < ${#COMP_WORDS[@]}; ++cmdIdx)); do
91
 
        if [[ ${COMP_WORDS[cmdIdx]} != -* ]]; then
92
 
            cmd=${COMP_WORDS[cmdIdx]}
93
 
            break
94
 
        fi
95
 
    done
96
 
 
97
 
    # complete command name if we are not already past the command
98
 
    if [[ $COMP_CWORD -le cmdIdx ]]; then
99
 
        COMPREPLY=( $( compgen -W "$cmds ${globalOpts[*]}" -- $cur ) )
100
 
        return 0
101
 
    fi
102
 
 
103
 
    # find the option for which we want to complete a value
104
 
    curOpt=
105
 
    if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
106
 
        curOpt=${COMP_WORDS[COMP_CWORD - 1]}
107
 
        if [[ $curOpt == = ]]; then
108
 
            curOpt=${COMP_WORDS[COMP_CWORD - 2]}
109
 
        elif [[ $cur == : ]]; then
110
 
            cur=
111
 
            curOpt="$curOpt:"
112
 
        elif [[ $curOpt == : ]]; then
113
 
            curOpt=${COMP_WORDS[COMP_CWORD - 2]}:
114
 
        fi
115
 
    fi
 
73
        local cur cmds cmdIdx cmd cmdOpts fixedWords i globalOpts
 
74
        local curOpt optEnums
 
75
        local IFS=$' \\n'
 
76
 
 
77
        COMPREPLY=()
 
78
        cur=${COMP_WORDS[COMP_CWORD]}
 
79
 
 
80
        cmds='%(cmds)s'
 
81
        globalOpts=( %(global_options)s )
 
82
 
 
83
        # do ordinary expansion if we are anywhere after a -- argument
 
84
        for ((i = 1; i < COMP_CWORD; ++i)); do
 
85
                [[ ${COMP_WORDS[i]} == "--" ]] && return 0
 
86
        done
 
87
 
 
88
        # find the command; it's the first word not starting in -
 
89
        cmd=
 
90
        for ((cmdIdx = 1; cmdIdx < ${#COMP_WORDS[@]}; ++cmdIdx)); do
 
91
                if [[ ${COMP_WORDS[cmdIdx]} != -* ]]; then
 
92
                        cmd=${COMP_WORDS[cmdIdx]}
 
93
                        break
 
94
                fi
 
95
        done
 
96
 
 
97
        # complete command name if we are not already past the command
 
98
        if [[ $COMP_CWORD -le cmdIdx ]]; then
 
99
                COMPREPLY=( $( compgen -W "$cmds ${globalOpts[*]}" -- $cur ) )
 
100
                return 0
 
101
        fi
 
102
 
 
103
        # find the option for which we want to complete a value
 
104
        curOpt=
 
105
        if [[ $cur != -* ]] && [[ $COMP_CWORD -gt 1 ]]; then
 
106
                curOpt=${COMP_WORDS[COMP_CWORD - 1]}
 
107
                if [[ $curOpt == = ]]; then
 
108
                        curOpt=${COMP_WORDS[COMP_CWORD - 2]}
 
109
                elif [[ $cur == : ]]; then
 
110
                        cur=
 
111
                        curOpt="$curOpt:"
 
112
                elif [[ $curOpt == : ]]; then
 
113
                        curOpt=${COMP_WORDS[COMP_CWORD - 2]}:
 
114
                fi
 
115
        fi
116
116
%(debug)s
117
 
    cmdOpts=( )
118
 
    optEnums=( )
119
 
    fixedWords=( )
120
 
    case $cmd in
 
117
        cmdOpts=( )
 
118
        optEnums=( )
 
119
        fixedWords=( )
 
120
        case $cmd in
121
121
%(cases)s\
122
 
    *)
123
 
        cmdOpts=(--help -h)
124
 
        ;;
125
 
    esac
126
 
 
127
 
    IFS=$'\\n'
128
 
    if [[ ${#fixedWords[@]} -eq 0 ]] && [[ ${#optEnums[@]} -eq 0 ]] && [[ $cur != -* ]]; then
129
 
        case $curOpt in
130
 
            tag:|*..tag:)
131
 
                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/ /\\\\\\\\ /g;') )
132
 
                ;;
133
 
        esac
134
 
        case $cur in
135
 
            [\\"\\']tag:*)
136
 
                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/^/tag:/') )
137
 
                ;;
138
 
            [\\"\\']*..tag:*)
139
 
                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//') )
140
 
                fixedWords=( $(for i in "${fixedWords[@]}"; do echo "${cur%%..tag:*}..tag:${i}"; done) )
141
 
                ;;
142
 
        esac
143
 
    elif [[ $cur == = ]] && [[ ${#optEnums[@]} -gt 0 ]]; then
144
 
        # complete directly after "--option=", list all enum values
145
 
        COMPREPLY=( "${optEnums[@]}" )
146
 
        return 0
147
 
    else
148
 
        fixedWords=( "${cmdOpts[@]}"
149
 
                     "${globalOpts[@]}"
150
 
                     "${optEnums[@]}"
151
 
                     "${fixedWords[@]}" )
152
 
    fi
153
 
 
154
 
    if [[ ${#fixedWords[@]} -gt 0 ]]; then
155
 
        COMPREPLY=( $( compgen -W "${fixedWords[*]}" -- $cur ) )
156
 
    fi
157
 
 
158
 
    return 0
 
122
        *)
 
123
                cmdOpts=(--help -h)
 
124
                ;;
 
125
        esac
 
126
 
 
127
        IFS=$'\\n'
 
128
        if [[ ${#fixedWords[@]} -eq 0 ]] && [[ ${#optEnums[@]} -eq 0 ]] && [[ $cur != -* ]]; then
 
129
                case $curOpt in
 
130
                        tag:|*..tag:)
 
131
                                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/ /\\\\\\\\ /g;') )
 
132
                                ;;
 
133
                esac
 
134
                case $cur in
 
135
                        [\\"\\']tag:*)
 
136
                                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//; s/^/tag:/') )
 
137
                                ;;
 
138
                        [\\"\\']*..tag:*)
 
139
                                fixedWords=( $(brz tags 2>/dev/null | sed 's/  *[^ ]*$//') )
 
140
                                fixedWords=( $(for i in "${fixedWords[@]}"; do echo "${cur%%..tag:*}..tag:${i}"; done) )
 
141
                                ;;
 
142
                esac
 
143
        elif [[ $cur == = ]] && [[ ${#optEnums[@]} -gt 0 ]]; then
 
144
                # complete directly after "--option=", list all enum values
 
145
                COMPREPLY=( "${optEnums[@]}" )
 
146
                return 0
 
147
        else
 
148
                fixedWords=( "${cmdOpts[@]}"
 
149
                             "${globalOpts[@]}"
 
150
                             "${optEnums[@]}"
 
151
                             "${fixedWords[@]}" )
 
152
        fi
 
153
 
 
154
        if [[ ${#fixedWords[@]} -gt 0 ]]; then
 
155
                COMPREPLY=( $( compgen -W "${fixedWords[*]}" -- $cur ) )
 
156
        fi
 
157
 
 
158
        return 0
159
159
}
160
 
""" % {
 
160
"""     % {
161
161
            "cmds": self.command_names(),
162
162
            "function_name": self.function_name,
163
163
            "cases": self.command_cases(),
174
174
            return ''
175
175
        else:
176
176
            return (r"""
177
 
    # Debugging code enabled using the --debug command line switch.
178
 
    # Will dump some variables to the top portion of the terminal.
179
 
    echo -ne '\e[s\e[H'
180
 
    for (( i=0; i < ${#COMP_WORDS[@]}; ++i)); do
181
 
        echo "\$COMP_WORDS[$i]='${COMP_WORDS[i]}'"$'\e[K'
182
 
    done
183
 
    for i in COMP_CWORD COMP_LINE COMP_POINT COMP_TYPE COMP_KEY cur curOpt; do
184
 
        echo "\$${i}=\"${!i}\""$'\e[K'
185
 
    done
186
 
    echo -ne '---\e[K\e[u'
 
177
        # Debugging code enabled using the --debug command line switch.
 
178
        # Will dump some variables to the top portion of the terminal.
 
179
        echo -ne '\e[s\e[H'
 
180
        for (( i=0; i < ${#COMP_WORDS[@]}; ++i)); do
 
181
                echo "\$COMP_WORDS[$i]='${COMP_WORDS[i]}'"$'\e[K'
 
182
        done
 
183
        for i in COMP_CWORD COMP_LINE COMP_POINT COMP_TYPE COMP_KEY cur curOpt; do
 
184
                echo "\$${i}=\"${!i}\""$'\e[K'
 
185
        done
 
186
        echo -ne '---\e[K\e[u'
187
187
""")
188
188
 
189
189
    def brz_version(self):
288
288
    def __cmp__(self, other):
289
289
        return cmp(self.name, other.name)
290
290
 
291
 
    def __lt__(self, other):
292
 
        return self.name < other.name
293
 
 
294
291
 
295
292
class DataCollector(object):
296
293
 
303
300
            self.selected_plugins = None
304
301
        else:
305
302
            self.selected_plugins = {x.replace('-', '_')
306
 
                                     for x in selected_plugins}
 
303
                                         for x in selected_plugins}
307
304
 
308
305
    def collect(self):
309
306
        self.global_options()
337
334
        plugin_name = cmd.plugin_name()
338
335
        if plugin_name is not None:
339
336
            if (self.selected_plugins is not None and
340
 
                    plugin not in self.selected_plugins):
 
337
                plugin not in self.selected_plugins):
341
338
                return None
342
339
            plugin_data = self.data.plugins.get(plugin_name)
343
340
            if plugin_data is None:
352
349
        # ones while maintaining the actual command name unchanged.
353
350
        cmd_data.aliases.extend(cmd.aliases)
354
351
        cmd_data.aliases.extend(sorted([useralias
355
 
                                        for cmdalias in cmd_data.aliases
356
 
                                        if cmdalias in self.user_aliases
357
 
                                        for useralias in self.user_aliases[cmdalias]
358
 
                                        if useralias not in cmd_data.aliases]))
 
352
            for cmdalias in cmd_data.aliases
 
353
            if cmdalias in self.user_aliases
 
354
            for useralias in self.user_aliases[cmdalias]
 
355
            if useralias not in cmd_data.aliases]))
359
356
 
360
357
        opts = cmd.options()
361
358
        for optname, opt in sorted(opts.items()):
363
360
 
364
361
        if 'help' == name or 'help' in cmd.aliases:
365
362
            cmd_data.fixed_words = ('($cmds %s)' %
366
 
                                    " ".join(sorted(help_topics.topic_registry.keys())))
 
363
                " ".join(sorted(help_topics.topic_registry.keys())))
367
364
 
368
365
        return cmd_data
369
366
 
370
367
    def option(self, opt):
371
368
        optswitches = {}
372
 
        parser = option.get_optparser([opt])
 
369
        parser = option.get_optparser({opt.name: opt})
373
370
        parser = self.wrap_parser(optswitches, parser)
374
371
        optswitches.clear()
375
372
        opt.add_option(parser, opt.short_name())
394
391
 
395
392
    def wrap_parser(self, optswitches, parser):
396
393
        orig_add_option_group = parser.add_option_group
397
 
 
398
394
        def tweaked_add_option_group(*opts, **attrs):
399
395
            return self.wrap_container(optswitches,
400
 
                                       orig_add_option_group(*opts, **attrs))
 
396
                orig_add_option_group(*opts, **attrs))
401
397
        parser.add_option_group = tweaked_add_option_group
402
398
        return self.wrap_container(optswitches, parser)
403
399
 
405
401
def bash_completion_function(out, function_name="_brz", function_only=False,
406
402
                             debug=False,
407
403
                             no_plugins=False, selected_plugins=None):
408
 
    dc = DataCollector(no_plugins=no_plugins,
409
 
                       selected_plugins=selected_plugins)
 
404
    dc = DataCollector(no_plugins=no_plugins, selected_plugins=selected_plugins)
410
405
    data = dc.collect()
411
406
    cg = BashCodeGen(data, function_name=function_name, debug=debug)
412
407
    if function_only:
422
417
    This command generates a shell function which can be used by bash to
423
418
    automatically complete the currently typed command when the user presses
424
419
    the completion key (usually tab).
425
 
 
 
420
    
426
421
    Commonly used like this:
427
422
        eval "`brz bash-completion`"
428
423
    """
429
424
 
430
425
    takes_options = [
431
426
        option.Option("function-name", short_name="f", type=text_type, argname="name",
432
 
                      help="Name of the generated function (default: _brz)"),
 
427
               help="Name of the generated function (default: _brz)"),
433
428
        option.Option("function-only", short_name="o", type=None,
434
 
                      help="Generate only the shell function, don't enable it"),
 
429
               help="Generate only the shell function, don't enable it"),
435
430
        option.Option("debug", type=None, hidden=True,
436
 
                      help="Enable shell code useful for debugging"),
 
431
               help="Enable shell code useful for debugging"),
437
432
        option.ListOption("plugin", type=text_type, argname="name",
438
 
                          # param_name="selected_plugins", # doesn't work, bug #387117
439
 
                          help="Enable completions for the selected plugin"
440
 
                          + " (default: all plugins)"),
 
433
                # param_name="selected_plugins", # doesn't work, bug #387117
 
434
                help="Enable completions for the selected plugin"
 
435
                + " (default: all plugins)"),
441
436
        ]
442
437
 
443
438
    def run(self, **kwargs):