summaryrefslogtreecommitdiff
path: root/scripts/gui/sclip
blob: 3d2caa54c3375f62442d2b12c0fd7ff0551dacc5 (plain)
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
#!/bin/sh

. slib

help() {
cat << EOF
${0}: print the clipboard content
options:
 -h              Print this message and exit
 -c [text]       Copy text to clipboard
 -f [file]       Copy content of file into clipboard
 -i              Copy standart input into clipboard
EOF

exit 0
}

check_program "xclip"

i_flg=0
silent=0
while getopts "f:c:sih" option; do
	case "${option}" in
	f) filepath="${OPTARG}" ;;

	c) input="${OPTARG}" ;;

	s) silent=1 ;;

	i) i_flg=1 ;;

	h) help ;;

	*) invalid_use -h ;;
	esac
done

shift $((OPTIND - 1))

[ "${#}" -ne 0 ] && invalid_use

if [ -n "${filepath}" ]; then
	mime_type=$(file --mime-type -b "${filepath}")

	if [ "${silent}" -eq 1 ]; then
		run "xclip -selection clipboard -t ${mime_type} -i ${filepath}"
	fi

	run --success-notify "${filepath} copied" \
		--failure-notify "${filepath} failed to copy" \
		"xclip -selection clipboard -t ${mime_type} -i ${filepath}"
fi

[ "${i_flg}" -eq 1 ] && input=$(cat)

if [ -n "${input}" ]; then
	# Don't use `run` since pipes are used
	printf '%s' "${input}" | xclip -selection clipboard

	if [ $? -eq 0 ]; then
		[ "${silent}" -eq 0 ] && send_notification "${argv0}:" "'${input}' copied"
		exit 0
	fi

	[ "${silent}" -eq 0 ] && send_notification "${argv0}:" "'${input}' failed to copy"

	exit 1
fi

run "xclip -o -selection clipboard"