improve copy/type defaulting logic

This commit is contained in:
Joel Beckmeyer 2022-10-28 13:06:53 -04:00
parent 5fc2f0a242
commit 869c5450bd
1 changed files with 46 additions and 22 deletions

View File

@ -6,6 +6,7 @@ autotype=0
copyisset=0 copyisset=0
fileisuser=0 fileisuser=0
help=0 help=0
onlypassword=0
squash=0 squash=0
typeisset=0 typeisset=0
@ -75,11 +76,11 @@ _pass_get() {
_usage() { _usage() {
printf "Usage: wofi-pass [options]\n" printf "Usage: wofi-pass [options]\n"
printf " -a, --autotype autotype whatever entry is chosen\n" printf " -a, --autotype autotype whatever entry is chosen\n"
printf " -c, --copy [cmd] copy to clipboard. Defaults to wl-copy if no cmd is given.\n" printf " -c, --copy=[cmd] copy to clipboard. Defaults to wl-copy if no cmd is given.\n"
printf " -f, --fileisuser use the name of the password file as username\n" printf " -f, --fileisuser use the name of the password file as username\n"
printf " -h, --help show this help message\n" printf " -h, --help show this help message\n"
printf " -s, --squash don't show field choice if password file only contains password\n" printf " -s, --squash don't show field choice if password file only contains password\n"
printf " -t, --type [cmd] type the selection instead of copying to clipboard.\n" printf " -t, --type=[cmd] type the selection instead of copying to clipboard.\n"
printf " Defaults to wtype if no cmd is given.\n" printf " Defaults to wtype if no cmd is given.\n"
} }
@ -88,25 +89,41 @@ eval set -- "$OPTS"
while true; do while true; do
case "$1" in case "$1" in
-a | --autotype ) autotype=1; shift ;; -a | --autotype ) autotype=1; shift ;;
-c | --copy ) -c ) copyisset=1; copy_cmd="$COPY_CMD"; shift ;;
--copy )
copyisset=1 copyisset=1
if [ -z "$2" ]; then if [ -n "$2" ]; then
copy_cmd="$COPY_CMD" if command -v "$2"; then
echo "$2"
copy_cmd="$2"
else
printf "Cannot find %s in path, defaulting to %s.\n" "$2" "$COPY_CMD"
copy_cmd="$COPY_CMD"
shift 2
fi
else else
copy_cmd="$2" copy_cmd="$COPY_CMD"
fi shift
shift 2;; fi ;;
-f | --fileisuser ) fileisuser=1; shift;; -f | --fileisuser ) fileisuser=1; shift;;
-h | --help ) help=1; shift ;; -h | --help ) help=1; shift ;;
-s | --squash ) squash=1; shift ;; -s | --squash ) squash=1; shift ;;
-t | --type ) -t ) typeisset=1; type_cmd="$TYPE_CMD"; shift ;;
--type )
typeisset=1 typeisset=1
if [ -z "$2" ]; then if [ -n "$2" ]; then
type_cmd="$TYPE_CMD" if command -v "$2"; then
echo "$2"
type_cmd="$2"
else
printf "Cannot find %s in path, defaulting to %s.\n" "$2" "$TYPE_CMD"
type_cmd="$TYPE_CMD"
shift 2
fi
else else
type_cmd="$2" type_cmd="$TYPE_CMD"
fi shift
shift 2;; fi ;;
-- ) shift; break;; -- ) shift; break;;
* ) break;; * ) break;;
esac esac
@ -139,10 +156,18 @@ field_list="$(_parse_fields)"
field_count="$(printf '%s' "$field_list" | wc -l)" field_count="$(printf '%s' "$field_list" | wc -l)"
if [ "$squash" -eq 1 ] && [ "$field_count" -eq 0 ]; then if [ "$squash" -eq 1 ] && [ "$field_count" -eq 0 ]; then
field="password" field="password"
onlypassword=1
elif [ "$autotype" -ne 1 ]; then elif [ "$autotype" -ne 1 ]; then
field=$(printf '%s\n' "$field_list" | wofi --dmenu) field=$(printf '%s\n' "$field_list" | wofi --dmenu)
fi fi
# get the command to output to
if [ "$typeisset" -eq 1 ]; then
output_cmd=$type_cmd
else
output_cmd=$copy_cmd
fi
if [ "$autotype" -eq 1 ] || [ "$field" = "autotype" ]; then if [ "$autotype" -eq 1 ] || [ "$field" = "autotype" ]; then
if [ "$fileisuser" -eq 1 ]; then if [ "$fileisuser" -eq 1 ]; then
username="$passname" username="$passname"
@ -150,15 +175,14 @@ if [ "$autotype" -eq 1 ] || [ "$field" = "autotype" ]; then
username=$(_pass_get "username") username=$(_pass_get "username")
fi fi
password=$(_pass_get "password") password=$(_pass_get "password")
if [ "$typeisset" -eq 1 ]; then
printf '%s\t%s\n' "$username" "$password" | $type_cmd # check if we are autotyping a password-only file
if [ "$onlypassword" -eq 1 ]; then
printf '%s\n' "$password" | $output_cmd
else else
printf '%s\t%s\n' "$username" "$password" | $copy_cmd printf '%s\t%s\n' "$username" "$password" | $output_cmd
fi fi
else else
if [ "$typeisset" -eq 1 ]; then _pass_get "$field" | $output_cmd
_pass_get "$field" | $type_cmd
else
_pass_get "$field" | $copy_cmd
fi
fi fi