aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-12-06 11:40:17 +1300
committerTom Ryder <tom@sanctum.geek.nz>2018-12-06 11:40:17 +1300
commit131368cd38f99c591e027003aec6fa23bbd834ef (patch)
tree00996a0a036c3c3948f59c2d311d8dfb3e8ee9f6
parent3098a8222d8c2ae8938d3b3ad988f51f03af7d0e (diff)
parent16e97487f24068568e2d31f85f40cc4d2b97a569 (diff)
downloaddotfiles-131368cd38f99c591e027003aec6fa23bbd834ef.tar.gz
dotfiles-131368cd38f99c591e027003aec6fa23bbd834ef.zip
Merge branch 'release/v3.1.0'v3.1.0
* release/v3.1.0: Bump VERSION Refactor bd()/sd() completion a lot
-rw-r--r--VERSION4
-rw-r--r--bash/bash_completion.d/bd.bash50
-rw-r--r--bash/bash_completion.d/sd.bash47
3 files changed, 68 insertions, 33 deletions
diff --git a/VERSION b/VERSION
index 4d194420..63c62745 100644
--- a/VERSION
+++ b/VERSION
@@ -1,2 +1,2 @@
-tejr dotfiles v3.0.1
-Tue Dec 4 02:52:42 UTC 2018
+tejr dotfiles v3.1.0
+Wed Dec 5 22:40:16 UTC 2018
diff --git a/bash/bash_completion.d/bd.bash b/bash/bash_completion.d/bd.bash
index 32969522..f6ca3a6b 100644
--- a/bash/bash_completion.d/bd.bash
+++ b/bash/bash_completion.d/bd.bash
@@ -12,38 +12,52 @@ _bd() {
COMPREPLY[ci++]=$comp
done < <(
- # Build an array of path nodes, leaf to root
+ # Build an array of path ancestors
path=$PWD
while [[ -n $path ]] ; do
- node=${path##*/}
+
+ # Peel off the leaf of the path
+ ancestor=${path##*/}
path=${path%/*}
- [[ -n $node ]] || continue
- nodes[ni++]=$node
+
+ # Skip if this is a null string; root, trailing/double slash...
+ [[ -n $ancestor ]] || continue
+
+ # Skip the first non-null element (current dir)
+ ((generation++)) || continue
+
+ # Push node onto ancestry list
+ ancestors[ai++]=$ancestor
done
- # Continue if we have at least two nodes, counting the leaf
- ((ni > 1)) || return
+ # Continue if we have at least one non-root ancestor
+ ((ai)) || return
- # Shift off the leaf, since it is not meaningful to go "back to" the
- # current directory
- nodes=("${nodes[@]:1}")
+ # Add quoted ancestors to new array; for long paths, this is faster than
+ # forking a subshell for `printf %q` on each item
+ while read -d / -r ancestor ; do
+ ancestors_quoted[aqi++]=$ancestor
+ done < <(printf '%q/' "${ancestors[@]}")
# Make matching behave appropriately
if _completion_ignore_case ; then
shopt -s nocasematch 2>/dev/null
fi
- # Iterate through the nodes and print the ones that match the word
- # being completed, with a trailing slash as terminator
- for node in "${nodes[@]}" ; do
- node_quoted=$(printf '%q' "$node")
- # Check the quoted and unquoted word for matching
- for match in "$node" "$(printf '%q' "$node")" ; do
- # Print any match, slash-terminated
+ # Iterate through keys of the ancestors array
+ for ai in "${!ancestors[@]}" ; do
+
+ # Get ancestor and associated quoted ancestor
+ ancestor=${ancestors[ai]}
+ ancestor_quoted=${ancestors_quoted[ai]}
+
+ # If either the unquoted or quoted ancestor matches, print the
+ # unquoted one as a completion reply
+ for match in "$ancestor" "$ancestor_quoted" ; do
case $match in
("$2"*)
- printf '%s/' "$node"
- continue
+ printf '%s/' "$ancestor"
+ break
;;
esac
done
diff --git a/bash/bash_completion.d/sd.bash b/bash/bash_completion.d/sd.bash
index 4dc72f31..8adc9810 100644
--- a/bash/bash_completion.d/sd.bash
+++ b/bash/bash_completion.d/sd.bash
@@ -14,25 +14,46 @@ _sd() {
# Make globs expand appropriately
shopt -s dotglob nullglob
+
+ # Get list of siblings; use trailing slashes to limit to directories
+ # There should always be at least one (self)
+ siblings=(../*/)
+
+ # Strip leading dot-dot-slash and trailing slash
+ siblings=("${siblings[@]#../}")
+ siblings=("${siblings[@]%/}")
+
+ # Add quoted siblings to new array; for large directories, this is
+ # faster than forking a subshell for `printf %q` on each item
+ while read -d / -r sibling ; do
+ siblings_quoted[sqi++]=$sibling
+ done < <(printf '%q/' "${siblings[@]}")
+
+ # Make matching work appropriately
if _completion_ignore_case ; then
shopt -s nocasematch 2>/dev/null
fi
- # Print matching sibling dirs that are not the current dir
- for sib in ../*/ ; do
- # Strip leading ../
- sib=${sib#../}
- # Strip trailing slash
- sib=${sib%/}
- # Skip self
- [[ $sib != "${PWD##*/}" ]] || continue
- # Check the quoted and unquoted word for matching
- for match in "$sib" "$(printf '%q' "$sib")" ; do
- # Print any match, slash-terminated
+ # Get current dir
+ self=${PWD##*/}
+
+ # Iterate through keys of the siblings array
+ for si in "${!siblings[@]}" ; do
+
+ # Get sibling and associated quoted sibling
+ sibling=${siblings[si]}
+ sibling_quoted=${siblings_quoted[si]}
+
+ # Skip if this sibling looks like the current dir
+ [[ $sibling != "$self" ]] || continue
+
+ # If either the unquoted or quoted sibling matches, print the
+ # unquoted one as a completion reply
+ for match in "$sibling" "$sibling_quoted" ; do
case $match in
("$2"*)
- printf '%s/' "$sib"
- continue
+ printf '%s/' "$sibling"
+ break
;;
esac
done