commit b0839699a40baa9c0f0b97e5c9153595ae155cf4
parent 3ae1e0b9d9984e26ec660bb84c4ef19e1b699b7e
Author: St John Karp <contact.stjo.hn>
Date: Thu, 9 Jun 2022 16:58:45 -0400
Support wl-copy instead of xclip on Wayland systems
Implemented a check to see whether $XDG_SESSION_TYPE equals x11.
If so, use xclip to copy passwords to the clipboard. Otherwise use wl-copy.
Diffstat:
2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/README b/README
@@ -18,8 +18,8 @@ Why is it called Balrog? Because "you shall not pass." Haaaaah.
* A POSIX shell. The `balrog` script is strictly POSIX compliant to
ensure maximum portability.
-* [xclip](https://github.com/astrand/xclip). Copies the password to the
- clipboard.
+* [xclip](https://github.com/astrand/xclip) on X11 or wl-copy on Wayland.
+ Copies the password to the clipboard.
* [OATH Toolkit](https://www.nongnu.org/oath-toolkit/) (optional). Used for
handling one-time passwords.
diff --git a/balrog b/balrog
@@ -11,6 +11,11 @@ INPLACE=0
CHARACTERS='a-zA-Z0-9!"#$%&'\''()*+,-./:;<=>?@[]^_‘{|}~\\'
TMP_FILE="$HOME/.balrogtmp"
+# Identify the system's clipboard utility.
+[ "$XDG_SESSION_TYPE" = 'x11' ] &&
+ CLIP='xclip -selection clipboard' ||
+ CLIP='wl-copy'
+
# Loop through all the arguments and set flags/options.
while [ "$#" -gt 0 ] ; do
case "$1" in
@@ -121,21 +126,21 @@ while [ -n "$ACTION" ] ; do
grep 'otpauth' |
sed 's/.*secret=\([a-zA-Z0-9]*\).*/\1/' |
oathtool --base32 --totp - |
- ([ "$COPY" -eq 1 ] && xclip -selection clipboard || cat)
+ ([ "$COPY" -eq 1 ] && $CLIP || cat)
# Decrypt and get the first line.
else
gpg2 --decrypt --quiet "$KEY_FILE" |
([ "$COPY" -eq 1 ] &&
head -n 1 |
tr -d '\n' |
- xclip -selection clipboard ||
+ $CLIP ||
cat)
fi
# Launch a background job to clear the clipboard in 30 seconds.
[ "$COPY" -eq 1 ] &&
sleep 30 &&
- xclip -selection clipboard < /dev/null &
+ $CLIP < /dev/null &
ACTION=''
;;