diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-30 14:43:33 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-30 14:51:41 +1200 |
commit | 95c3c2daf1e63f02a37db0e003f01da788acfb7d (patch) | |
tree | 8d477a9d48f58b8100b34892478bdce619db4ee4 | |
parent | Note broken mysql() (diff) | |
download | dotfiles-95c3c2daf1e63f02a37db0e003f01da788acfb7d.tar.gz dotfiles-95c3c2daf1e63f02a37db0e003f01da788acfb7d.zip |
Handle empty filename completions better
Bash 4.4 hangs in an awkward way (probably outputting the literal null
char in some unexpected context) without this; I'm not sure if this is a
bug or whether it's just been tolerated behaviour until now.
-rw-r--r-- | bash/bash_completion.d/bd.bash | 2 | ||||
-rw-r--r-- | bash/bash_completion.d/eds.bash | 12 | ||||
-rw-r--r-- | bash/bash_completion.d/man.bash | 15 | ||||
-rw-r--r-- | bash/bash_completion.d/mysql.bash | 14 | ||||
-rw-r--r-- | bash/bash_completion.d/pass.bash | 14 | ||||
-rw-r--r-- | bash/bash_completion.d/path.bash | 14 | ||||
-rw-r--r-- | bash/bash_completion.d/sd.bash | 14 |
7 files changed, 56 insertions, 29 deletions
diff --git a/bash/bash_completion.d/bd.bash b/bash/bash_completion.d/bd.bash index 4b3d4a94..86146a4e 100644 --- a/bash/bash_completion.d/bd.bash +++ b/bash/bash_completion.d/bd.bash @@ -16,7 +16,7 @@ _bd() { local dirname for dirname in "${dirnames[@]}" ; do [[ $dirname == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue - COMPREPLY=("${COMPREPLY[@]}" "$(printf %q "$dirname")") + COMPREPLY[${#COMPREPLY[@]}]=$(printf %q "$dirname") done } complete -F _bd bd diff --git a/bash/bash_completion.d/eds.bash b/bash/bash_completion.d/eds.bash index 0395fd4c..3829e157 100644 --- a/bash/bash_completion.d/eds.bash +++ b/bash/bash_completion.d/eds.bash @@ -5,6 +5,7 @@ _eds() { edspath=${EDSPATH:-"$HOME"/.local/bin} [[ -d $edspath ]] || return while IFS= read -rd '' executable ; do + [[ -n $executable ]] || continue COMPREPLY[${#COMPREPLY[@]}]=$executable done < <( shopt -s dotglob nullglob @@ -15,8 +16,15 @@ _eds() { [[ -f $file && -x $file ]] || continue executables[${#executables[@]}]=${file##*/} done - ((${#executables[@]})) || exit 1 - printf '%q\0' "${executables[@]}" + + # Print quoted entries, null-delimited, if there was at least one; + # otherwise, just print a null character to stop this hanging in Bash + # 4.4 + if ((${#executables[@]})) ; then + printf '%q\0' "${executables[@]}" + else + printf '\0' + fi ) } complete -F _eds eds diff --git a/bash/bash_completion.d/man.bash b/bash/bash_completion.d/man.bash index 7456f69b..9861e9dd 100644 --- a/bash/bash_completion.d/man.bash +++ b/bash/bash_completion.d/man.bash @@ -19,6 +19,7 @@ _man() { # Read completion results from a subshell and add them to the COMPREPLY # array individually while IFS= read -rd '' page ; do + [[ -n $page ]] || continue COMPREPLY[${#COMPREPLY[@]}]=$page done < <( @@ -51,12 +52,14 @@ _man() { pages=("${pages[@]%.@([glx]z|bz2|lzma|Z)}") pages=("${pages[@]%.[0-9]*}") - # Bail out if we ended up with no pages somehow to prevent us from - # printing - ((${#pages[@]})) || exit 1 - - # Print the pages array to stdout, quoted and null-delimited - printf '%q\0' "${pages[@]}" + # Print quoted entries, null-delimited, if there was at least one; + # otherwise, just print a null character to stop this hanging in Bash + # 4.4 + if ((${#pages[@]})) ; then + printf '%q\0' "${pages[@]}" + else + printf '\0' + fi ) } complete -F _man -o default man diff --git a/bash/bash_completion.d/mysql.bash b/bash/bash_completion.d/mysql.bash index f64b6f32..9755f033 100644 --- a/bash/bash_completion.d/mysql.bash +++ b/bash/bash_completion.d/mysql.bash @@ -12,6 +12,7 @@ _mysql() { # Return the names of the .cnf files sans prefix as completions local db while IFS= read -rd '' db ; do + [[ -n $db ]] || continue COMPREPLY[${#COMPREPLY[@]}]=$db done < <( @@ -24,11 +25,14 @@ _mysql() { cnfs=("${cnfs[@]#"$dirname"/}") cnfs=("${cnfs[@]%.cnf}") - # Bail if no files to prevent empty output - ((${#cnfs[@]})) || exit 1 - - # Print the conf names, null-delimited - printf '%q\0' "${cnfs[@]}" + # Print quoted entries, null-delimited, if there was at least one; + # otherwise, just print a null character to stop this hanging in Bash + # 4.4 + if ((${#cnfs[@]})) ; then + printf '%q\0' "${cnfs[@]}" + else + printf '\0' + fi ) } complete -F _mysql -o default mysql diff --git a/bash/bash_completion.d/pass.bash b/bash/bash_completion.d/pass.bash index 28941952..feff78ae 100644 --- a/bash/bash_completion.d/pass.bash +++ b/bash/bash_completion.d/pass.bash @@ -15,6 +15,7 @@ _pass() # doesn't seem to do this properly with a null delimiter) local entry while IFS= read -rd '' entry ; do + [[ -n $entry ]] || continue COMPREPLY[${#COMPREPLY[@]}]=$entry done < <( @@ -29,11 +30,14 @@ _pass() entries=("${entries[@]#"$passdir"/}") entries=("${entries[@]%.gpg}") - # Bail if no entries to prevent empty output - ((${#entries[@]})) || exit 1 - - # Print all the entries, null-delimited - printf '%q\0' "${entries[@]}" + # Print quoted entries, null-delimited, if there was at least one; + # otherwise, just print a null character to stop this hanging in Bash + # 4.4 + if ((${#entries[@]})) ; then + printf '%q\0' "${entries[@]}" + else + printf '\0' + fi ) } complete -F _pass pass diff --git a/bash/bash_completion.d/path.bash b/bash/bash_completion.d/path.bash index 60b36844..e0e7732d 100644 --- a/bash/bash_completion.d/path.bash +++ b/bash/bash_completion.d/path.bash @@ -19,6 +19,7 @@ _path() { insert|append|check) local dirname while IFS= read -rd '' dirname ; do + [[ -n $dirname ]] || continue COMPREPLY[${#COMPREPLY[@]}]=$dirname done < <( @@ -30,11 +31,14 @@ _path() { dirnames=("${COMP_WORDS[COMP_CWORD]}"*/) dirnames=("${dirnames[@]%/}") - # Bail if no results to prevent empty output - ((${#dirnames[@]})) || exit 1 - - # Print results, quoted and null-delimited - printf '%q\0' "${dirnames[@]}" + # Print quoted entries, null-delimited, if there was at + # least one; otherwise, just print a null character to stop + # this hanging in Bash 4.4 + if ((${#dirnames[@]})) ; then + printf '%q\0' "${dirnames[@]}" + else + printf '\0' + fi ) ;; diff --git a/bash/bash_completion.d/sd.bash b/bash/bash_completion.d/sd.bash index f8017591..0c59f544 100644 --- a/bash/bash_completion.d/sd.bash +++ b/bash/bash_completion.d/sd.bash @@ -9,6 +9,7 @@ _sd() { # Build list of matching sibiling directories while IFS= read -rd '' dirname ; do + [[ -n $dirname ]] || continue COMPREPLY[${#COMPREPLY[@]}]=$dirname done < <( @@ -29,11 +30,14 @@ _sd() { sibs[${#sibs[@]}]=$dirname done - # Bail if no results to prevent empty output - ((${#sibs[@]})) || exit 1 - - # Print results, null-delimited - printf '%q\0' "${sibs[@]}" + # Print quoted sibs, null-delimited, if there was at least one; + # otherwise, just print a null character to stop this hanging in Bash + # 4.4 + if ((${#sibs[@]})) ; then + printf '%q\0' "${sibs[@]}" + else + printf '\0' + fi ) } complete -F _sd sd |