Skip to content

Commit

Permalink
Add suport for xsel
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonVanAssche committed Sep 1, 2022
1 parent 84ddf4b commit 3b55891
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions bashpass
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ PasswordAlreadyExists() {
printf '\n'
printf 'Do you want to overwrite the existing password [y/N]: '
read -r overwrite

case "${overwrite}" in
[Yy])
rm -rf "${passLocation}/${name}.gpg"
Expand Down Expand Up @@ -139,7 +139,7 @@ RandomPassword() {
printf 'Retype the password for %s: ' "${name}"
read -rs password2
printf '\n'

while [[ "${password1}" != "${password2}" ]]; do
printf 'Passwords didn'\''t match, please try again: '
read -rs password2
Expand Down Expand Up @@ -245,8 +245,10 @@ Show() {
# Copy the password that was given to the clipboard.
# Clear the clipboard afterwards.
Copy() {
# Detect 'xclip'
command -v xclip &> /dev/null || Kill "xclip is required" "127"
# Detect 'xclip' or 'xsel'
command -v xclip &> /dev/null ||
command -v xsel &> /dev/null ||
Kill "xclip or xsel is required" "127"

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

# Decrypte the password file and copy it to the clipboard using xclip.
# Decrypte the password file and copy it to the clipboard using xclip or xsel.
# This WON'T show the password inside the terminal output.
if ! printf '%s' "$(gpg --decrypt --quiet "${passLocation}/${name}.gpg")" | xclip -selection clipboard; then
xclip -selection clipboard < /dev/null
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
:
else
command -v xclip &> /dev/null && xclip -selection clipboard < /dev/null
command -v xsel &> /dev/null && xsel --clipboard < /dev/null
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
xclip -selection clipboard < /dev/null

command -v xclip &> /dev/null && xclip -selection clipboard < /dev/null
command -v xsel &> /dev/null && xsel --clipboard < /dev/null

printf 'Clipboard has been cleared to ensure it cannot be leaked.'
printf '\n'
}
Expand All @@ -278,7 +286,7 @@ Copy() {
List() {
# Enable globbing for only this function.
set +f

# Print the list of all the passwords (without using the `ls` command).
for p in "${passLocation}"/*.gpg; do
# Remove the path to the file.
Expand Down

0 comments on commit 3b55891

Please sign in to comment.