-
Notifications
You must be signed in to change notification settings - Fork 0
/
yank
executable file
·102 lines (89 loc) · 2.93 KB
/
yank
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/sh
# shellcheck disable=2009
set -ue
usage() {
printf 'yank: usage: yank [-p]\n'
printf 'flags:\n'
printf ' -p, --primary yanks to the system primary selection instead if available on the platform\n'
printf ' --help show usage\n'
}
if_debug() {
[ -n "${DEBUG-}" ]
}
info() {
printf 'yank: %s\n' "${1}" >&2
}
# parse args
primary=
while :; do
case "${1-}" in
-p|--primary) primary='p' ;;
--help) usage && exit ;;
-?*) # unknown option
info "ERROR: unknown option: ${1}" && exit 1
;;
*) break ;;
esac
shift
done
# prepare the data - stdin or a file
file="${1:-"/dev/stdin"}"
tmpfile="$(mktemp)"
cat "${file}" >"${tmpfile}"
file="${tmpfile}"
# cleanup temp file for security reasons
# shellcheck disable=2064
trap 'rm -f "${file}"' 0 1 2 3 15
is_tty=
is_x11=
is_wayland=
is_termux=
[ -t 0 ] || [ -t 1 ] || [ -t 2 ] && is_tty=true
[ "${XDG_SESSION_TYPE-}" = 'X11' ] && [ -n "${DISPLAY-}" ] && is_x11=true
[ "${XDG_SESSION_TYPE-}" = 'wayland' ] && [ -n "${WAYLAND_DISPLAY-}" ] && is_wayland=true
[ "$(ps -ef | grep -c 'com.termux')" -gt 1 ] && is_termux=true
if_debug && info "tty='${is_tty}' x11='${is_x11}' wayland='${is_wayland}' termux='${is_termux}'"
# exit 1 if we can't yank on this system in this form
[ "${is_tty}" != true ] &&
[ "${is_x11}" != true ] &&
[ "${is_wayland}" != true ] &&
[ "${is_termux}" != true ] &&
exit 1
convert_mime_type() {
# convert the file if it is image - we can't yank jpeg for some reason
case "${mime_type}" in
image/png) ;;
image/*)
exec 3<>"${file}"
rm -f "${file}"
file="$(mktemp)"
convert - png:- <&3 >"${file}"
mime_type='image/png'
;;
*)
# maybe the following ruining some things (like formatting of text)?
# maybe i want it to ruin it?
# but is this script for me, or potentially for other people? who knows.
# mime_type='text/plain'
;;
esac
}
# Determine (and convert) mime type -
# use it later if supported by the platform
mime_type="$(file --mime-type -b '-' <"${file}")"
[ "${is_tty}" != true ] && [ "${is_termux}" != true ] && convert_mime_type
# FIXME: osc52 variant currently tty only supports 'text/plain'
if_debug && info "yanking to: ${primary:-c}" && set -x
if [ "${is_tty}" = true ] && [ "${mime_type}" = 'text/plain' ]; then
if_debug && info 'yanking with: osc52' && set -x
printf "\033]52;%s;%s\a" "${primary:-"c"}" "$(cat "${file}" | base64)"
elif [ "${is_wayland}" = true ]; then
if_debug && info 'yanking with: wayland' && set -x
wl-copy ${primary:+"--primary"} --trim-newline --type "${mime_type}" <"${file}"
elif [ "${is_x11}" = true ]; then
if_debug && info 'yanking with: x11' && set -x
xclip -rmlastnl -t "${mime_type}" -in -selection "${primary:-"clipboard"}" <"${file}"
elif [ "${is_termux}" = true ]; then
if_debug && info 'yanking with: termux' && set -x
[ -z "${primary}" ] && printf '%s' "$(cat "${file}")" | termux-clipboard-set
fi