| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Module for positionals completion in Bash.''' |
| 5 |
|
| 6 |
from . import algo |
| 7 |
from . import bash_when |
| 8 |
from .str_utils import indent |
| 9 |
|
| 10 |
|
| 11 |
def _make_block(code): |
| 12 |
if not code: |
| 13 |
return '{\n return 0;\n}' |
| 14 |
|
| 15 |
return '{\n%s\n return 0;\n}' % indent(code, 2) |
| 16 |
|
| 17 |
|
| 18 |
def _is_not_command_arg(positional): |
| 19 |
return ( |
| 20 |
not positional.complete or |
| 21 |
positional.complete[0] != 'command_arg' |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
def _generate_subcommand_positional(generator): |
| 26 |
cmds = generator.subcommands.get_choices().keys() |
| 27 |
complete = generator.completer.choices(generator.ctxt, [], cmds).get_code() |
| 28 |
return '(( ${#POSITIONALS[@]} == %d )) && %s' % ( |
| 29 |
generator.subcommands.get_positional_num(), |
| 30 |
_make_block(complete)) |
| 31 |
|
| 32 |
|
| 33 |
def _get_positional_condition(positional): |
| 34 |
op = '==' |
| 35 |
if positional.repeatable: |
| 36 |
op = '>=' |
| 37 |
return '${#POSITIONALS[@]} %s %s' % (op, positional.get_positional_num()) |
| 38 |
|
| 39 |
|
| 40 |
def _generate_positionals_with_when(generator): |
| 41 |
code = [] |
| 42 |
|
| 43 |
positionals = filter(_is_not_command_arg, generator.positionals) |
| 44 |
|
| 45 |
for positional in positionals: |
| 46 |
condition = _get_positional_condition(positional) |
| 47 |
r = '(( %s )) && ' % condition |
| 48 |
if positional.when: |
| 49 |
r += '%s && ' % bash_when.generate_when_conditions( |
| 50 |
generator.ctxt, |
| 51 |
generator.commandline, |
| 52 |
generator.variable_manager, |
| 53 |
positional.when) |
| 54 |
r += '%s' % _make_block(generator.complete_option(positional)) |
| 55 |
|
| 56 |
code.append(r) |
| 57 |
|
| 58 |
return '\n\n'.join(code) |
| 59 |
|
| 60 |
|
| 61 |
def _generate_positionals_without_when(generator): |
| 62 |
code = [] |
| 63 |
|
| 64 |
positionals = filter(_is_not_command_arg, generator.positionals) |
| 65 |
|
| 66 |
grouped_by_complete = algo.group_by( |
| 67 |
positionals, generator.complete_option) |
| 68 |
|
| 69 |
for complete, positionals in grouped_by_complete.items(): |
| 70 |
conditions = [_get_positional_condition(p) for p in positionals] |
| 71 |
r = '(( %s )) && ' % ' || '.join(conditions) |
| 72 |
r += '%s' % _make_block(complete) |
| 73 |
code.append(r) |
| 74 |
|
| 75 |
return '\n\n'.join(code) |
| 76 |
|
| 77 |
|
| 78 |
def generate(generator): |
| 79 |
'''Generate code for completing positionals.''' |
| 80 |
|
| 81 |
code = [] |
| 82 |
|
| 83 |
has_when = any(p.when for p in generator.positionals) |
| 84 |
|
| 85 |
if has_when: |
| 86 |
code += [_generate_positionals_with_when(generator)] |
| 87 |
else: |
| 88 |
code += [_generate_positionals_without_when(generator)] |
| 89 |
|
| 90 |
if generator.subcommands: |
| 91 |
code += [_generate_subcommand_positional(generator)] |
| 92 |
|
| 93 |
return '\n\n'.join(c for c in code if c) |