From 0681de59778f82c9006de70ed9a9ee6644fd4a18 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 1 Dec 2018 02:29:42 +1300 Subject: Adjust loop short circuits and pattern matches --- bash/bash_completion.d/bd.bash | 7 +++++-- bash/bash_completion.d/eds.bash | 4 +++- bash/bash_completion.d/git.bash | 5 +++-- bash/bash_completion.d/gpg.bash | 11 ++++++++--- bash/bash_completion.d/make.bash | 7 ++++--- bash/bash_completion.d/man.bash | 10 +++++++--- bash/bash_completion.d/mex.bash | 3 ++- bash/bash_completion.d/path.bash | 7 +++++-- bash/bash_completion.d/sd.bash | 10 +++++++--- bash/bash_completion.d/ud.bash | 1 + 10 files changed, 45 insertions(+), 20 deletions(-) diff --git a/bash/bash_completion.d/bd.bash b/bash/bash_completion.d/bd.bash index 1c1a40a8..120080c3 100644 --- a/bash/bash_completion.d/bd.bash +++ b/bash/bash_completion.d/bd.bash @@ -15,8 +15,11 @@ _bd() { # Add the matching dirnames to the reply local dirname for dirname in "${dirnames[@]}" ; do - [[ $dirname == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue - COMPREPLY[${#COMPREPLY[@]}]=$(printf %q "$dirname") + case $dirname in + "${COMP_WORDS[COMP_CWORD]}"*) + COMPREPLY[${#COMPREPLY[@]}]=$(printf %q "$dirname") + ;; + esac done } complete -F _bd bd diff --git a/bash/bash_completion.d/eds.bash b/bash/bash_completion.d/eds.bash index 0b5e34ea..1c5b2aa2 100644 --- a/bash/bash_completion.d/eds.bash +++ b/bash/bash_completion.d/eds.bash @@ -25,7 +25,9 @@ _eds() { files=("${EDSPATH:-"$HOME"/.local/bin}"/"${COMP_WORDS[COMP_CWORD]}"*) declare -a executables for file in "${files[@]}" ; do - [[ -f $file && -x $file ]] || continue + ! [[ -d $file ]] || continue + [[ -e $file ]] || continue + [[ -x $file ]] || continue executables[${#executables[@]}]=${file##*/} done diff --git a/bash/bash_completion.d/git.bash b/bash/bash_completion.d/git.bash index 28313f76..a2edb468 100644 --- a/bash/bash_completion.d/git.bash +++ b/bash/bash_completion.d/git.bash @@ -12,9 +12,9 @@ _git() { refs) local ref while IFS= read -r ref ; do - [[ -n $ref ]] || continue ref=${ref#refs/*/} case $ref in + '') continue ;; "${COMP_WORDS[COMP_CWORD]}"*) COMPREPLY[${#COMPREPLY[@]}]=$ref ;; @@ -59,7 +59,8 @@ _git() { execpath=$(git --exec-path) || return local path for path in "$execpath"/git-"${COMP_WORDS[COMP_CWORD]}"* ; do - [[ -f $path ]] || continue + ! [[ -d $path ]] || continue + [[ -e $path ]] || continue [[ -x $path ]] || continue COMPREPLY[${#COMPREPLY[@]}]=${path#"$execpath"/git-} done diff --git a/bash/bash_completion.d/gpg.bash b/bash/bash_completion.d/gpg.bash index 2855a1a9..0f6785f9 100644 --- a/bash/bash_completion.d/gpg.bash +++ b/bash/bash_completion.d/gpg.bash @@ -5,13 +5,18 @@ _gpg() { hash gpg 2>/dev/null || return 1 # Bail if not completing an option - [[ ${COMP_WORDS[COMP_CWORD]} == --* ]] || return 1 + case ${COMP_WORDS[COMP_CWORD]} in + --*) return 1 ;; + esac # Generate completion reply from gpg(1) options local option while read -r option ; do - [[ $option == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue - COMPREPLY[${#COMPREPLY[@]}]=$option + case $option in + "${COMP_WORDS[COMP_CWORD]}"*) + COMPREPLY[${#COMPREPLY[@]}]=$option + ;; + esac done < <(gpg --dump-options 2>/dev/null) } complete -F _gpg -o bashdefault -o default gpg diff --git a/bash/bash_completion.d/make.bash b/bash/bash_completion.d/make.bash index c36a039a..0f39ef4b 100644 --- a/bash/bash_completion.d/make.bash +++ b/bash/bash_completion.d/make.bash @@ -5,9 +5,10 @@ _make() { # first, then "Makefile"). You may want to add "GNU-makefile" after this. local mf for mf in makefile Makefile '' ; do - [[ -f $mf ]] && break + [[ -e $mf ]] || continue + break done - [[ -n $mf ]] || return 1 + [[ -n $mf ]] || return # Iterate through the Makefile, line by line local line @@ -39,7 +40,7 @@ _make() { *[^[:word:]./-]*) ;; # Add targets that match what we're completing - ${COMP_WORDS[COMP_CWORD]}*) + "${COMP_WORDS[COMP_CWORD]}"*) COMPREPLY[${#COMPREPLY[@]}]=$target ;; esac diff --git a/bash/bash_completion.d/man.bash b/bash/bash_completion.d/man.bash index fcf26436..7574db40 100644 --- a/bash/bash_completion.d/man.bash +++ b/bash/bash_completion.d/man.bash @@ -17,9 +17,13 @@ _man() { # If this is the second word, and the previous word started with a number, # we'll assume that's the section to search local section subdir - if ((COMP_CWORD > 1)) && [[ ${COMP_WORDS[COMP_CWORD-1]} == [0-9]* ]] ; then - section=${COMP_WORDS[COMP_CWORD-1]} - subdir=man${section%%[^0-9]*} + if ((COMP_CWORD > 1)) ; then + case ${COMP_WORDS[COMP_CWORD-1]} in + [0-9]*) + section=${COMP_WORDS[COMP_CWORD-1]} + subdir=man${section%%[^0-9]*} + ;; + esac fi # Read completion results from a subshell and add them to the COMPREPLY diff --git a/bash/bash_completion.d/mex.bash b/bash/bash_completion.d/mex.bash index d25f1824..bc3d2c7b 100644 --- a/bash/bash_completion.d/mex.bash +++ b/bash/bash_completion.d/mex.bash @@ -6,7 +6,8 @@ _mex() { for dir in "${path[@]}" ; do [[ -d $dir ]] || continue for name in "$dir"/* ; do - [[ -f $name ]] || continue + [[ -e $name ]] || continue + ! [[ -d $name ]] || continue ! [[ -x $name ]] || continue COMPREPLY[${#COMPREPLY[@]}]=${name##*/} done diff --git a/bash/bash_completion.d/path.bash b/bash/bash_completion.d/path.bash index fcb57949..9aab8488 100644 --- a/bash/bash_completion.d/path.bash +++ b/bash/bash_completion.d/path.bash @@ -52,8 +52,11 @@ _path() { IFS=: read -rd '' -a promptarr < <(printf '%s\0' "$PATH") local part for part in "${promptarr[@]}" ; do - [[ $part == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue - COMPREPLY[${#COMPREPLY[@]}]=$(printf '%q' "$part") + case $part in + "${COMP_WORDS[COMP_CWORD]}"*) + COMPREPLY[${#COMPREPLY[@]}]=$(printf '%q' "$part") + ;; + esac done ;; diff --git a/bash/bash_completion.d/sd.bash b/bash/bash_completion.d/sd.bash index 2d2d8f22..1194b33a 100644 --- a/bash/bash_completion.d/sd.bash +++ b/bash/bash_completion.d/sd.bash @@ -5,7 +5,9 @@ _sd() { ((COMP_CWORD == 1)) || return 1 # Current directory can't be root directory - [[ $PWD != / ]] || return 1 + case $PWD in + /) return 1 ;; + esac # Build list of matching sibling directories local dirname @@ -37,8 +39,10 @@ _sd() { local -a sibs local dirname for dirname in "${dirnames[@]}" ; do - [[ $dirname != "${PWD##*/}" ]] || continue - sibs[${#sibs[@]}]=$dirname + case $dirname in + "${PWD##*/}") ;; + *) sibs[${#sibs[@]}]=$dirname ;; + esac done # Print quoted sibling directories, null-delimited diff --git a/bash/bash_completion.d/ud.bash b/bash/bash_completion.d/ud.bash index 278fbbc3..9ded41e6 100644 --- a/bash/bash_completion.d/ud.bash +++ b/bash/bash_completion.d/ud.bash @@ -7,6 +7,7 @@ _ud() { # Iterate through directories, null-separated, add them to completions local dirname while IFS= read -rd '' dirname ; do + [[ -n "$dirname" ]] || continue COMPREPLY[${#COMPREPLY[@]}]=$dirname done < <( -- cgit v1.2.3