willpower232

willpower232 / passmenu.sh

Last active 1 month ago

Like 0

dmenu script for pass

Revision 5811443b14650d64403393072fda4b91737be3a4

passmenu.sh Raw
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
8shopt -s nullglob globstar
9
10typeit=0
11include_username=1
12editit=0
13
14while [[ -n "$1" ]]; do
15 if [[ $1 == "--type" ]]; then
16 typeit=1
17 elif [[ $1 == "--no-username" ]]; then
18 include_username=0
19 elif [[ $1 == "--edit" ]]; then
20 editit=1
21 elif [[ $1 == "--" ]]; then
22 break;
23 fi
24 shift
25done
26
27
28prefix=${PASSWORD_STORE_DIR-~/.password-store}
29password_files=( "$prefix"/**/*.gpg )
30password_files=( "${password_files[@]#"$prefix"/}" )
31password_files=( "${password_files[@]%.gpg}" )
32
33password=$(printf '%s\n' "${password_files[@]}" | dmenu -i "$@")
34
35if [[ $editit -eq 1 ]]; then
36 # Insert the edit command for your preferred terminal here:
37 #termite -e "pass edit '$password' > /dev/null"
38 #xterm "pass edit '$password' > /dev/null"
39 #urxvt -e "pass edit '$password' > /dev/null"
40 #gnome-terminal -- pass edit "$password" > /dev/null
41 # ..etc
42 exit
43fi
44
45[[ -n $password ]] || exit
46
47if [[ $typeit -eq 0 ]]; then
48 pass show -c "$password" | xclip
49
50else
51 pwfile=$(pass show "$password")
52 password=$(printf "%s" "$pwfile" | sed -n 1p)
53 username=$(printf "%s" "$pwfile" | grep -Po "^[Uu]ser(name)?: \K(.*)")
54 printf '%s' "$password" | xclip -selection clipboard
55 print '%s' "$password" | xclip
56 if [ -z "$username" ] || [[ $include_username -eq 0 ]]; then
57 printf '%s' "$password" | xdotool type --clearmodifiers --file -
58 else
59 pstr=$(printf '%s\t%s' "$username" "$password")
60 xdotool type "$pstr"
61 fi
62fi
63