aboutsummaryrefslogtreecommitdiff
path: root/.local/share/bin/preview
blob: 819938b7c16c85181ad3d98825de7cb73b1c004f (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
[ -n "$1" ] && FILE="$1" || {
	echo "error: no input file"
	exit 1
}
[ -n "$2" ] && WIDTH="$(( "$2" - 1 ))" || WIDTH="$(tput cols)"
[ -n "$3" ] && HEIGHT="$3" || HEIGHT="$(tput lines)"
[ -n "$4" ] && POS_X="$4" || POS_X="0"
[ -n "$5" ] && POS_Y="$5" || POS_Y="0"

MIMETYPE="$(file --mime-type -Lb "$FILE")"
EXT="${FILE#*.}"

if [ "$MIMETYPE" = "text/plain" ] ; then
	[ "$EXT" = "md" ] && MIMETYPE="text/markdown"
	[ "$EXT" = "rst" ] && MIMETYPE="text/x-rst"
fi
[ "$MIMETYPE" = "application/javascript" ] && MIMETYPE="text/javascript"

render_manpage() {
	exec groff -T utf8 -m man -rcR=1 -rIN=0 -rLL="${WIDTH}n" << EOF
.nr an-suppress-header-and-footer 1
.TH
$(cat | preconv)
EOF
}

case "$MIMETYPE" in
	image/*)
		if [ -z "$UB_SOCKET" ] ; then
			echo "ueberzug not available"
		else
			ueberzugpp cmd \
				--socket "$UB_SOCKET" \
				--action add \
				--identifier PREVIEW \
				--file "$FILE" \
				--max-width "$WIDTH" --max-height "$HEIGHT" \
				--xpos "$POS_X" --ypos "$POS_Y"
			exit 1 # <- disable cache
		fi
		;;
	text/markdown)
		pandoc --from=gfm --to=man "$FILE" | render_manpage
		;;
	text/x-rst)
		pandoc --from=rst --to=man "$FILE" | render_manpage
		;;
	text/*)
		bat \
			--terminal-width="$WIDTH" \
			--paging=never \
			--color=always \
			-- "$FILE"
		;;
	application/x-tar|\
	application/x-bzip|\
	application/x-bzip2|\
	application/gzip|\
	application/zip|\
	application/x-7z-compressed|\
	application/vnd.rar)
		bsdtar -tf "$FILE"
		;;
	application/json)
		jq --color-output . "$FILE"
		;;
	*)
		echo "$MIMETYPE"
		file -b "$FILE" | fold --width="$WIDTH" --spaces
		;;
esac

exit 0