main.sh
1 #!/usr/bin/env bash 2 3 CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 4 # shellcheck source=scripts/utils.sh 5 source "${CURRENT_DIR}/utils.sh" 6 7 OPT_COPY_TO_CLIPBOARD="$(get_tmux_option "@pass-copy-to-clipboard" "off")" 8 OPT_HIDE_PREVIEW="$(get_tmux_option "@pass-hide-preview" "off")" 9 OPT_HIDE_PW_FROM_PREVIEW="$(get_tmux_option "@pass-hide-pw-from-preview" "off")" 10 spinner_pid="" 11 12 # ------------------------------------------------------------------------------ 13 14 # Taken from: 15 # https://github.com/yardnsm/dotfiles/blob/master/_setup/utils/spinner.sh 16 show_spinner() { 17 18 local -r MSG="$1" 19 20 local -r FRAMES="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏" 21 local -r DELAY=0.05 22 23 local i=0 24 local current_symbol 25 26 trap 'exit 0' SIGTERM 27 28 while true; do 29 current_symbol="${FRAMES:i++%${#FRAMES}:1}" 30 printf "\\e[0;34m%s\\e[0m %s" "$current_symbol" "$MSG" 31 printf "\\r" 32 sleep $DELAY 33 done 34 35 return $? 36 } 37 38 spinner_start() { 39 tput civis 40 show_spinner "$1" & 41 spinner_pid=$! 42 } 43 44 spinner_stop() { 45 tput cnorm 46 kill "$spinner_pid" &> /dev/null 47 spinner_pid="" 48 } 49 50 # ------------------------------------------------------------------------------ 51 52 get_items() { 53 pushd "${PASSWORD_STORE_DIR:-$HOME/.password-store}" 1>/dev/null || exit 2 54 find . -type f -name '*.gpg' | sed 's/\.gpg//' | sed 's/^\.\///' | sort 55 popd 1>/dev/null || exit 2 56 } 57 58 get_password() { 59 pass show "${1}" | head -n1 60 } 61 62 get_login() { 63 local keys="user login username" 64 local match 65 66 for candidate in $keys; do 67 match=$(pass show "${1}" | grep -i "$candidate" | cut -d ':' -f 2 | xargs) 68 69 if [[ ! -z $match ]]; then break; fi 70 done 71 72 echo "$match" 73 } 74 75 # ------------------------------------------------------------------------------ 76 77 main() { 78 local -r ACTIVE_PANE="$1" 79 80 local items 81 local sel 82 local passwd 83 local login 84 local header='enter=paste, alt-enter=user, ctrl-e=edit, ctrl-d=delete, tab=preview' 85 local preview_hidden 86 local preview_cmd 87 88 if [[ "x$OPT_HIDE_PREVIEW" == "xon" ]]; then 89 preview_hidden='--preview-window=hidden' 90 fi 91 92 if [[ "x$OPT_HIDE_PW_FROM_PREVIEW" == "xon" ]]; then 93 preview_cmd='pass show {} | tail -n+2' 94 else 95 preview_cmd='pass show {}' 96 fi 97 98 spinner_start "Fetching items" 99 items="$(get_items)" 100 spinner_stop 101 102 sel="$(echo "$items" | \ 103 fzf \ 104 --inline-info --no-multi \ 105 --tiebreak=begin \ 106 --preview="$preview_cmd" \ 107 $preview_hidden \ 108 --bind=tab:toggle-preview \ 109 --header="$header" \ 110 --bind=alt-enter:accept \ 111 --expect=enter,ctrl-e,ctrl-d,ctrl-c,esc,alt-enter)" 112 113 if [ $? -gt 0 ]; then 114 echo "error: unable to complete command - check/report errors above" 115 echo "You can also set the fzf path in options (see readme)." 116 read -r 117 exit 118 fi 119 120 key=$(head -1 <<< "$sel") 121 text=$(tail -n +2 <<< "$sel") 122 123 case $key in 124 125 enter) 126 spinner_start "Fetching password" 127 passwd="$(get_password "$text")" 128 spinner_stop 129 130 if [[ "$OPT_COPY_TO_CLIPBOARD" == "on" ]]; then 131 copy_to_clipboard "$passwd" 132 clear_clipboard 30 133 else 134 tmux send-keys -t "$ACTIVE_PANE" "$passwd" 135 fi 136 ;; 137 138 alt-enter) 139 spinner_start "Fetching username" 140 login="$(get_login "$text")" 141 spinner_stop 142 143 if [[ "$OPT_COPY_TO_CLIPBOARD" == "on" ]]; then 144 copy_to_clipboard "$login" 145 else 146 tmux send-keys -t "$ACTIVE_PANE" "$login" 147 fi 148 ;; 149 150 ctrl-e) 151 pass edit "$text" 152 ;; 153 154 ctrl-d) 155 pass rm "$text" 156 ;; 157 158 esac 159 } 160 161 main "$@"