From: Zbigniew Jędrzejewski-Szmek Date: Sun, 21 Jul 2024 09:21:49 +0000 (+0200) Subject: completions: apply suggestions from shellcheck X-Git-Tag: v24~7^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a92f5c98f3fc5542a5ac4302b4ae0b4fb1a8d000;p=thirdparty%2Fmkosi.git completions: apply suggestions from shellcheck readarray is used to create arrays. The one clear advantage is that we don't need to override $IFS. Together with the change to not assign an unused variable, this removes shellcheck warnings. Nevertheless, shellcheck would still warn about the file because it doesn't know about the variables that are in the part that is generated dynamically. Also, move more content to the static resource file. The order of declarations doesn't matter, so it's fine if the variables are defined below the functions. Also, adjust the formatting in the bash resource to follow the usual style with 'if something; then' on one line. --- diff --git a/mkosi/completion.py b/mkosi/completion.py index dec09c690..ed1df98c4 100644 --- a/mkosi/completion.py +++ b/mkosi/completion.py @@ -115,7 +115,8 @@ def finalize_completion_bash(options: list[CompletionItem], resources: Path) -> options_by_key = {o.short: o for o in options if o.short} | {o.long: o for o in options if o.long} with io.StringIO() as c: - c.write("# SPDX-License-Identifier: LGPL-2.1-or-later\n\n") + c.write(completion.read_text()) + c.write(to_bash_array("_mkosi_options", options_by_key.keys())) c.write("\n\n") @@ -139,8 +140,6 @@ def finalize_completion_bash(options: list[CompletionItem], resources: Path) -> c.write(to_bash_array("_mkosi_verbs", [str(v) for v in config.Verb])) c.write("\n\n\n") - c.write(completion.read_text()) - return c.getvalue() @@ -184,15 +183,12 @@ def finalize_completion_zsh(options: list[CompletionItem], resources: Path) -> s completion = resources / "completion.zsh" with io.StringIO() as c: - c.write("#compdef mkosi\n") - c.write("# SPDX-License-Identifier: LGPL-2.1-or-later\n\n") + c.write(completion.read_text()) + c.write("\n") c.write(to_zsh_array("_mkosi_verbs", [str(v) for v in config.Verb])) c.write("\n\n") - c.write(completion.read_text()) - c.write("\n") - c.write("_arguments -s \\\n") c.write(" '(- *)'{-h,--help}'[Show this help]' \\\n") c.write(" '(- *)--version[Show package version]' \\\n") diff --git a/mkosi/resources/completion.bash b/mkosi/resources/completion.bash index c665752a6..82026d962 100644 --- a/mkosi/resources/completion.bash +++ b/mkosi/resources/completion.bash @@ -1,3 +1,6 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# shellcheck shell=bash + _mkosi_compgen_files() { compgen -f -- "$1" } @@ -7,13 +10,13 @@ _mkosi_compgen_dirs() { } _mkosi_completion() { - local completing_program="$1" + # completing_program="$1" local completing_word="$2" local completing_word_preceding="$3" if [[ "$completing_word" =~ ^- ]] # completing an option then - COMPREPLY=( $(compgen -W "${_mkosi_options[*]}" -- "${completing_word}") ) + readarray -t COMPREPLY < <(compgen -W "${_mkosi_options[*]}" -- "${completing_word}") elif [[ "$completing_word_preceding" =~ ^- ]] # the previous word was an option then @@ -24,20 +27,20 @@ _mkosi_completion() { if [[ -n "${current_option_compgen}" ]] then - local IFS=$'\n' - COMPREPLY=( $("${current_option_compgen}" "${completing_word}") ) - unset IFS + readarray -t COMPREPLY < <("${current_option_compgen}" "${completing_word}") fi - COMPREPLY+=( $(compgen -W "${current_option_choices}" -- "${completing_word}") ) + readarray -t COMPREPLY -O "${#COMPREPLY[@]}" \ + < <(compgen -W "${current_option_choices}" -- "${completing_word}") if [[ "${current_option_nargs}" == "?" ]] then - COMPREPLY+=( $(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}") ) + readarray -t COMPREPLY -O "${#COMPREPLY[@]}" \ + < <(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}") fi else # the preceding word wasn't an option, so we are doing position # arguments now and all of them are verbs - COMPREPLY=( $(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}") ) + readarray -t COMPREPLY < <(compgen -W "${_mkosi_verbs[*]}" -- "${completing_word}") fi } diff --git a/mkosi/resources/completion.zsh b/mkosi/resources/completion.zsh index e9cd3b58e..b1687d5ae 100644 --- a/mkosi/resources/completion.zsh +++ b/mkosi/resources/completion.zsh @@ -1,3 +1,7 @@ +#compdef mkosi +# SPDX-License-Identifier: LGPL-2.1-or-later +# shellcheck shell=zsh + _mkosi_verb(){ if (( CURRENT == 1 )); then _describe -t commands 'mkosi verb' _mkosi_verbs