[PATCH] find prints number of results and has an option to copy a, unique match to clipboard

Benjamin Narang ben.narang at laposte.net
Mon Dec 5 22:09:13 CET 2016


---
 src/password-store.sh | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index 63be840..5a2ce89 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -222,8 +222,9 @@ cmd_usage() {
 	        Selectively reencrypt existing passwords using new gpg-id.
 	    $PROGRAM [ls] [subfolder]
 	        List passwords.
-	    $PROGRAM find pass-names...
-	    	List passwords that match pass-names.
+	    $PROGRAM find [--clip,-c] pass-names...
+	        List passwords that match pass-names.
+	        If [--clip,-c] option is provided and if there is a unique
match, it is copied to clipboard.
 	    $PROGRAM [show] [--clip[=line-number],-c[line-number]] pass-name
 	        Show existing password and optionally put it on the clipboard.
 	        If put on the clipboard, it will be cleared in $CLIP_TIME seconds.
@@ -333,10 +334,28 @@ cmd_show() {
 }

 cmd_find() {
-	[[ -z "$@" ]] && die "Usage: $PROGRAM $COMMAND pass-names..."
+	local opts clip=0
+	opts="$($GETOPT -o c -l clip -n "$PROGRAM" -- "$@")"
+	eval set -- "$opts"
+	local err=$?
+	while true; do case $1 in
+		-c|--clip) clip=1; shift; break ;;
+		-|--) shift; break ;;
+	esac done
+
+	[[ $err -ne 0 ]] && die "Usage: $PROGRAM $COMMAND [--clip,-c] pass-names"
 	IFS="," eval 'echo "Search Terms: $*"'
-	local terms="*$(printf '%s*|*' "$@")"
-	tree -C -l --noreport -P "${terms%|*}" --prune --matchdirs
--ignore-case "$PREFIX" | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?(
->|$)/\1\2/g'
+	local terms_tree="*$(printf '%s*|*' "$@")"
+	local terms_find="$(printf '.*%s.*\.gpg|' "$@")"
+	local matching_passwords=$(find $PREFIX -regextype posix-egrep -iregex
"$terms_find")
+	local matching_passwords_nb=$(echo "$matching_passwords" | wc -l)
+	tree -C -l --noreport -P "${terms_tree%|*}" --prune --matchdirs
--ignore-case "$PREFIX" | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?(
->|$)/\1\2/g'
+	if [[ $matching_passwords_nb -eq 1 ]] && [[ $clip -eq 1 ]]; then
+		echo "1 result."
+		cmd_show -c "$(echo $matching_passwords | sed -E "s%\.gpg|$PREFIX/%%g")"
+	else
+		echo "$matching_passwords_nb results."
+	fi
 }

 cmd_grep() {
-- 
2.1.4



More information about the Password-Store mailing list