Skip to content

Instantly share code, notes, and snippets.

@silveiralexf
Last active December 30, 2019 15:26
Show Gist options
  • Save silveiralexf/ecd4049be0b50c1773d1bceeb5a71b9d to your computer and use it in GitHub Desktop.
Save silveiralexf/ecd4049be0b50c1773d1bceeb5a71b9d to your computer and use it in GitHub Desktop.
Simple dmenu-pass launcher for passwords, notes and application shortcuts
#!/bin/bash
#-------------------------------------------------------------------------------
# File : dmenu-pass.sh
# Author : Felipe A. Silveira (felipe.alexandre@gmail.com)
# Repository : https://gist.github.com/fsilveir
#
#-------------------------------------------------------------------------------
# SCRIPT DESCRIPTION
#-------------------------------------------------------------------------------
#
# Synopsis : Simple dmenu-pass launcher for passwords, notes and application
# shortcuts
#
# Requirements: pass, gpg, dmenu, xclip, gedit, exo-open
#
# Arguments:
# --pass : Chooses a password from password vault
# --notes : Open a personal note with gedit
# --apps : Quick launches a desktop application with exo-open
#
#-------------------------------------------------------------------------------
# Globals
#-------------------------------------------------------------------------------
# The following utilities are required for the script to work
REQUIREMENTS="pass gpg dmenu xclip gedit exo-open"
# Default directories for password store, personal notes, and desktop shortcuts
PASSWORD_STORE_DIR="$HOME/.password-store/keys"
PERSONAL_NOTES_DIR="$HOME/git/notes"
DESKTOP_APPS_DIR="/usr/share/applications"
# Extensions to hide from dmenu display
PASSWORD_STORE_EXT=".gpg"
PERSONAL_NOTES_EXT=".md"
DESKTOP_APPS_EXT=".desktop"
# Dmenu font and color schemes
DMENU_FONT="Dejavu Sans Mono:medium:size=18"
DMENU_COLOR_PASS="-nb #191919 -nf #FF0000 -sb #FF9318 -sf #191919"
DMENU_COLOR_APPS="-nb #191919 -nf #2E9EF4 -sb #2E9EF4 -sf #191919"
DMENU_COLOR_NOTES="-nb #191919 -nf #2aa198 -sb #2aa198 -sf #191919"
#-------------------------------------------------------------------------------
# Functions
#-------------------------------------------------------------------------------
main() {
check_requirements
get_dmenu_settings "$@"
get_dmenu_input
take_action
}
take_action() {
if [[ $arg == "--pass" ]]; then
pass show -c "keys/${result}" 2>/dev/null
echo "$result" | head -n 1 | tr -d "\n" | awk -F "/" '{print $NF}' | xclip
sleep 10 && pkill xclip
elif [[ $arg == "--notes" ]]; then
gedit "${prefix}/${result}.md"
elif [[ $arg == "--apps" ]]; then
exo-open "${prefix}/${result}${suffix}" &>/dev/null
fi
}
get_dmenu_input() {
shopt -s nullglob globstar
item=( "$prefix"/**/*${suffix})
item=( "${item[@]#"$prefix"/}" )
item=( "${item[@]%${suffix}}" )
result=$(printf '%s\n' "${item[@]}" | dmenu -fn "$DMENU_FONT" $color_scheme)
[[ -n $result ]] || exit
}
get_dmenu_settings() {
if [[ $@ == "" ]]; then
msg_usage_error
fi
for arg in "$@"
do
if [[ $arg == "--pass" ]]; then
color_scheme="${DMENU_COLOR_PASS}"
prefix="${PASSWORD_STORE_DIR}"
suffix="${PASSWORD_STORE_EXT}"
shift
elif [[ $arg == "--notes" ]]; then
color_scheme="${DMENU_COLOR_NOTES}"
prefix="${PERSONAL_NOTES_DIR}"
suffix="${PERSONAL_NOTES_EXT}"
shift
elif [[ $arg == "--apps" ]]; then
color_scheme="${DMENU_COLOR_APPS}"
prefix="${DESKTOP_APPS_DIR}"
suffix="${DESKTOP_APPS_EXT}"
shift
else
msg_usage_error
fi
done
}
check_requirements() {
for dir_name in "${PASSWORD_STORE_DIR}" "${PERSONAL_NOTES_DIR}" "${DESKTOP_APPS_DIR}"
do
if [ ! -d "${dir_name}" ] ; then
echo "$(date +%Y-%m-%d): ERROR - Directory '${dir_name}' not found! Exiting!"
exit 1
fi
done
for UTIL in $REQUIREMENTS
do
command -v "$UTIL" &> /dev/null || msg_missed_req
done
}
msg_missed_req() {
echo "$(date +%Y-%m-%d): ERROR - Required util '${UTIL}' not found! Exiting!"
exit 1
}
msg_usage_error() {
printf "ERROR - You did not specify a valid argument. Exiting!
The following arguments are available:
--pass : Chooses a password from password vault
--notes : Open a personal note with gedit
--apps : Quick launches a desktop application with exo-open\n\n"
exit 1
}
#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------
main "$@"
#-------------------------------------------------------------------------------
# EOF
#-------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment