[PATCH] Add plain and no-header flags to list subcommand to make the, output friendler with tools like fzf

Gonzalo Matheu gonzalommj at gmail.com
Tue Jun 4 15:05:05 CEST 2019


Hello people,

I added a couple of options on 'list/ls' command to make the output 
friendler for a fuzzy search (like fzf). Combining this commands is 
faster (at least in my case) to show passwords and you don't even need 
to remember the full name.


The options are **--no-header** and **--plain** flags are added to make 
the list command output friendlier to 
[fzf](https://github.com/junegunn/fzf).

Output looks like:

```
➜  pass --plain --no-header
b
a/2
a/1
cred1
I am a cred with lots of spaces
```
and the default output is:
```
➜  pass
Password Store
├── a
│   ├── 1
│   └── 2
├── b
├── cred1
└── I am a cred with lots of spaces
```

Having [fzf](https://github.com/junegunn/fzf) installed and executing  
`pass show $(pass --plain --no-header | fzf)` will open the fuzzy finder 
to search password names. (I wrote a [password-store zsh 
plugin](https://github.com/gmatheu/shell-plugins/blob/master/password-store/init.sh) 
using antigen)

asciinema recording with usage -> 
https://asciinema.org/a/cwdgh4xfTNm6fr6SEnH3arPEm


Hope this is useful.

Regards.

PS: I forked on gitlab.com -> 
https://gitlab.com/gmatheu/password-store/merge_requests/1

-

Gonzalo Matheu


---

diff --git a/man/pass.1 b/man/pass.1
index a555dcb..7586308 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -74,12 +74,17 @@ the password store. If only one \fIgpg-id\fP is 
given, and it is an empty string
  then the current \fI.gpg-id\fP file for the specified \fIsub-folder\fP 
(or root if
  unspecified) is removed.
  .TP
-\fBls\fP \fIsubfolder\fP
+\fBls\fP [ \fI--plain\fP ] \fIsubfolder\fP
  List names of passwords inside the tree at
  .I subfolder
  by using the
  .BR tree (1)
-program. This command is alternatively named \fBlist\fP.
+program. If \fI--plain\fP is specified it will show qualified password 
names using
+.BR find (1)
+program instead of
+.BR tree (1)
+utility. When \fI--no-header\fP is used it will show only password 
names without any header.
+This command is alternatively named \fBlist\fP.
  .TP
  \fBgrep\fP [\fIGREPOPTIONS\fP] \fIsearch-string\fP
  Searches inside each decrypted password file for \fIsearch-string\fP, 
and displays line
@@ -472,6 +477,7 @@ The location of the text editor used by \fBedit\fP.
  .BR xclip (1),
  .BR wl-clipboard (1),
  .BR qrencode (1).
+.BR find(1).

  .SH AUTHOR
  .B pass
diff --git a/src/password-store.sh b/src/password-store.sh
index 284eabf..5dbc61d 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -364,16 +364,19 @@ cmd_init() {

  cmd_show() {
      local opts selected_line clip=0 qrcode=0
-    opts="$($GETOPT -o q::c:: -l qrcode::,clip:: -n "$PROGRAM" -- "$@")"
+    opts="$($GETOPT -o q::c:: -l qrcode::,clip::,plain,no-header -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 ;;
+        --plain) plain=1; shift 1;;
+        --no-header) no_header=1; shift 1;;
          --) shift; break ;;
      esac done

-    [[ $err -ne 0 || ( $qrcode -eq 1 && $clip -eq 1 ) ]] && die "Usage: 
$PROGRAM $COMMAND [--clip[=line-number],-c[line-number]] 
[--qrcode[=line-number],-q[line-number]] [pass-name]"
+    [[ $err -ne 0 || ( $qrcode -eq 1 && $clip -eq 1 ) ]] && \
+    die "Usage: $PROGRAM $COMMAND 
[--clip[=line-number],-c[line-number]] 
[--qrcode[=line-number],-q[line-number]] [--plain] [--no-header] 
[pass-name]"

      local pass
      local path="$1"
@@ -394,12 +397,18 @@ cmd_show() {
              fi
          fi
      elif [[ -d $PREFIX/$path ]]; then
-        if [[ -z $path ]]; then
-            echo "Password Store"
-        else
-            echo "${path%\/}"
+        if [[ $no_header -ne 1 ]]; then
+            if [[ -z $path ]]; then
+                echo "Password Store"
+            else
+                echo "${path%\/}"
+            fi
+        fi
+        if [[ $plain -eq 1 ]]; then
+            find "$PREFIX/$path" -name '*.gpg' | sed -E "s#$PREFIX/##g; 
s/\.gpg//g"
+            else
+                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
          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
diff --git a/tests/t0020-show-tests.sh b/tests/t0020-show-tests.sh
index a4b782f..0d62ae1 100755
--- a/tests/t0020-show-tests.sh
+++ b/tests/t0020-show-tests.sh
@@ -19,4 +19,42 @@ test_expect_success 'Test "show" of nonexistant 
password' '
      test_must_fail "$PASS" show cred2
  '

+test_expect_success 'Show list as a tree' '
+    "$PASS" insert -e "a/1"<<<"a1" &&
+    "$PASS" insert -e "a/2"<<<"a2" &&
+    "$PASS" insert -e "b"<<<"b" &&
+    "$PASS" list | grep -q ".*a" &&
+    "$PASS" list | grep -q ".*1" &&
+    "$PASS" list | grep -q ".*b"
+'
+
+test_expect_success 'Show plain list' '
+    "$PASS" insert -e "a/1"<<<"a1" &&
+    "$PASS" insert -e "a/2"<<<"a2" &&
+    "$PASS" insert -e "b"<<<"b" &&
+    "$PASS" list --plain | grep -q "a/1" &&
+    "$PASS" list --plain | grep -q "a/2" &&
+    "$PASS" list --plain | grep -q "b"
+'
+
+test_expect_success 'Show plain list on path' '
+    "$PASS" insert -e "a/1"<<<"a1" &&
+    "$PASS" insert -e "a/2"<<<"a2" &&
+    "$PASS" insert -e "b"<<<"b" &&
+    "$PASS" list --plain a | grep -q "a/1" &&
+    "$PASS" list --plain a | grep -q "a/2" &&
+    "$PASS" list --plain a | grep -c "a/" | grep "2" &&
+    "$PASS" list --plain a | grep -c "b" | grep "0"
+'
+
+test_expect_success 'Shows header by default' '
+    "$PASS" insert -e "a/1"<<<"a1" &&
+    "$PASS" list | grep "Password Store"
+'
+
+test_expect_success 'Do not show header' '
+    "$PASS" insert -e "a/1"<<<"a1" &&
+    "$PASS" list --no-header | grep -c "Password Store" | grep "0"
+'
+
  test_done
-- 
2.17.1


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.zx2c4.com/pipermail/password-store/attachments/20190604/8a1c25e2/attachment.html>


More information about the Password-Store mailing list