-
Notifications
You must be signed in to change notification settings - Fork 34
/
totty
executable file
·38 lines (32 loc) · 933 Bytes
/
totty
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
#!/bin/bash
usage() {
bin=$(basename $0)
echo >&2 "Usage: $bin image_path_or_url [path_or_url...]"
echo >&2 "Usage: TOTTY_X=n $bin ..."
echo >&2
echo >&2 "Display image or url in a terminal using img2xterm."
echo >&2 "Optional TOTTY_X env var allows to"
echo >&2 " specify terminal width, instead of using stty to get it."
exit ${1:-0}
}
[[ "$1" = -h || "$1" = --help ]] && usage
dst=/tmp/media.img2tty.png
if [[ -z "$TOTTY_X" ]]
then read tty_rows tty_cols < <(stty size)
else tty_cols=$TOTTY_X
fi
err=0
for p in "$@"; do
[[ "$p" =~ https?:// ]] && {
curl -s "$p" >"$dst"\
|| { echo >&2 "Failed to fetch url: $p"; err=1; continue; }
p=$dst
}
ct=$(file --brief --mime-type "$p")
[[ "$ct" =~ ^image/ ]]\
|| { echo >&2 "Unknown content type ($ct): $p"; err=1; continue; }
[[ "$#" -gt 1 ]] && echo "--- $p"
convert "$p" -resize "${tty_cols}"x "$dst" && img2xterm "$dst" || err=1
done
rm -f "$dst"
exit $err