| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # 1 - source copy from https://geluk.io/p/passmenu.sh (https://github.com/Baggykiin/pass-winmenu) |
| 4 | # 2 - make dmenu case insensitive |
| 5 | # 3 - correct include_username variable |
| 6 | # 4 - only pick out usernames if they are the start of the sentence, i.e. ignore non-primary usernames listed in the file (mostly for bitbucket) |
| 7 | # 5 - dependency test and install |
| 8 | # 6 - error output on bad argument |
| 9 | |
| 10 | shopt -s nullglob globstar |
| 11 | |
| 12 | typeit=0 |
| 13 | include_username=1 |
| 14 | editit=0 |
| 15 | deps=0 |
| 16 | |
| 17 | while [[ -n "$1" ]]; do |
| 18 | if [[ $1 == "--type" ]]; then |
| 19 | typeit=1 |
| 20 | elif [[ $1 == "--no-username" ]]; then |
| 21 | include_username=0 |
| 22 | elif [[ $1 == "--edit" ]]; then |
| 23 | editit=1 |
| 24 | elif [[ $1 == "--install-dependencies" ]]; then |
| 25 | deps=1 |
| 26 | elif [[ $1 == "--" ]]; then |
| 27 | break; |
| 28 | else |
| 29 | echo "Error: unhandled argument $1." >&2 |
| 30 | exit 1 |
| 31 | fi |
| 32 | shift |
| 33 | done |
| 34 | |
| 35 | if [[ $deps -eq 1 ]]; then |
| 36 | sudo apt install dmenu xclip xdotool -y |
| 37 | exit |
| 38 | fi |
| 39 | |
| 40 | if ! [ -x "$(command -v dmenu)" ]; then |
| 41 | echo 'Error: dmenu is not installed.' >&2 |
| 42 | exit 1 |
| 43 | fi |
| 44 | |
| 45 | prefix=${PASSWORD_STORE_DIR-~/.password-store} |
| 46 | password_files=( "$prefix"/**/*.gpg ) |
| 47 | password_files=( "${password_files[@]#"$prefix"/}" ) |
| 48 | password_files=( "${password_files[@]%.gpg}" ) |
| 49 | |
| 50 | password=$(printf '%s\n' "${password_files[@]}" | dmenu -i "$@") |
| 51 | |
| 52 | if [[ $editit -eq 1 ]]; then |
| 53 | # Insert the edit command for your preferred terminal here: |
| 54 | #termite -e "pass edit '$password' > /dev/null" |
| 55 | #xterm "pass edit '$password' > /dev/null" |
| 56 | #urxvt -e "pass edit '$password' > /dev/null" |
| 57 | #gnome-terminal -- pass edit "$password" > /dev/null |
| 58 | # ..etc |
| 59 | exit |
| 60 | fi |
| 61 | |
| 62 | [[ -n $password ]] || exit |
| 63 | |
| 64 | if ! [ -x "$(command -v xclip)" ]; then |
| 65 | echo 'Error: xclip is not installed.' >&2 |
| 66 | exit 1 |
| 67 | fi |
| 68 | |
| 69 | if [[ $typeit -eq 0 ]]; then |
| 70 | pass show -c "$password" | xclip |
| 71 | |
| 72 | else |
| 73 | if ! [ -x "$(command -v xdotool)" ]; then |
| 74 | echo 'Error: xdotool is not installed.' >&2 |
| 75 | exit 1 |
| 76 | fi |
| 77 | |
| 78 | pwfile=$(pass show "$password") |
| 79 | password=$(printf "%s" "$pwfile" | sed -n 1p) |
| 80 | username=$(printf "%s" "$pwfile" | grep -Po "^[Uu]ser(name)?: \K(.*)") |
| 81 | printf '%s' "$password" | xclip -selection clipboard |
| 82 | print '%s' "$password" | xclip |
| 83 | if [ -z "$username" ] || [[ $include_username -eq 0 ]]; then |
| 84 | printf '%s' "$password" | xdotool type --clearmodifiers --file - |
| 85 | else |
| 86 | pstr=$(printf '%s\t%s' "$username" "$password") |
| 87 | xdotool type "$pstr" |
| 88 | fi |
| 89 | fi |
| 90 |
willpower232 / passmenu.sh
Last active 1 month ago
dmenu script for pass
Revision 6309b94059bf401643295ee70a432529ddb76619