[PATCH bash ordered autocomplete] Order the autocompletion in bash

jerabaul29 jean.rblt at gmail.com
Mon Mar 30 10:34:31 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 | 35 ++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 6 deletions(-)

diff --git a/src/completion/pass.bash-completion b/src/completion/pass.bash-completion
index 95d3e1e..68b65fe 100644
--- a/src/completion/pass.bash-completion
+++ b/src/completion/pass.bash-completion
@@ -4,7 +4,21 @@
 # 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 () {
+	echo $1 | tr ' ' '\n' | sort | tr '\n' ' '
+}
+
+_append_to_compreply () {
+	sorted_crrt_compreply_entries=$(_sort_entries_string $1)
+	local IFS=" "
+	for word in ${sorted_crrt_compreply_entries}; do
+		COMPREPLY+=($word)
+	done
+}
+
 _pass_complete_entries () {
+	local crrt_compreply_entries=""
+
 	local prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
 	prefix="${prefix%/}/"
 	local suffix=".gpg"
@@ -44,7 +58,7 @@ _pass_complete_entries () {
 		[[ -d $item ]] && item="$item/"
 
 		item="${item%$suffix}"
-		COMPREPLY+=("${item#$prefix}")
+		crrt_compreply_entries="$crrt_compreply_entries ${item#$prefix}"
 		if [[ $i -eq 0 ]]; then
 			firstitem=$item
 		fi
@@ -56,9 +70,13 @@ _pass_complete_entries () {
 	if [[ $i -gt 1 || ( $i -eq 1 && -d $firstitem ) ]]; then
 		compopt -o nospace
 	fi
+
+	_append_to_compreply $crrt_compreply_entries
 }
 
 _pass_complete_folders () {
+	local crrt_compreply_entries=""
+
 	local prefix="${PASSWORD_STORE_DIR:-$HOME/.password-store/}"
 	prefix="${prefix%/}/"
 
@@ -66,25 +84,30 @@ _pass_complete_folders () {
 	local items=($(compgen -d $prefix$cur))
 	for item in ${items[@]}; do
 		[[ $item == $prefix.* ]] && continue
-		COMPREPLY+=("${item#$prefix}/")
+		crrt_compreply_entries="$crrt_compreply_entries ${item#$prefix}"
 	done
+
+	_append_to_compreply $crrt_compreply_entries
 }
 
 _pass_complete_keys () {
+	local crrt_compreply_entries=""
+
 	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}))
+	crrt_compreply_entries="$crrt_compreply_entries $(compgen -W "${keys}" -- ${cur})"
+
+	_append_to_compreply $crrt_compreply_entries
 }
 
 _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 +164,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