From f32aceae66fb5dcfeeee6a8127664934ef354927 Mon Sep 17 00:00:00 2001
From: Timmo Verlaan <tverlaan@gmail.com>
Date: Tue, 5 Jun 2018 12:36:26 +0200
Subject: [PATCH] add timeout option to pass show to clear output

---
 src/password-store.sh | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index 19b3124..c1b5dfe 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -263,9 +263,9 @@ cmd_usage() {
 	        List passwords.
 	    $PROGRAM find pass-names...
 	    	List passwords that match pass-names.
-	    $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.
+	    $PROGRAM [show] [--clip[=line-number],-c[line-number] | --timeout,-t] pass-name
+	        Show existing password, optionally clear output after $CLIP_TIME seconds.
+	        Or put on the clipboard, it will be cleared in $CLIP_TIME seconds.
 	    $PROGRAM grep search-string
 	        Search for password files containing search-string when decrypted.
 	    $PROGRAM insert [--echo,-e | --multiline,-m] [--force,-f] pass-name
@@ -345,13 +345,14 @@ cmd_init() {
 }
 
 cmd_show() {
-	local opts selected_line clip=0 qrcode=0
-	opts="$($GETOPT -o q::c:: -l qrcode::,clip:: -n "$PROGRAM" -- "$@")"
+	local opts selected_line clip=0 qrcode=0 timeout=0
+	opts="$($GETOPT -o q::c::t -l qrcode::,clip::,timeout -n "$PROGRAM" -- "$@")"
 	local err=$?
 	eval set -- "$opts"
 	while true; do case $1 in
 		-q|--qrcode) qrcode=1; selected_line="${2:-1}"; shift 2 ;;
 		-c|--clip) clip=1; selected_line="${2:-1}"; shift 2 ;;
+		-t|--timeout) timeout=1; shift ;;
 		--) shift; break ;;
 	esac done
 
@@ -362,7 +363,19 @@ cmd_show() {
 	check_sneaky_paths "$path"
 	if [[ -f $passfile ]]; then
 		if [[ $clip -eq 0 && $qrcode -eq 0 ]]; then
-			$GPG -d "${GPG_OPTS[@]}" "$passfile" || exit $?
+			if [[ $timeout -eq 0 ]]; then
+				$GPG -d "${GPG_OPTS[@]}" "$passfile" || exit $?
+			else
+				local content="$($GPG -d "${GPG_OPTS[@]}" "$passfile")"
+				[[ -n $content ]] || die "Decrypting failed or file is empty"
+				printf "$content\n"
+				sleep $CLIP_TIME
+				local lines="$(printf "${content}\n" | wc -l | tr -d ' ')"
+				printf "\e[${lines}A"
+				printf "$content\n" | while IFS= read -r line; do
+					printf "%${#line}s\n"
+				done
+			fi
 		else
 			[[ $selected_line =~ ^[0-9]+$ ]] || die "Clip location '$selected_line' is not a number."
 			local pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${selected_line} | head -n 1)"
-- 
2.14.1

