From 48a6fe57798091a1f530e5a0c80ddf5f2356eada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 17 May 2022 23:26:41 +0300 Subject: [PATCH 1/6] style(bash-v2): use arithmetic evaluation in numeric context --- bash_completionsV2.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 351391af3..4a300dea0 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -88,12 +88,12 @@ __%[1]s_process_completion_results() { local shellCompDirectiveFilterFileExt=%[6]d local shellCompDirectiveFilterDirs=%[7]d - if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then + if (((directive & shellCompDirectiveError) != 0)); then # Error code. No completion. __%[1]s_debug "Received error from custom completion go code" return else - if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then + if (((directive & shellCompDirectiveNoSpace) != 0)); then if [[ $(type -t compopt) = "builtin" ]]; then __%[1]s_debug "Activating no space" compopt -o nospace @@ -101,7 +101,7 @@ __%[1]s_process_completion_results() { __%[1]s_debug "No space directive not supported in this version of bash" fi fi - if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then + if (((directive & shellCompDirectiveNoFileComp) != 0)); then if [[ $(type -t compopt) = "builtin" ]]; then __%[1]s_debug "Activating no file completion" compopt +o default @@ -116,7 +116,7 @@ __%[1]s_process_completion_results() { local activeHelp=() __%[1]s_extract_activeHelp - if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then + if (((directive & shellCompDirectiveFilterFileExt) != 0)); then # File extension filtering local fullFilter filter filteringCmd @@ -129,7 +129,7 @@ __%[1]s_process_completion_results() { filteringCmd="_filedir $fullFilter" __%[1]s_debug "File filtering command: $filteringCmd" $filteringCmd - elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then + elif (((directive & shellCompDirectiveFilterDirs) != 0)); then # File completion for directories only # Use printf to strip any trailing newline @@ -150,7 +150,7 @@ __%[1]s_process_completion_results() { __%[1]s_handle_special_char "$cur" = # Print the activeHelp statements before we finish - if [ ${#activeHelp[*]} -ne 0 ]; then + if ((${#activeHelp[*]} != 0)); then printf "\n"; printf "%%s\n" "${activeHelp[@]}" printf "\n" @@ -240,7 +240,7 @@ __%[1]s_handle_standard_completion_case() { done < <(printf "%%s\n" "${completions[@]}") # If there is a single completion left, remove the description text - if [ ${#COMPREPLY[*]} -eq 1 ]; then + 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}" @@ -257,7 +257,7 @@ __%[1]s_handle_special_char() if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then local word=${comp%%"${comp##*${char}}"} local idx=${#COMPREPLY[*]} - while [[ $((--idx)) -ge 0 ]]; do + while ((--idx >= 0)); do COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"} done fi @@ -284,7 +284,7 @@ __%[1]s_format_comp_descriptions() # Make sure we can fit a description of at least 8 characters # if we are to align the descriptions. - if [[ $maxdesclength -gt 8 ]]; then + if ((maxdesclength > 8)); then # Add the proper number of spaces to align the descriptions for ((i = ${#comp} ; i < longest ; i++)); do comp+=" " @@ -296,8 +296,8 @@ __%[1]s_format_comp_descriptions() # If there is enough space for any description text, # truncate the descriptions that are too long for the shell width - if [ $maxdesclength -gt 0 ]; then - if [ ${#desc} -gt $maxdesclength ]; then + if ((maxdesclength > 0)); then + if ((${#desc} > maxdesclength)); then desc=${desc:0:$(( maxdesclength - 1 ))} desc+="…" fi From 10da1a8996d6855667cad84fe7243150f5d54797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 17 May 2022 23:28:02 +0300 Subject: [PATCH 2/6] style(bash-v2): remove unnecessary $ from array index variables --- bash_completionsV2.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 4a300dea0..72c1ae7ae 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -258,7 +258,7 @@ __%[1]s_handle_special_char() local word=${comp%%"${comp##*${char}}"} local idx=${#COMPREPLY[*]} while ((--idx >= 0)); do - COMPREPLY[$idx]=${COMPREPLY[$idx]#"$word"} + COMPREPLY[idx]=${COMPREPLY[idx]#"$word"} done fi } From 933caf637f33c2f9b9a55c41ffafb51c6af8a90b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 17 May 2022 23:31:04 +0300 Subject: [PATCH 3/6] style(bash-v2): [[ ]] over [ ], == over =, remove unnecessary quoting --- bash_completionsV2.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 72c1ae7ae..70c0da518 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -51,7 +51,7 @@ __%[1]s_get_completion_results() { lastChar=${lastParam:$((${#lastParam}-1)):1} __%[1]s_debug "lastParam ${lastParam}, lastChar ${lastChar}" - if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then + if [[ -z ${cur} && ${lastChar} != = ]]; then # If the last parameter is complete (there is a space following it) # We add an extra empty parameter so we can indicate this to the go method. __%[1]s_debug "Adding extra empty parameter" @@ -61,7 +61,7 @@ __%[1]s_get_completion_results() { # When completing a flag with an = (e.g., %[1]s -n=) # bash focuses on the part after the =, so we need to remove # the flag part from $cur - if [[ "${cur}" == -*=* ]]; then + if [[ ${cur} == -*=* ]]; then cur="${cur#*=}" fi @@ -73,7 +73,7 @@ __%[1]s_get_completion_results() { directive=${out##*:} # Remove the directive out=${out%%:*} - if [ "${directive}" = "${out}" ]; then + if [[ ${directive} == "${out}" ]]; then # There is not directive specified directive=0 fi @@ -94,7 +94,7 @@ __%[1]s_process_completion_results() { return else if (((directive & shellCompDirectiveNoSpace) != 0)); then - if [[ $(type -t compopt) = "builtin" ]]; then + if [[ $(type -t compopt) == builtin ]]; then __%[1]s_debug "Activating no space" compopt -o nospace else @@ -102,7 +102,7 @@ __%[1]s_process_completion_results() { fi fi if (((directive & shellCompDirectiveNoFileComp) != 0)); then - if [[ $(type -t compopt) = "builtin" ]]; then + if [[ $(type -t compopt) == builtin ]]; then __%[1]s_debug "Activating no file completion" compopt +o default else @@ -135,7 +135,7 @@ __%[1]s_process_completion_results() { # Use printf to strip any trailing newline local subdir subdir=$(printf "%%s" "${completions[0]}") - if [ -n "$subdir" ]; then + if [[ -n $subdir ]]; then __%[1]s_debug "Listing directories in $subdir" pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return else @@ -174,10 +174,10 @@ __%[1]s_extract_activeHelp() { local endIndex=${#activeHelpMarker} while IFS='' read -r comp; do - if [ "${comp:0:endIndex}" = "$activeHelpMarker" ]; then + if [[ ${comp:0:endIndex} == $activeHelpMarker ]]; then comp=${comp:endIndex} __%[1]s_debug "ActiveHelp found: $comp" - if [ -n "$comp" ]; then + if [[ -n $comp ]]; then activeHelp+=("$comp") fi else @@ -318,9 +318,9 @@ __start_%[1]s() # Call _init_completion from the bash-completion package # to prepare the arguments properly if declare -F _init_completion >/dev/null 2>&1; then - _init_completion -n "=:" || return + _init_completion -n =: || return else - __%[1]s_init_completion -n "=:" || return + __%[1]s_init_completion -n =: || return fi __%[1]s_debug From 966d0a8b85be81637c148713ef40c1a4194409ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 17 May 2022 23:32:38 +0300 Subject: [PATCH 4/6] style(bash-v2): use ${foo-} rather than ${foo:-} in emptiness check The result of the expansion is null no matter if the variable is unset or null in both cases; the former form is arguably easier on the eye. --- bash_completionsV2.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 70c0da518..87a906673 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -24,7 +24,7 @@ func genBashComp(buf io.StringWriter, name string, includeDesc bool) { __%[1]s_debug() { - if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then + if [[ -n ${BASH_COMP_DEBUG_FILE-} ]]; then echo "$*" >> "${BASH_COMP_DEBUG_FILE}" fi } From aa6017008a58092d2cae894d2dc07d7f1d804ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 17 May 2022 23:38:49 +0300 Subject: [PATCH 5/6] style(bash-v2): remove unnecessary trailing linefeed removal No longer needed as of f464d6c82e9af74b7a46301a775163984af32cd1, saves a subshell. --- bash_completionsV2.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 87a906673..429aaf71b 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -132,9 +132,8 @@ __%[1]s_process_completion_results() { elif (((directive & shellCompDirectiveFilterDirs) != 0)); then # File completion for directories only - # Use printf to strip any trailing newline local subdir - subdir=$(printf "%%s" "${completions[0]}") + subdir=${completions[0]} if [[ -n $subdir ]]; then __%[1]s_debug "Listing directories in $subdir" pushd "$subdir" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return From ef504a698b4b8e55e298682618a3bc9c507522dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 18 Jun 2022 22:51:31 +0200 Subject: [PATCH 6/6] style(bash-v2): use herestring in activehelp extraction Herestrings read cleaner than process substitutions, and work in posix mode (but we do and will have some process substitutions so this doesn't matter much). Both approaches may end up using temporary files. --- bash_completionsV2.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 429aaf71b..dad2f0eb0 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -183,7 +183,7 @@ __%[1]s_extract_activeHelp() { # Not an activeHelp line but a normal completion completions+=("$comp") fi - done < <(printf "%%s\n" "${out}") + done <<<"${out}" } __%[1]s_handle_completion_types() {