summaryrefslogtreecommitdiff
path: root/scripts/gui/sclip
blob: dc692f95a5b467c6a0265489308fcf46be28f4e1 (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
71
72
73
74
75
#!/bin/sh

. "lib_common.sh"
. "lib_handle.sh"

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

clipboard_handle check_program

if [ "${#}" -eq 0 ]; then
	clipboard_handle get-clipboard
	exit $?
fi

read_stdin=0
silent=0
while getopts "f:c:sih" option; do
	case "${option}" in
	f)
		filepath="${OPTARG}"
		[ -f "${filepath}" ] || err "Not a file"
	;;
	c) input="${OPTARG}" ;;
	s) silent=1 ;;
	i) read_stdin=1 ;;
	h) help ;;

	*) invalid_use -h ;;
	esac
done
shift $((OPTIND - 1))

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

if [ -n "${filepath}" ]; then
	mime_type=$(file_handle get-mime_type "$filepath") || {
		err "Failed to detect mime type"; exit 1;}

	clipboard_handle file "${mime_type}" "${filepath}"
	status=$?

else
	[ -z "$input" ] && [ "$read_stdin" -eq 1 ] && input=$(cat)
	[ -z "$input" ] && err "Empty input"

	printf '%s' "${input}" | clipboard_handle text
	status=$?
fi

if [ "${silent}" -eq 0 ]; then
	if [ "${status}" -eq 0 ]; then
		if [ -n "${filepath}" ]; then
			notify_handle send 5 "Copied file: ${filepath}"
		else
			notify_handle send 5 "Copied text"
		fi
	else
		if [ -n "${filepath}" ]; then
			notify_handle send 5 "Failed to copy file: ${filepath}"
		else
			notify_handle send 5 "Failed to copy text"
		fi
	fi
fi