| 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 | |
| 9 | shopt -s nullglob globstar |
| 10 | |
| 11 | typeit=0 |
| 12 | include_username=1 |
| 13 | editit=0 |
| 14 | deps=0 |
| 15 | |
| 16 | while [[ -n "$1" ]]; do |
| 17 | if [[ $1 == "--type" ]]; then |
| 18 | typeit=1 |
| 19 | elif [[ $1 == "--no-username" ]]; then |
| 20 | include_username=0 |
| 21 | elif [[ $1 == "--edit" ]]; then |
| 22 | editit=1 |
| 23 | elif [[ $1 == "--install-dependencies" ]]; then |
| 24 | deps=1 |
| 25 | elif [[ $1 == "--" ]]; then |
| 26 | break; |
| 27 | fi |
| 28 | shift |
| 29 | done |
| 30 | |
| 31 | if [[ $deps -eq 1 ]]; then |
| 32 | sudo apt install dmenu xclip xdotool -y |
| 33 | exit |
| 34 | fi |
| 35 | |
| 36 | if ! [ -x "$(command -v dmenu)" ]; then |
| 37 | echo 'Error: dmenu is not installed.' >&2 |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | prefix=${PASSWORD_STORE_DIR-~/.password-store} |
| 42 | password_files=( "$prefix"/**/*.gpg ) |
| 43 | password_files=( "${password_files[@]#"$prefix"/}" ) |
| 44 | password_files=( "${password_files[@]%.gpg}" ) |
| 45 | |
| 46 | password=$(printf '%s\n' "${password_files[@]}" | dmenu -i "$@") |
| 47 | |
| 48 | if [[ $editit -eq 1 ]]; then |
| 49 | # Insert the edit command for your preferred terminal here: |
| 50 | #termite -e "pass edit '$password' > /dev/null" |
| 51 | #xterm "pass edit '$password' > /dev/null" |
| 52 | #urxvt -e "pass edit '$password' > /dev/null" |
| 53 | #gnome-terminal -- pass edit "$password" > /dev/null |
| 54 | # ..etc |
| 55 | exit |
| 56 | fi |
| 57 | |
| 58 | [[ -n $password ]] || exit |
| 59 | |
| 60 | if ! [ -x "$(command -v xclip)" ]; then |
| 61 | echo 'Error: xclip is not installed.' >&2 |
| 62 | exit 1 |
| 63 | fi |
| 64 | |
| 65 | if [[ $typeit -eq 0 ]]; then |
| 66 | pass show -c "$password" | xclip |
| 67 | |
| 68 | else |
| 69 | if ! [ -x "$(command -v xdotool)" ]; then |
| 70 | echo 'Error: xdotool is not installed.' >&2 |
| 71 | exit 1 |
| 72 | fi |
| 73 | |
| 74 | pwfile=$(pass show "$password") |
| 75 | password=$(printf "%s" "$pwfile" | sed -n 1p) |
| 76 | username=$(printf "%s" "$pwfile" | grep -Po "^[Uu]ser(name)?: \K(.*)") |
| 77 | printf '%s' "$password" | xclip -selection clipboard |
| 78 | print '%s' "$password" | xclip |
| 79 | if [ -z "$username" ] || [[ $include_username -eq 0 ]]; then |
| 80 | printf '%s' "$password" | xdotool type --clearmodifiers --file - |
| 81 | else |
| 82 | pstr=$(printf '%s\t%s' "$username" "$password") |
| 83 | xdotool type "$pstr" |
| 84 | fi |
| 85 | fi |
| 86 |
willpower232 / passmenu.sh
Last active 1 month ago
dmenu script for pass
Revision 1020eda10073725f3adc2c471dcd5b247ce77ca7