[PATCH bash ordered autocomplete] Order the autocompletion in bash

jerabaul29 jean.rblt at gmail.com
Sun Mar 29 13:54:27 CEST 2020


Order the autocompletion in bash so that first
all commands, then all options, then all dirs,
then all files are shown. Each of the subsets
is ordered.
---
 src/completion/pass.bash-completion | 40 ++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 6 deletions(-)

diff --git a/src/completion/pass.bash-completion b/src/completion/pass.bash-completion
index 95d3e1e..95dcb40 100644
--- a/src/completion/pass.bash-completion
+++ b/src/completion/pass.bash-completion
@@ -4,7 +4,26 @@
 # Brian Mattern <rephorm at rephorm.com>. All Rights Reserved.
 # This file is licensed under the GPLv2+. Please see COPYING for more information.
 
+_sort_entries_string () {
+    # local sorted_list=$(echo $1 | xargs -n1 | sort | xargs)
+    local sorted_list=$(echo $1 | tr ' ' '\n' | sort | tr '\n' ' ')
+    echo $sorted_list
+}
+
+_setup_tmp_compreply () {
+    TMP_COMPREPLY=()
+}
+
+_append_tmp_compreply () {
+    # sort the TMP_COMPREPLY array
+    IFS=$'\n' TMP_COMPREPLY=($(sort <<<"${TMP_COMPREPLY[*]}")); unset IFS
+    # append the tmp array to the COMPREPLY
+    COMPREPLY+=( "${TMP_COMPREPLY[@]}" )
+}
+
 _pass_complete_entries () {
+    _setup_tmp_compreply
+
 	local prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
 	prefix="${prefix%/}/"
 	local suffix=".gpg"
@@ -44,7 +63,7 @@ _pass_complete_entries () {
 		[[ -d $item ]] && item="$item/"
 
 		item="${item%$suffix}"
-		COMPREPLY+=("${item#$prefix}")
+		TMP_COMPREPLY+=("${item#$prefix}")
 		if [[ $i -eq 0 ]]; then
 			firstitem=$item
 		fi
@@ -56,9 +75,13 @@ _pass_complete_entries () {
 	if [[ $i -gt 1 || ( $i -eq 1 && -d $firstitem ) ]]; then
 		compopt -o nospace
 	fi
+
+    _append_tmp_compreply
 }
 
 _pass_complete_folders () {
+    _setup_tmp_compreply
+
 	local prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
 	prefix="${prefix%/}/"
 
@@ -66,25 +89,30 @@ _pass_complete_folders () {
 	local items=($(compgen -d $prefix$cur))
 	for item in ${items[@]}; do
 		[[ $item == $prefix.* ]] && continue
-		COMPREPLY+=("${item#$prefix}/")
+		TMP_COMPREPLY+=("${item#$prefix}/")
 	done
+
+    _append_tmp_compreply
 }
 
 _pass_complete_keys () {
+	_setup_tmp_compreply
+
 	local GPG="gpg"
 	which gpg2 &>/dev/null && GPG="gpg2"
-
 	local IFS=$'\n'
 	# Extract names and email addresses from gpg --list-keys
 	local keys="$($GPG --list-secret-keys --with-colons | cut -d : -f 10 | sort -u | sed '/^$/d')"
-	COMPREPLY+=($(compgen -W "${keys}" -- ${cur}))
+	TMP_COMPREPLY+=($(compgen -W "${keys}" -- ${cur}))
+
+    _append_tmp_compreply
 }
 
 _pass()
 {
 	COMPREPLY=()
 	local cur="${COMP_WORDS[COMP_CWORD]}"
-	local commands="init ls find grep show insert generate edit rm mv cp git help version ${PASSWORD_STORE_EXTENSION_COMMANDS[*]}"
+    local commands=$(_sort_entries_string "init ls find grep show insert generate edit rm mv cp git help version ${PASSWORD_STORE_EXTENSION_COMMANDS[*]}")
 	if [[ $COMP_CWORD -gt 1 ]]; then
 		local lastarg="${COMP_WORDS[$COMP_CWORD-1]}"
 		case "${COMP_WORDS[1]}" in
@@ -141,4 +169,4 @@ _pass()
 	fi
 }
 
-complete -o filenames -F _pass pass
+complete -o nosort -o filenames -F _pass pass
-- 
2.17.1



More information about the Password-Store mailing list