File: bash_complete.py

package info (click to toggle)
crazy-complete 0.3.7-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 13,342; sh: 995; makefile: 68
file content (460 lines) | stat: -rw-r--r-- 15,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2025-2026 Benjamin Abendroth <braph93@gmx.de>

'''This module contains code for completing arguments in Bash.'''

from . import shell
from . import bash_versions
from .str_utils import indent
from .type_utils import is_dict_type
from .bash_utils import make_file_extension_pattern


class BashCompletionBase:
    '''Base class for Bash completions.'''

    def get_code(self, append=False):
        '''Returns the code used for completing an argument.

        If `append` is `True`, then the results will be appended to $COMPREPLY,
        otherwise $COMPREPLY will be truncated.

        Args:
            append (bool): If True, the results will be appended to $COMPREPLY.

        Returns:
            str: The command for Bash completion.
        '''
        raise NotImplementedError

    def get_function(self):
        '''Returns a function.'''
        raise NotImplementedError


class BashCompletionCode(BashCompletionBase):
    '''Used for completion code that internally modify $COMPREPLY.'''

    def __init__(self, ctxt, code):
        self.ctxt = ctxt
        self.code = code

    def get_code(self, append=False):
        if not self.code:
            return ''

        r = []

        if append:
            r.append('local COMPREPLY_OLD=("${COMPREPLY[@]}")')

        r.append(self.code)

        if append:
            r.append('COMPREPLY=("${COMPREPLY_OLD[@]}" "${COMPREPLY[@]}")')

        return '\n'.join(r)

    def get_function(self):
        if not self.code:
            return 'true'

        return self.ctxt.helpers.add_dynamic_func(self.ctxt, self.get_code())


class BashCompletionFunc(BashCompletionBase):
    '''Used for completion functions that internally modify $COMPREPLY.'''

    def __init__(self, ctxt, args):
        self.ctxt = ctxt
        self.args = args

    def get_code(self, append=False):
        r = []

        if append:
            r.append('local COMPREPLY_OLD=("${COMPREPLY[@]}")')

        r.append(shell.join_quoted(self.args))

        if append:
            r.append('COMPREPLY=("${COMPREPLY_OLD[@]}" "${COMPREPLY[@]}")')

        return '\n'.join(r)

    def get_function(self):
        if len(self.args) == 1:
            return self.args[0]

        return self.ctxt.helpers.add_dynamic_func(self.ctxt, self.get_code())


class CompgenW(BashCompletionBase):
    '''Used for completing a list of words.'''

    def __init__(self, ctxt, values):
        self.ctxt = ctxt
        self.values = [str(value) for value in values]

    def get_code(self, append=False):
        needs_quote = any(filter(shell.needs_quote, self.values))

        if not needs_quote:
            return 'COMPREPLY%s=($(compgen -W %s -- "$cur"))' % (
                ('+' if append else ''),
                shell.quote(' '.join(self.values)))

        return ('%s %s-- %s' % (
            self.ctxt.helpers.use_function('values'),
            ('-a ' if append else ''),
            shell.join_quoted(self.values)))

    def get_function(self):
        return self.ctxt.helpers.add_dynamic_func(self.ctxt, self.get_code())


class BashCompletionCompgen(BashCompletionBase):
    '''Used for completion that uses Bash's `compgen` command.'''

    def __init__(self, ctxt, compgen_args, code=None):
        self.ctxt = ctxt
        self.compgen_args = compgen_args
        self.code = code

    def get_code(self, append=False):
        compgen_cmd = 'COMPREPLY%s=($(compgen %s -- "$cur"))' % (
            ('+' if append else ''), self.compgen_args)

        if not self.code:
            return compgen_cmd

        return f'{self.code}\n{compgen_cmd}'

    def get_function(self):
        return self.ctxt.helpers.add_dynamic_func(self.ctxt, self.get_code())


class BashCompleteCombine(BashCompletionBase):
    '''Used for combining multiple completion commands.'''

    def __init__(self, ctxt, trace, completer, commands):
        self.ctxt = ctxt
        self.completion_objects = []

        for command_args in commands:
            obj = completer.complete_from_def(ctxt, trace, command_args)
            self.completion_objects.append(obj)

    def get_code(self, append=False):
        code = [self.completion_objects[0].get_code(append=append)]
        for obj in self.completion_objects[1:]:
            code.append(obj.get_code(append=True))
        return '\n'.join(code)

    def get_function(self):
        return self.ctxt.helpers.add_dynamic_func(self.ctxt, self.get_code())


class BashCompleteKeyValueList(BashCompletionCode):
    '''Used for completing a list of key-value pairs.'''

    # pylint: disable=too-many-arguments
    # pylint: disable=too-many-positional-arguments

    def __init__(self, ctxt, trace, completer,
                 pair_separator, value_separator, values):
        funcs = {}

        for key, _desc, complete in values:
            if not complete:
                funcs[key] = 'false'
            elif complete[0] == 'none':
                funcs[key] = 'true'
            else:
                obj = completer.complete_from_def(ctxt, trace, complete)
                funcs[key] = obj.get_function()

        q = shell.quote
        args = ' \\\n'.join('%s %s' % (q(k), q(f)) for k, f in funcs.items())

        code = '%s %s %s \\\n%s' % (
            ctxt.helpers.use_function('key_value_list'),
            shell.quote(pair_separator),
            shell.quote(value_separator),
            indent(args, 2)
        )

        super().__init__(ctxt, code)


class BashCompleter(shell.ShellCompleter):
    '''Code generator for completing arguments in Bash.'''

    # pylint: disable=missing-function-docstring
    # pylint: disable=too-many-public-methods
    # pylint: disable=too-many-arguments
    # pylint: disable=too-many-positional-arguments

    def none(self, ctxt, _trace, *_):
        return BashCompletionCode(ctxt, '')

    def integer(self, ctxt, _trace, options):
        suffixes = []

        if options:
            if 'suffixes' in options:
                for suffix, description in options['suffixes'].items():
                    suffixes.append(f'{suffix}:{description}')

        if not suffixes:
            return BashCompletionCode(ctxt, '')

        func = ctxt.helpers.use_function('number')
        return BashCompletionFunc(ctxt, [func, *suffixes])

    def float(self, ctxt, trace, options=None):
        return self.integer(ctxt, trace, options)

    def choices(self, ctxt, _trace, choices):
        return CompgenW(ctxt, choices)

    def command(self, ctxt, _trace, opts=None):
        code = None
        path = None
        append = None
        prepend = None

        if opts:
            path = opts.get('path', None)
            append = opts.get('path_append', None)
            prepend = opts.get('path_prepend', None)

        if path:
            code = 'local -x PATH=%s' % shell.quote(path)
        elif append and prepend:
            append = shell.quote(append)
            prepend = shell.quote(prepend)
            code = 'local -x PATH=%s:"$PATH":%s' % (prepend, append)
        elif append:
            code = 'local -x PATH="$PATH":%s' % shell.quote(append)
        elif prepend:
            code = 'local -x PATH=%s:"$PATH"' % shell.quote(prepend)

        return BashCompletionCompgen(ctxt, '-A command', code=code)

    def directory(self, ctxt, _trace, opts=None):
        directory = None if opts is None else opts.get('directory', None)
        filedir = bash_versions.filedir(ctxt)

        if directory:
            r = 'builtin pushd %s &>/dev/null && {\n' % shell.quote(directory)
            r += '  %s -d\n' % filedir
            r += '  builtin popd >/dev/null\n'
            r += '}'
            return BashCompletionCode(ctxt, r)

        return BashCompletionFunc(ctxt, [filedir, '-d'])

    def file(self, ctxt, _trace, opts=None):
        fuzzy = False
        directory = None
        extensions = None
        ignore_globs = None

        if opts:
            fuzzy = opts.get('fuzzy', False)
            directory = opts.get('directory', None)
            extensions = opts.get('extensions', None)
            ignore_globs = opts.get('ignore_globs', None)

        args = [bash_versions.filedir(ctxt)]

        if extensions:
            args.append(make_file_extension_pattern(extensions, fuzzy))

        if not directory and not ignore_globs:
            return BashCompletionFunc(ctxt, args)

        if directory:
            r = 'builtin pushd %s &>/dev/null && {\n' % shell.quote(directory)
            r += '  %s\n' % shell.join_quoted(args)
            r += '  builtin popd >/dev/null\n'
            r += '}'
        else:
            r = shell.join_quoted(args)

        if ignore_globs:
            func = ctxt.helpers.use_function('file_filter')
            r += '\n%s %s' % (func, shell.join_quoted(ignore_globs))

        return BashCompletionCode(ctxt, r)

    def mime_file(self, ctxt, _trace, pattern):
        func = ctxt.helpers.use_function('mime_file')
        return BashCompletionFunc(ctxt, [func, pattern])

    def group(self, ctxt, _trace):
        return BashCompletionCompgen(ctxt, '-A group')

    def hostname(self, ctxt, _trace):
        return BashCompletionCompgen(ctxt, '-A hostname')

    def pid(self, ctxt, _trace):
        return BashCompletionFunc(ctxt, [bash_versions.pids(ctxt)])

    def process(self, ctxt, _trace):
        return BashCompletionFunc(ctxt, [bash_versions.pnames(ctxt)])

    def range(self, ctxt, _trace, start, stop, step=1):
        if step == 1:
            return BashCompletionCompgen(ctxt, f"-W '{{{start}..{stop}}}'")

        return BashCompletionCompgen(ctxt, f"-W '{{{start}..{stop}..{step}}}'")

    def service(self, ctxt, _trace):
        return BashCompletionCompgen(ctxt, '-A service')

    def user(self, ctxt, _trace):
        return BashCompletionCompgen(ctxt, '-A user')

    def variable(self, ctxt, _trace):
        return BashCompletionCompgen(ctxt, '-A variable')

    def environment(self, ctxt, _trace):
        return BashCompletionCompgen(ctxt, '-A export')

    def exec(self, ctxt, _trace, command):
        func = ctxt.helpers.use_function('exec')
        return BashCompletionFunc(ctxt, [func, command])

    def exec_fast(self, ctxt, _trace, command):
        func = ctxt.helpers.use_function('exec_fast')
        return BashCompletionFunc(ctxt, [func, command])

    def exec_internal(self, ctxt, _trace, command):
        return BashCompletionCode(ctxt, command)

    def value_list(self, ctxt, _trace, opts):
        separator = opts.get('separator', ',')
        duplicates = opts.get('duplicates', False)
        values = opts['values']

        if is_dict_type(values):
            values = list(values.keys())

        args = [ctxt.helpers.use_function('value_list')]
        if duplicates:
            ctxt.helpers.use_function('value_list', 'duplicates')
            args.append('-d')
        args.append(separator)

        return BashCompletionFunc(ctxt, args + values)

    def key_value_list(self, ctxt, trace, pair_separator, value_separator, values):
        return BashCompleteKeyValueList(ctxt, trace, self, pair_separator, value_separator, values)

    def combine(self, ctxt, trace, commands):
        return BashCompleteCombine(ctxt, trace, self, commands)

    def list(self, ctxt, trace, command, opts=None):
        separator = opts.get('separator', ',') if opts else ','
        duplicates = opts.get('duplicates', False) if opts else False

        obj = self.complete_from_def(ctxt, trace, command)
        code = obj.get_code()

        func = ctxt.helpers.use_function('value_list')

        dup_arg = ''
        if duplicates:
            ctxt.helpers.use_function('value_list', 'duplicates')
            dup_arg = ' -d'

        r = 'local cur_old="$cur"\n'
        r += 'cur="${cur##*%s}"\n' % shell.quote(separator)
        r += '%s\n' % code
        r += 'cur="$cur_old"\n'
        r += '%s%s %s "${COMPREPLY[@]}"' % (func, dup_arg, shell.quote(separator))
        return BashCompletionCode(ctxt, r)

    def history(self, ctxt, _trace, pattern):
        func = ctxt.helpers.use_function('history')
        return BashCompletionFunc(ctxt, [func, pattern])

    def commandline_string(self, ctxt, _trace):
        func = ctxt.helpers.use_function('commandline_string')
        return BashCompletionFunc(ctxt, [func])

    def command_arg(self, ctxt, _trace):
        return BashCompletionCode(ctxt, '')

    def date(self, ctxt, _trace, _format):
        return BashCompletionCode(ctxt, '')

    def date_format(self, ctxt, _trace):
        return BashCompletionCode(ctxt, '')

    def uid(self, ctxt, _trace):
        return BashCompletionFunc(ctxt, [bash_versions.uids(ctxt)])

    def gid(self, ctxt, _trace):
        return BashCompletionFunc(ctxt, [bash_versions.gids(ctxt)])

    def signal(self, ctxt, _trace):
        return BashCompletionFunc(ctxt, [bash_versions.signals(ctxt)])

    def filesystem_type(self, ctxt, _trace):
        return BashCompletionFunc(ctxt, [bash_versions.fstypes(ctxt)])

    def prefix(self, ctxt, trace, prefix, command):
        obj = self.complete_from_def(ctxt, trace, command)
        func = obj.get_function()
        prefix_func = ctxt.helpers.use_function('prefix')
        return BashCompletionFunc(ctxt, [prefix_func, prefix, func])

    def ip_address(self, ctxt, _trace, type_='all'):
        func = bash_versions.ip_addresses(ctxt)

        if type_ == 'ipv6':
            return BashCompletionFunc(ctxt, [func, '-6'])

        if type_ == 'ipv4':
            return BashCompletionFunc(ctxt, [func])

        return BashCompletionFunc(ctxt, [func, '-a'])

    # =========================================================================
    # Bonus
    # =========================================================================

    def mountpoint(self, ctxt, _trace):
        func = ctxt.helpers.use_function('mountpoint')
        return BashCompletionFunc(ctxt, [func])

    def locale(self, ctxt, trace):
        func = ctxt.helpers.use_function('locales')
        return BashCompletionFunc(ctxt, [func])

    def charset(self, ctxt, trace):
        func = ctxt.helpers.use_function('charsets')
        return BashCompletionFunc(ctxt, [func])

    def login_shell(self, ctxt, _trace):
        return BashCompletionFunc(ctxt, [bash_versions.shells(ctxt)])

    def net_interface(self, ctxt, _trace):
        func = bash_versions.available_interfaces(ctxt)
        return BashCompletionFunc(ctxt, [func])

    def timezone(self, ctxt, _trace):
        exec_func = ctxt.helpers.use_function('exec')
        list_func = ctxt.helpers.use_function('timezone_list')
        return BashCompletionFunc(ctxt, [exec_func, list_func])

    def alsa_card(self, ctxt, _trace):
        func = ctxt.helpers.use_function('alsa_list_cards')
        return BashCompletionFunc(ctxt, [func])

    def alsa_device(self, ctxt, _trace):
        func = ctxt.helpers.use_function('alsa_list_devices')
        return BashCompletionFunc(ctxt, [func])