[PATCH 2/6] Decide what clipboard to use in `clip()`.

Doron Behar doron.behar at gmail.com
Wed Feb 7 20:39:09 CET 2018


---
 src/password-store.sh | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index ebd4a35..24b5003 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -150,16 +150,37 @@ check_sneaky_paths() {
 #
 
 clip() {
+	# Decide what clipboard to use
+	case "${PASSWORD_STORE_DEFAULT_X_SELECTION}" in
+		"")
+			default_x_selection="clipboard"
+			;;
+		clipboard|primary|CLIPBOARD|PRIMARY)
+			default_x_selection="${PASSWORD_STORE_DEFAULT_X_SELECTION,,}"
+			;;
+		*)
+			die "The defined default clipboard selection ${PASSWORD_STORE_DEFAULT_X_SELECTION} isn\'t supported."
+			;;
+	esac
+	if [[ "$1" -eq 1 ]]; then
+		if [[ "$default_x_selection" == "primary" ]]; then
+			x_selection=clipboard
+		elif [[ "$default_x_selection" == "clipboard" ]]; then
+			x_selection=primary
+		fi
+	else
+		x_selection=${default_x_selection}
+	fi
 	# This base64 business is because bash cannot store binary data in a shell
 	# variable. Specifically, it cannot store nulls nor (non-trivally) store
 	# trailing new lines.
 	local sleep_argv0="password store sleep on display $DISPLAY"
 	pkill -f "^$sleep_argv0" 2>/dev/null && sleep 0.5
-	local before="$(xclip -o -selection "$X_SELECTION" 2>/dev/null | base64)"
-	echo -n "$1" | xclip -selection "$X_SELECTION" || die "Error: Could not copy data to the clipboard"
+	local before="$(xclip -o -selection "$x_selection" 2>/dev/null | base64)"
+	echo -n "$2" | xclip -selection "$x_selection" || die "Error: Could not copy data to the clipboard"
 	(
 		( exec -a "$sleep_argv0" bash <<<"trap 'kill %1' TERM; sleep '$CLIP_TIME' & wait" )
-		local now="$(xclip -o -selection "$X_SELECTION" | base64)"
+		local now="$(xclip -o -selection "$x_selection" | base64)"
 		[[ $now != $(echo -n "$1" | base64) ]] && before="$now"
 
 		# It might be nice to programatically check to see if klipper exists,
@@ -171,9 +192,9 @@ clip() {
 		# so we axe it here:
 		qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory &>/dev/null
 
-		echo "$before" | base64 -d | xclip -selection "$X_SELECTION"
+		echo "$before" | base64 -d | xclip -selection "$x_selection"
 	) 2>/dev/null & disown
-	echo "Copied $2 to clipboard. Will clear in $CLIP_TIME seconds."
+	echo "Copied $3 to clipboard. Will clear in $CLIP_TIME seconds."
 }
 
 qrcode() {
@@ -367,7 +388,7 @@ cmd_show() {
 			local pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | tail -n +${selected_line} | head -n 1)"
 			[[ -n $pass ]] || die "There is no password to put on the clipboard at line ${selected_line}."
 			if [[ $clip -eq 1 ]]; then
-				clip "$pass" "$path"
+				clip "$nextclip" "$pass" "$path"
 			elif [[ $qrcode -eq 1 ]]; then
 				qrcode "$pass" "$path"
 			fi
@@ -530,7 +551,7 @@ cmd_generate() {
 	git_add_file "$passfile" "$verb generated password for ${path}."
 
 	if [[ $clip -eq 1 ]]; then
-		clip "$pass" "$path"
+		clip "$nextclip" "$pass" "$path"
 	elif [[ $qrcode -eq 1 ]]; then
 		qrcode "$pass" "$path"
 	else
-- 
2.16.1




More information about the Password-Store mailing list