Skip to content

Commit

Permalink
Simplify Copy function
Browse files Browse the repository at this point in the history
Rewrite for the `Copy` function to improve readability. The main difference is the removal of the "weird" if-statement with the `:`'s in it.
  • Loading branch information
AntonVanAssche committed Mar 14, 2023
1 parent 5fc2a74 commit 1cf96ab
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions bashpass
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,16 @@ Show() {
# Copy the password that was given to the clipboard.
# Clear the clipboard afterwards.
Copy() {
# Detect 'xclip' (X11), 'xsel' (X11) or 'wl-clipboard' (Wayland).
commmand -v xclip &> /dev/null ||
command -v xsel &> /dev/null ||
command -v wl-copy &> /dev/null ||
Kill "xclip, xsel or wl-clipboard (Wayland) is required" "127"
# Detect 'xclip' (X11), 'xsel' (X11) or 'wl-copy' (Wayland).
if command -v xclip &> /dev/null; then
clipboardCommand="xclip -selection clipboard"
elif command -v xsel &> /dev/null; then
clipboardCommand="xsel --clipboard"
elif command -v wl-copy &> /dev/null; then
clipboardCommand="wl-copy"
else
kill "xclip, xsel, or wl-copy is required" 127
fi

# Ask for a name when no name was given using ${2}.
Name "${1}" "copy"
Expand All @@ -269,30 +274,26 @@ Copy() {
# Ignore terminal interrupts (CTRL+C).
trap '' INT

# Decrypte the password file and copy it to the clipboard using xclip or xsel.
# Decrypte the password file and copy it to the clipboard using xclip, xsel or wl-copy.
# This WON'T show the password inside the terminal output.
if gpg --decrypt --quiet "${passLocation}/${name}.gpg" | xclip -selection clipboard 2> /dev/null; then
:
elif gpg --decrypt --quiet "${passLocation}/${name}.gpg" | xsel --clipboard 2> /dev/null; then
:
elif gpg --decrypt --quiet "${passLocation}/${name}.gpg" | wl-copy 2> /dev/null; then
:
if gpg --decrypt --quiet "${passLocation}/${name}.gpg" | ${clipboardCommand}; then
printf 'Password %s has been copied to the clipboard' "'${name}'"
printf '\n'
else
command -v "xclip" &> /dev/null && xclip -selection clipboard < /dev/null
command -v "xsel" &> /dev/null && xsel --clipboard < /dev/null
command -v "wl-copy" &> /dev/null && wl-copy --clear
Kill "failed to copy password '${name}' to the clipboard." "1"
kill "failed to copy password '${name}' to the clipboard." 1
fi

printf 'Password %s has been copied to the clipboard' "'${name}'"
printf '\n'

# Wait for the timer to end clear the clipboard.
# 'timer' can be changed in the config file.
Sleep "${timer}" || kill 0
command -v "xclip" &> /dev/null && xclip -selection clipboard < /dev/null
command -v "xsel" &> /dev/null && xsel --clipboard < /dev/null
command -v "wl-copy" &> /dev/null && wl-copy --clear

if command -v xclip &> /dev/null; then
xclip -selection clipboard < /dev/null
elif command -v xsel &> /dev/null; then
xsel --clipboard < /dev/null
elif command -v wl-copy &> /dev/null; then
wl-copy --clear
fi

printf 'Clipboard has been cleared to ensure it cannot be leaked.'
printf '\n'
Expand Down

0 comments on commit 1cf96ab

Please sign in to comment.