From a711d042266e358c73e17061fac9abd9c7ed2582 Mon Sep 17 00:00:00 2001 From: zawz Date: Sun, 15 Nov 2020 21:08:17 +0100 Subject: [PATCH] zpass: add clipboard wayland support --- zpass/src/clipboard.sh | 45 ++++++++++++++++++++++++++++++++++++++++++ zpass/src/operation.sh | 3 ++- zpass/src/util.sh | 22 ++++++--------------- 3 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 zpass/src/clipboard.sh diff --git a/zpass/src/clipboard.sh b/zpass/src/clipboard.sh new file mode 100644 index 0000000..8db7bf8 --- /dev/null +++ b/zpass/src/clipboard.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +clipboard() +{ + unset in + read -r in + while read -r ln + do + in="$in +$ln" + done + printf "%s" "$in" | xclip -selection clipboard + which wl-copy >/dev/null 2>&1 && printf "%s" "$in" | wl-copy +} + +# $1 = delay in sec +clipboard_clear() { + if [ -n "$1" ] + then + for I in $(screen -ls | grep "$fname"_clipboard | awk '{print $1}') + do + screen -S "$I" -X stuff "^C" + done + screen -dmS "$fname"_clipboard sh -c "sleep $1 + xclip -selection clipboard < /dev/null + which wl-copy 2>&1 && wl-copy < /dev/null + sleep 1" + else + echo | clipboard + fi +} + +copy_check() +{ + if ps -e | grep -qi wayland + then + which wl-copy >/dev/null 2>&1 || error 1 "ERROR: running wayland but wl-clipboard is not installed" + elif [ -n "$DISPLAY" ] + then + which xclip >/dev/null 2>&1 || error 1 "ERROR: running X but xclip is not installed" + else + error 1 "ERROR: no graphical server detected" + fi + return 0 +} diff --git a/zpass/src/operation.sh b/zpass/src/operation.sh index 1cd8568..3aec44e 100644 --- a/zpass/src/operation.sh +++ b/zpass/src/operation.sh @@ -37,7 +37,8 @@ get() # $1 = path copy() { - { get "$1" || return $?; } | tr -d '\n' | xclip -selection clipboard && clipboard_clear "$ZPASS_CLIPBOARD_TIME" + copy_check || return $? + { get "$1" || return $?; } | remove_trailing_newline | clipboard && clipboard_clear "$ZPASS_CLIPBOARD_TIME" } # $@ = path diff --git a/zpass/src/util.sh b/zpass/src/util.sh index 542de33..b1cbf40 100644 --- a/zpass/src/util.sh +++ b/zpass/src/util.sh @@ -13,13 +13,17 @@ escape_chars() { echo "$*" | sed 's|\.|\\\.|g;s|/|\\/|g' } +remove_trailing_newline() { + awk 'NR>1{print PREV} {PREV=$0} END{printf("%s",$0)}' +} + # $@ = paths sanitize_paths() { for N do - echo "$N" | grep "^/" && echo "Path cannot start with /" >&2 && return 1 - echo "$N" | grep -w ".." && echo "Path cannot contain .." >&2 && return 1 + echo "$N" | grep -q "^/" && echo "Path cannot start with /" >&2 && return 1 + echo "$N" | grep -qw ".." && echo "Path cannot contain .." >&2 && return 1 done return 0 } @@ -54,17 +58,3 @@ _cmd_() { sh -c "$*" fi } - -# $1 = delay in sec -clipboard_clear() { - if [ -n "$1" ] - then - for I in $(screen -ls | grep "$fname"_clipboard | awk '{print $1}') - do - screen -S "$I" -X stuff "^C" - done - screen -dmS "$fname"_clipboard sh -c "sleep $1 ; xclip -selection clipboard < /dev/null ; sleep 1" # put empty string into clipboard - else - xclip -selection clipboard < /dev/null - fi -}