summaryrefslogtreecommitdiff
path: root/bin/icat
blob: d363e09889d7573cd96ce0821eb067f47b92bcbc (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
#!/bin/sh -e
# Display images directly in terminal.
# Tested with xterm v361. Depends on imagemagick convert(1).

[ "$LC_TERMINAL" = iTerm2 ] && [ -x "$HOME/.iterm2/imgcat" ] && exec "$HOME/.iterm2/imgcat" "$@"

# maxsize prints the geomtry size of terminal window, with
# a maximum value of 1000 pixels for width and height, or
# 640x480 if terminal size can not be probed.
maxsize() {
	[ "$WINDOWID" ] || { echo "640x480>"; return; }
	xwininfo -id "$WINDOWID" | {
		w=1000 h=1000
		while read -r a b; do
			case $a in
			(Width:)  [ $((b < w)) = 1 ] && w=$b ;;
			(Height:) [ $((b < h)) = 1 ] && h=$b  ;;
			esac
		done
		echo "${w}x$h"
	}
}

clearline() { printf "\r                                                     \r"; }

ms=$(maxsize)

# Use convert(1) from imagemagick to output sixel format.
# -flatten uses the terminal background color as transparent layer
# -resize use the terminal size as bounding box, with a limit of 1000 pixels
# image is not resized if smaller than limit
#exec convert -resize "$(maxsize)>" -transparent-color white -flatten -- ${1:--} sixel:-
# color #rrrrggggbbbb
#exec convert -resize "$(maxsize)>" -- ${1:--} sixel:${2:--}

put=:
while getopts :cpv opt; do
	case $opt in
	(c) clear=1 ;;
	(p) pause=1 put=printf ;;
	(v) put=printf ;;
	(*) echo "Usage: icat [-cpv] [image_file ...]"; exit 1 ;;
	esac
done
shift $((OPTIND - 1))

[ "$#" = 0 -o "$#" = 1 -a "$1" = - ] && {
	convert -resize "$ms>" - sixel:-
	exit
}

for f; do
	$put "Next: %s" "$f"
	[ "$pause" ] && {
		read -sr
		[ "$REPLY" = s ] && clearline && continue
	}
	[ "$clear" ] && clear
	clearline
	$put "%s\n" "$f"
	convert -resize "$ms>" -- "$f" sixel:-
done