-
Notifications
You must be signed in to change notification settings - Fork 10
/
showqr
executable file
·75 lines (61 loc) · 2.17 KB
/
showqr
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
#!/bin/bash
# Copyright 2015--2023 Raphael Reitzig
#
# 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/>
# Requires: qrencode, display, xsel
# For tiling workaround under i3, also: wmctrl, awk
# Shows text found as CLI parameters as a QR-code.
# Pass `-` to read from STDIN.
# If no parameters are given, reads from clipboard.
set -eu
set -o pipefail
f=`mktemp --suff=.png`;
# Convert clipboarde, STDIN resp. first parameter
if [ $# -eq 0 ]; then
qrencode -o "${f}" "$(xsel -b)"
elif [ "${1}" == "-" ]; then
qrencode -o "${f}" < /dev/stdin
else
qrencode -o "${f}" "$*";
fi
# Open the QR code for viewing
display -size "1000>x1000>" "${f}" &
pid="$!"
# If i3 is active, trickery to get a floating viewer
# instead of madness (cf. https://github.com/ImageMagick/ImageMagick/issues/1213).
if wmctrl -m | grep "Name: i3" > /dev/null; then
# cf. https://unix.stackexchange.com/a/474300/17409
# Can't search for the PID (display doesn't tell it to wmctrl).
# Turns out the window name is
#
# ImageMagick: tmp.XXXX.png
#
# so we can use that to search for the window ID.
# We use only the filename which ends up in its own column in awk.
winname="`basename "${f}" | sed -e 's/\\//./'`"
# Wait for the window to open
winid=''
while : ; do
winid="`wmctrl -l | awk -vwinname="${winname}" '$5==winname {print $1; exit}'`"
[[ -z "${winid}" ]] || break
done
# Focus the window
wmctrl -ia "${winid}"
# Make it float
i3-msg floating enable > /dev/null;
i3-msg move position center > /dev/null;
fi
# Wait for the user to close display and clean up
wait "${pid}";
rm ${f};