From 87f20b7e05c558266af84843c62cbf9de89344f8 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 18 Dec 2021 16:59:25 +0100 Subject: [PATCH 1/2] Extract clipper choice to function --- pass-pick | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/pass-pick b/pass-pick index 8e72d75..cdfac10 100755 --- a/pass-pick +++ b/pass-pick @@ -71,6 +71,19 @@ _picker() { exit 1 fi } +# copies to clipboard, removes any trailing newlines, +# and only keeps it in for 1 paste (1 loop to read in script, 1 to output) +_clipper() { + if command -v wl-copy 1>/dev/null 2>/dev/null; then + wl-copy -o -n + elif command -v xclip 1>/dev/null 2>/dev/null; then + xclip -i -selection 'clipboard' -loops 2 -rmlastnl + elif command -v xsel 1>/dev/null 2>/dev/null; then + xsel -b + else + notify-send "No clipboard utility" "Install wl-copy, xclip or xsel." + fi +} # parse, see https://unix.stackexchange.com/a/331965/8541 _parse_config() { @@ -164,7 +177,6 @@ clip_password() { _p_get_field() { local gp_entry="$1" local gp_field="$2" - local clip="$3" # return on first successfully returned key for key in $gp_field; do @@ -173,23 +185,7 @@ _p_get_field() { # found entry if [ -n "$value" ]; then - - if [ -n "$clip" ]; then - # copies to clipboard, removes any trailing newlines, - # and only keeps it in for 1 paste (1 loop to read in script, 1 to output) - if command -v wl-copy; then - echo "$value" | wl-copy -o && break - elif command -v xclip; then - echo "$value" | xclip -i -selection 'clipboard' -loops 2 -rmlastnl && break - elif command -v xsel; then - echo "$value" | xsel -b && break - else - notify-send "No clipboard utility" "Install wl-copy, xclip or xsel." - fi - else - echo "$value" && break - fi - + echo "$value" && break fi done } @@ -212,7 +208,7 @@ show_username() { } clip_username() { - _p_get_field "$1" "${PASS_USERNAME_FIELD}" "-c" + _p_get_field "$1" "${PASS_USERNAME_FIELD}" | _clipper } show_field() { @@ -220,7 +216,7 @@ show_field() { } clip_field() { - _p_get_field "$1" "$2" "-c" + _p_get_field "$1" "$2" | _clipper } list_fields() { From 846c6793538514a2ea8bbbddad5ff82fddf21a33 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sat, 18 Dec 2021 17:14:52 +0100 Subject: [PATCH 2/2] Add fallback to entry name if no username field Will look for the username as before within the metadata of the entry, but if there is no field which satisfies the conditions (by default being named username, user, or login), will fall back to using the entry name in pass (i.e. the filename). fixes #3 --- pass-pick | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pass-pick b/pass-pick index cdfac10..5c76db1 100755 --- a/pass-pick +++ b/pass-pick @@ -203,12 +203,16 @@ _p_get_key_value() { } # return username for argument passed +# Prefers in-metadata username, falls back to filename show_username() { - _p_get_field "$1" "${PASS_USERNAME_FIELD}" + result=$(_p_get_field "$1" "${PASS_USERNAME_FIELD}") + if [ -z "$result" ]; then + echo "${1##*/}" + fi } clip_username() { - _p_get_field "$1" "${PASS_USERNAME_FIELD}" | _clipper + show_username "$1" "${PASS_USERNAME_FIELD}" | _clipper } show_field() { @@ -216,7 +220,7 @@ show_field() { } clip_field() { - _p_get_field "$1" "$2" | _clipper + show_field "$1" "$2" | _clipper } list_fields() {