aboutsummaryrefslogtreecommitdiff
path: root/vis-clipboard
blob: 0f9e90bd24f38d1657710e387838ebac17cff27a (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/sh
# Copyright (C) 2016 Richard Burke, ISC licensed
# shellcheck disable=SC2317
set -e

vc_fatal() {
	echo "$@" >&2
	exit 1
}

vc_usage() {
	vc_fatal "Usage: $(basename "$0") [--selection sel] [--usable|--copy|--paste]

Copy/paste clipboard interface with support on all provided platforms.

Available selections: clipboard, primary

Example: $(basename "$0") --copy --selection primary"
}

vc_determine_command() {
	if [ -n "$WAYLAND_DISPLAY" ]; then
		for c in wl-copy wl-paste; do
			if command -v "$c" >/dev/null 2>&1; then
				echo "wlclipboard"
				return 0
			fi
		done

		for c in waycopy waypaste; do
			if command -v "$c" >/dev/null 2>&1; then
				echo "wayclip"
				return 0
			fi
		done
	fi

	if [ -n "$DISPLAY" ]; then
		for c in xclip xsel; do
			if command -v "$c" >/dev/null 2>&1; then
				echo "$c"
				return 0
			fi
		done
	fi

	if command -v pbcopy >/dev/null 2>&1; then
		echo 'mac'
		return 0
	fi

	if [ -c /dev/clipboard ]; then
		echo 'cygwin'
		return 0
	fi

	return 1
}

vc_usable() {
	if vc_determine_command >/dev/null 2>&1; then
		exit 0
	fi

	exit 1
}

vc_copy() {
	COPY_CMD="$(vc_determine_command 2>/dev/null)"

	# shellcheck disable=SC2181
	if [ $? -ne 0 ] || [ -z "$COPY_CMD" ]; then
		vc_fatal 'System clipboard not supported'
	fi

	"vc_${COPY_CMD}_copy"

	exit $?
}

vc_paste() {
	PASTE_CMD="$(vc_determine_command 2>/dev/null)"

	# shellcheck disable=SC2181
	if [ $? -ne 0 ] || [ -z "$PASTE_CMD" ]; then
		vc_fatal 'System clipboard not supported'
	fi

	"vc_${PASTE_CMD}_paste"

	exit $?
}

vc_wlclipboard_copy() {
	if [ "$sel" = "primary" ]; then
		wl-copy --primary -t TEXT 2>/dev/null
	else
		wl-copy -t TEXT 2>/dev/null
	fi
}

vc_wlclipboard_paste() {
	if [ "$sel" = "primary" ]; then
		wl-paste --no-newline --primary -t text
	else
		wl-paste --no-newline -t text
	fi
}

vc_wayclip_copy() {
	if [ "$sel" = "primary" ]; then
		waycopy -p
	else
		waycopy
	fi
}

vc_wayclip_paste() {
	if [ "$sel" = "primary" ]; then
		waypaste -p
	else
		waypaste
	fi
}

vc_xsel_copy() {
	xsel --"$sel" -i
}

vc_xsel_paste() {
	xsel --"$sel" -o
}

vc_xclip_copy() {
	xclip -selection "$sel" -i >/dev/null 2>&1
}

vc_xclip_paste() {
	xclip -selection "$sel" -o
}

vc_mac_copy() {
	pbcopy
}

vc_mac_paste() {
	pbpaste
}

vc_cygwin_copy() {
	cat >/dev/clipboard
}

vc_cygwin_paste() {
	cat /dev/clipboard
}

while [ $# -gt 0 ]; do
	case "$1" in
		--usable) fn=vc_usable;;
		--copy) fn=vc_copy;;
		--paste) fn=vc_paste;;
		--selection)
			shift
			if [ "$1" != "clipboard" ] && [ "$1" != "primary" ]; then
				vc_fatal "Invalid selection: $1\nValid options are 'clipboard' or 'primary'"
			fi
			sel="$1";;
		*) vc_usage;;
	esac
	shift
done

sel=${sel:-"clipboard"} $fn

vc_usage