-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcliptex
executable file
·126 lines (107 loc) · 2.72 KB
/
cliptex
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
#!/bin/bash
# This file is part of pnglatex <https://github.com/mneri/pnglatex>.
# Copyright Massimo Neri <hello@mneri.me> and all the contributors.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
HELP=
SESSION=
SILENT=0
SHOWVERSION=
declare -r VERSION=0.13
function failure {
if [ ! -t 1 ]; then
notify-send -i image-missing "pnglatex" "Could not generate the image: invalid formula"
fi
}
function generate {
local TMPFILE
if [ -z "$SESSION" ]; then
SESSION=$(session)
fi
if [ "$SESSION" != "wayland" ] && [ "$SESSION" != "x11" ]; then
echo "Unsupported session: $SESSION" >&2
exit 1
fi
if [ "$SESSION" = "wayland" ]; then
TMPFILE=$(wl-paste | pnglatex)
wl-copy < "$TMPFILE" > /dev/null 2>&1
elif [ "$SESSION" = "x11" ]; then
TMPFILE=$(xclip -o | pnglatex)
xclip -selection clipboard -t image/png -i "$TMPFILE" > /dev/null 2>&1
fi
if [ -f "$TMPFILE" ]; then
success
rm "$TMPFILE"
else
failure
exit 1
fi
}
function main {
parse "$@"
if [ "$HELP" = 1 ]; then
usage
exit 0
fi
if [ "$SHOWVERSION" = 1 ]; then
version
exit 0
fi
generate
}
function parse {
while getopts hs:Sv ARG; do
case $ARG in
h)
HELP=1
;;
s)
SESSION=$OPTARG
;;
S)
SILENT=1
;;
v)
SHOWVERSION=1
;;
?)
exit 1
esac
done
}
function session {
loginctl | grep $(id -u) | awk '/tty/ {print $1}' | xargs loginctl show-session -p Type | awk -F= '{print $2}'
}
function success {
if [ "$SILENT" == 0 ] && [ ! -t 1 ]; then
notify-send -i image-x-generic "pnglatex" "Image copied to the system clipboard"
fi
}
function usage {
echo "cliptex $VERSION - Generate a formula from the system clipboard."
echo "Copyright Massimo Neri <hello@mneri.me> and all the contributors."
echo
echo "List of options:"
echo " -h Print this help message."
echo " -s <session> Force a session (wayland or x11)."
echo " -S Don't show system notifications."
echo " -v Show version."
echo
echo "Examples:"
echo " cliptex"
echo " cliptex -s x11"
}
function version {
echo $VERSION
}
main "$@"