zpass: add clipboard wayland support

This commit is contained in:
zawz 2020-11-15 21:08:17 +01:00
parent eb2f658c27
commit a711d04226
3 changed files with 53 additions and 17 deletions

45
zpass/src/clipboard.sh Normal file
View file

@ -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
}

View file

@ -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

View file

@ -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
}