Skip to content

Commit

Permalink
review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wismill committed Oct 4, 2023
1 parent adebb5a commit eb8f500
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
26 changes: 12 additions & 14 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
)
23 changes: 11 additions & 12 deletions tools/xkbcli-completion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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]}"
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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")
Expand Down

0 comments on commit eb8f500

Please sign in to comment.