Skip to content

Commit

Permalink
completion: fix bash completion script
Browse files Browse the repository at this point in the history
Template the options definitions directly into the completion function, since
for some weird scoping reasons even though the script is read fine and when
running a shell with set -x one can see e.g. _mkosi_options being assigned the
proper values, the completion function still uses '' for
"${_mkosi_options[*]}".

This wasn't caught during development because the script works fine when
sourced.
  • Loading branch information
behrmann committed Jul 27, 2024
1 parent a876e0a commit 3de52b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
10 changes: 5 additions & 5 deletions mkosi/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shlex
from collections.abc import Iterable, Mapping
from pathlib import Path
from textwrap import indent
from typing import Optional, Union

from mkosi import config
Expand Down Expand Up @@ -114,10 +115,8 @@ def to_bash_hasharray(name: str, entries: Mapping[str, Union[str, int]]) -> str:

options_by_key = {o.short: o for o in options if o.short} | {o.long: o for o in options if o.long}

template = completion.read_text()
with io.StringIO() as c:
c.write(completion.read_text())
c.write("\n")

c.write(to_bash_array("_mkosi_options", options_by_key.keys()))
c.write("\n\n")

Expand All @@ -139,9 +138,10 @@ def to_bash_hasharray(name: str, entries: Mapping[str, Union[str, int]]) -> str:
c.write("\n\n")

c.write(to_bash_array("_mkosi_verbs", [str(v) for v in config.Verb]))
c.write("\n\n\n")

return c.getvalue()
definitions = c.getvalue()

return template.replace("##VARIABLEDEFINITIONS##", indent(definitions, " " * 4))


def finalize_completion_fish(options: list[CompletionItem], resources: Path) -> str:
Expand Down
11 changes: 5 additions & 6 deletions mkosi/resources/completion.bash
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# shellcheck shell=bash

declare -a _mkosi_options
declare -A _mkosi_nargs
declare -A _mkosi_choices
declare -A _mkosi_compgen
declare -a _mkosi_verbs

_mkosi_compgen_files() {
compgen -f -- "$1"
}
Expand All @@ -16,6 +10,11 @@ _mkosi_compgen_dirs() {
}

_mkosi_completion() {
local -a _mkosi_options _mkosi_verbs
local -A _mkosi_nargs _mkosi_choices _mkosi_compgen

##VARIABLEDEFINITIONS##

# completing_program="$1"
local completing_word="$2"
local completing_word_preceding="$3"
Expand Down

0 comments on commit 3de52b3

Please sign in to comment.