Skip to content

Instantly share code, notes, and snippets.

@atmoz
Last active May 18, 2018 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atmoz/bc6ef0f724d8105cee5e16c4bca04023 to your computer and use it in GitHub Desktop.
Save atmoz/bc6ef0f724d8105cee5e16c4bca04023 to your computer and use it in GitHub Desktop.
# ...
# assuming $mod and $dmenuOptions are set
set $passMenu "pass: [p]assword [l]ogin [b]rowse"
bindsym $mod+p mode $passMenu
mode $passMenu {
bindsym p exec --no-startup-id ~/bin/passmenu --type -- $dmenuOptions; mode "default"
bindsym l exec --no-startup-id ~/bin/passmenu --type --get username:password --enter -- $dmenuOptions; mode "default"
bindsym b exec --no-startup-id ~/bin/passmenu --type --browse -- $dmenuOptions; mode "default"
bindsym Return mode "default"
bindsym Escape mode "default"
}
# ...
This code will not be updated. Use this instead: https://github.com/atmoz/bin/blob/master/passmenu
#!/usr/bin/env bash
# No longer updated. Download newest here:
# https://github.com/atmoz/bin/blob/master/passmenu
shopt -s nullglob globstar
#####################################################################
# Parse arguments
#####################################################################
clipTime=45
typeit=0
typeDelay=30
enter=0
tab=1
browse=0
xclipArgs="-selection clipboard"
declare -a get=("password")
while [ -n "$1" ]; do
case "$1" in
--get) IFS=':' read -a get <<< "$2"; shift 2 ;;
--browse) browse=1; shift ;;
--type) typeit=1; shift ;;
--enter) enter=1; shift ;;
--no-tab) tab=0; shift ;;
--delay) typeDelay="$2"; shift 2;;
--xclip-args) xclipArgs="$2"; shift 2;;
--) shift; break ;;
*) break ;;
esac
done
#####################################################################
# Helper functions
#####################################################################
function findPasswordFiles() {
prefix=${PASSWORD_STORE_DIR-~/.password-store}
password_files=( "$prefix"/**/*.gpg )
password_files=( "${password_files[@]#"$prefix"/}" )
password_files=( "${password_files[@]%.gpg}" )
printf '%s\n' "${password_files[@]}"
}
function getKeys() {
# 1. Replace password value with key
# 2. Remove invalid lines
# 3. Remove values
echo "$@" | sed \
-e '1c\password' \
-e '/^[^:]*\:.*$/!d' \
-e 's/^\([^:]*\)\:.*$/\1/'
}
function getValue() {
if [[ "$1" == "password" ]]; then
echo "$2" | { IFS= read -r pass; printf %s "$pass"; }
else
echo "$2" | sed -n "s/^$1\:[[:blank:]]*\(.*\)$/\1/p"
fi
}
function typeText() {
echo -n "$@" | xdotool type --clearmodifiers --delay $typeDelay --file -
}
function typeEnter() {
xdotool key --delay $typeDelay Return
}
function typeTab() {
xdotool key --delay $typeDelay Tab
}
function clipboard() {
string="$@"
oldClip="$(xclip $xclipArgs -o 2>/dev/null)"
echo -n "$string" | xclip $xclipArgs
{
sleep $clipTime
if [ "$(xclip $xclipArgs -o 2>/dev/null)" == "$string" ]; then
echo -n "$oldClip" | xclip $xclipArgs
fi
} &
}
#####################################################################
# Select and unlock password file
#####################################################################
password=$(findPasswordFiles | dmenu "$@")
[[ -n $password ]] || exit
lines="$(pass show "$password" 2>/dev/null)"
#####################################################################
# Browse custom values with dmenu
#####################################################################
if [[ $browse -eq 1 ]]; then
keys="$(getKeys "$lines")"
key="password"
values=""
if [[ $typeit -eq 0 ]]; then
prompt="Copy to clipboard: "
else
prompt="Write to focused input: "
fi
while true; do
key="$(echo -e "$keys" | IFS=$'\n' dmenu "$@" -p "$prompt")"
[[ -n "$key" ]] || break
[[ -n "$value" ]] && values="$values"$'\n'
value="$(getValue "$key" "$lines")"
if [[ $typeit -eq 1 ]]; then
typeText "$value"
if [[ $tab -eq 1 ]]; then
typeTab
fi
else
prompt="$prompt $key"
values="$values$value"
fi
done
if [[ $typeit -eq 0 ]]; then
clipboard "$values"
fi
#####################################################################
# Get password and/or custom values with arguments
#####################################################################
else
values=""
for key in "${get[@]}"; do
if [ "$key" == "password" ]; then
value="$(echo "$lines" | head -n1 )"
else
value="$(getValue "$key" "$lines")"
fi
[[ -n "$values" ]] && values="$values"$'\n'
values="$values$value"
done
if [[ $typeit -eq 0 ]]; then
clipboard "$values"
else
vc=0 # value counter
while IFS=$'\n' read -r value || [[ -n "$value" ]]; do
let "vc++"
if [[ $vc -gt 1 && $tab -eq 1 ]]; then
typeTab
fi
typeText "$value"
done <<< $values
if [[ $enter -eq 1 ]]; then
typeEnter
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment