aboutsummaryrefslogtreecommitdiff
path: root/.local/share/bin/preview
blob: 0ca63a70df8c82d3d31bc2d57805befc75a22216 (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
#!/bin/sh
[ -n "$1" ] && FILE="$1" || {
	echo "error: no input file"
	exit 1
}
[ -n "$2" ] && WIDTH="$(( "$2" - 3 ))" || 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#*.}"

[ "$EXT" = "md" ] && [ "$MIMETYPE" = "text/plain" ] && MIMETYPE="application/markdown"

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)
EOF
}

case "$MIMETYPE" in
	image/*)
		echo '{
			"action": "add",
			"x": 0,
			"y": 0,
			"path": "/home/loek/docs/frob.jpg"
		}' | ueberzug layer --parser json
		;;
	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/markdown)
		pandoc --from=gfm --to=man "$FILE" | render_manpage
		;;
	*)
		echo "$MIMETYPE"
		file -b "$FILE" | fold --width="$WIDTH" --spaces
		;;
esac