From eb8f5007692307bd37fcef5e012aef59f28b4e27 Mon Sep 17 00:00:00 2001 From: Pierre Le Marre Date: Wed, 4 Oct 2023 08:53:33 +0200 Subject: [PATCH] review fixes --- meson.build | 26 ++++++++++++-------------- meson_options.txt | 8 +++++++- tools/xkbcli-completion.sh | 23 +++++++++++------------ 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/meson.build b/meson.build index 0d67dee5d..452b4ed34 100644 --- a/meson.build +++ b/meson.build @@ -438,21 +438,19 @@ if build_tools dependencies: tools_dep, install: true) install_man('tools/xkbcli.1') - bash_completion_path = get_option('bash-completion-path') - if bash_completion_path == '' - bash_completion = dependency('bash-completion', required : false) - if bash_completion.found() - bash_completion_path = bash_completion.get_variable(pkgconfig : 'completionsdir') - else - bash_completion_path = get_option('datadir') / 'bash-completion/completions' + if get_option('enable-bash-completion') + bash_completion_path = get_option('bash-completion-path') + if bash_completion_path == '' + bash_completion = dependency('bash-completion', required: false) + if bash_completion.found() + bash_completion_path = bash_completion.get_variable(pkgconfig: 'completionsdir') + else + bash_completion_path = get_option('datadir') / 'bash-completion/completions' + endif endif - endif - if bash_completion_path != 'no' - install_data( - 'tools/xkbcli-completion.sh', - rename: 'xkbcli', - install_dir: bash_completion_path - ) + install_data('tools/xkbcli-completion.sh', + rename: 'xkbcli', + install_dir: bash_completion_path) endif # Tool: compile-keymap diff --git a/meson_options.txt b/meson_options.txt index f64e4e9f2..1a408ad3e 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -16,7 +16,7 @@ option( option( 'bash-completion-path', type: 'string', - description: 'directory for bash completion scripts ["no" disables]' + description: 'directory for bash completion scripts' ) option( 'default-rules', @@ -84,3 +84,9 @@ option( value: true, description: 'Enable building libxkbregistry', ) +option( + 'enable-bash-completion', + type: 'boolean', + value: true, + description: 'Enable installing bash completion scripts', +) diff --git a/tools/xkbcli-completion.sh b/tools/xkbcli-completion.sh index fa58359ba..dea6e1a98 100755 --- a/tools/xkbcli-completion.sh +++ b/tools/xkbcli-completion.sh @@ -8,12 +8,11 @@ ___xkbcli_main() { # Initialization: https://github.com/scop/bash-completion/blob/fdf4456186eb4548ef628e65fb1be73d8e4695e9/bash_completion.d/000_bash_completion_compat.bash#L205 - local cur prev words cword + local cur prev words cword cmd _init_completion -s || return - local i=1 cmd - # Find subcommand + local i=1 while [[ "$i" -lt "$COMP_CWORD" ]] do local s="${COMP_WORDS[i]}" @@ -28,9 +27,10 @@ ___xkbcli_main() done # Parse available subcommands - local help_output line is_command_list=false subcommands=() - help_output=$(xkbcli --help) - while IFS= read -r line; do + local line + local is_command_list=false + local subcommands=() + while IFS='' read -r line; do # Traverse subcommand list if [[ "$is_command_list" == true ]]; then # Check for subcommand based on the indentation @@ -44,11 +44,11 @@ ___xkbcli_main() elif [[ "$line" == "Commands:" ]]; then is_command_list=true fi - done <<< "$help_output" + # NOTE: <( COMMAND ) Bash construct is process substitution. + done < <(xkbcli --help) # No previous subcommand or incomplete: completion for root xkbcli command - if [[ "$i" -eq "$COMP_CWORD" ]] - then + if [[ "$i" -eq "$COMP_CWORD" ]]; then local opts # Doc for _parse_help: https://github.com/scop/bash-completion/blob/fdf4456186eb4548ef628e65fb1be73d8e4695e9/bash_completion.d/000_bash_completion_compat.bash#L311 opts=$(_parse_help xkbcli) @@ -58,8 +58,7 @@ ___xkbcli_main() fi # Found a supported subcommand: proceed to completion - if [[ " ${subcommands[*]} " =~ " $cmd " ]] - then + if [[ " ${subcommands[*]} " =~ " $cmd " ]]; then ___xkbcli_subcommand "$cmd" fi } @@ -85,7 +84,7 @@ ___xkbcli_subcommand() # Parse help to get command options local opts - # Doc for _parse_usage & _parse_help: + # Doc for _parse_usage and _parse_help: # • https://github.com/scop/bash-completion/blob/fdf4456186eb4548ef628e65fb1be73d8e4695e9/bash_completion.d/000_bash_completion_compat.bash#L335 # • https://github.com/scop/bash-completion/blob/fdf4456186eb4548ef628e65fb1be73d8e4695e9/bash_completion.d/000_bash_completion_compat.bash#L311 opts=$(_parse_usage xkbcli "$1 --help")