From fbc5c65ae193a7fd8d294158b36829637b55b27c Mon Sep 17 00:00:00 2001 From: Toni Kangas Date: Thu, 23 Jun 2022 16:59:54 +0300 Subject: [PATCH 1/2] Escape bash completion when there is only one possible completion This ensures completions with whitespace are completed as a single argument. Signed-off-by: Toni Kangas --- bash_completionsV2.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 78f43b91..1b397870 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -216,7 +216,7 @@ __%[1]s_handle_completion_types() { comp=${comp%%%%$tab*} # Only consider the completions that match if [[ $comp == "$cur"* ]]; then - COMPREPLY+=("$comp") + COMPREPLY+=( "$(printf %%q "$comp")" ) fi done < <(printf "%%s\n" "${completions[@]}") ;; @@ -234,6 +234,12 @@ __%[1]s_handle_standard_completion_case() { # Short circuit to optimize if we don't have descriptions if [[ "${completions[*]}" != *$tab* ]]; then IFS=$'\n' read -ra COMPREPLY -d '' < <(compgen -W "${completions[*]}" -- "$cur") + + # If there is a single completion left, escape the completion + if ((${#COMPREPLY[*]} == 1)); then + COMPREPLY[0]=$(printf %%q "${COMPREPLY[0]}") + fi + return 0 fi @@ -252,12 +258,12 @@ __%[1]s_handle_standard_completion_case() { fi done < <(printf "%%s\n" "${completions[@]}") - # If there is a single completion left, remove the description text + # If there is a single completion left, remove the description text and escape the completion if ((${#COMPREPLY[*]} == 1)); then __%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}" comp="${COMPREPLY[0]%%%%$tab*}" __%[1]s_debug "Removed description from single completion, which is now: ${comp}" - COMPREPLY[0]=$comp + COMPREPLY[0]=$(printf %%q "${comp}") else # Format the descriptions __%[1]s_format_comp_descriptions $longest fi From 3f2dad7403b97a26a368848db9ad93e2fd13562e Mon Sep 17 00:00:00 2001 From: Toni Kangas Date: Thu, 23 Jun 2022 23:46:49 +0300 Subject: [PATCH 2/2] Use line-break as field separator in compgen words input This ensures that completions with whitespace are treated as single completion. Signed-off-by: Toni Kangas --- bash_completionsV2.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 1b397870..91a9186e 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -233,7 +233,8 @@ __%[1]s_handle_standard_completion_case() { # Short circuit to optimize if we don't have descriptions if [[ "${completions[*]}" != *$tab* ]]; then - IFS=$'\n' read -ra COMPREPLY -d '' < <(compgen -W "${completions[*]}" -- "$cur") + local compgen_words=$(printf "%%s\n" "${completions[@]}") + IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n' compgen -W "${compgen_words}" -- "$cur") # If there is a single completion left, escape the completion if ((${#COMPREPLY[*]} == 1)); then