I've written a patch which tests whether the output is a terminal, and
then formats using `tree` only in that case.  If not, `find` is used
instead.  This would simplify scripting around pass.

It may be useful to add `-s|--simple` and `-P|--pretty` parameters to
the find and show commands to override this behavior.

Thoughts? Corrections?

--Paul

From f33de0640cf6db60959b1121e752434c89741b19 Mon Sep 17 00:00:00 2001
From: xPMo <[email protected]>
Date: Wed, 25 Apr 2018 15:03:19 -0500
Subject: [PATCH] Don't `tree` if outputting to pipe

---
 man/pass.1            |  5 +++--
 src/password-store.sh | 23 ++++++++++++++++-------
 2 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/man/pass.1 b/man/pass.1
index e842178..bdf0594 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -77,7 +77,7 @@ unspecified) is removed.
 \fBls\fP \fIsubfolder\fP
 List names of passwords inside the tree at
 .I subfolder
-by using the
+\. If outputting to a terminal, the names will be formatted using the
 .BR tree (1)
 program. This command is alternatively named \fBlist\fP.
 .TP
@@ -89,7 +89,8 @@ for matching. Make use of the \fIGREP_OPTIONS\fP environment variable to set par
 options.
 .TP
 \fBfind\fP \fIpass-names\fP...
-List names of passwords inside the tree that match \fIpass-names\fP by using the
+List names of passwords inside the tree that match \fIpass-names\fP
+\. If outputting to a terminal, the names will be formatted using the
 .BR tree (1)
 program. This command is alternatively named \fBsearch\fP.
 .TP
diff --git a/src/password-store.sh b/src/password-store.sh
index eac5404..edcc463 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -374,12 +374,16 @@ cmd_show() {
 			fi
 		fi
 	elif [[ -d $PREFIX/$path ]]; then
-		if [[ -z $path ]]; then
-			echo "Password Store"
+		if [[ -t 1 ]]; then
+			if [[ -z $path ]]; then
+				echo "Password Store"
+			else
+				echo "${path%\/}"
+			fi
+			tree -C -l --noreport "$PREFIX/$path" | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .gpg at end of line, but keep colors
 		else
-			echo "${path%\/}"
+			find "$PREFIX/$path" -not -path "$PREFIX/.*" -type f | sed -E -e "s:$PREFIX/::" -e 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
 		fi
-		tree -C -l --noreport "$PREFIX/$path" | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .gpg at end of line, but keep colors
 	elif [[ -z $path ]]; then
 		die "Error: password store is empty. Try \"pass init\"."
 	else
@@ -389,9 +393,14 @@ cmd_show() {
 
 cmd_find() {
 	[[ $# -eq 0 ]] && die "Usage: $PROGRAM $COMMAND 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'
+	if [[ -t 1 ]]; then
+		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'
+	else
+		local terms="*$(printf '%s*|*' "$@")"
+		find "$PREFIX/$path" -not -path "$PREFIX/.*" -type f -iname "${terms%|*}" | sed -E -e "s:$PREFIX/::" -e 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
+	fi
 }
 
 cmd_grep() {
-- 
2.17.0



Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Password-Store mailing list
[email protected]
https://lists.zx2c4.com/mailman/listinfo/password-store

Reply via email to