-
Notifications
You must be signed in to change notification settings - Fork 3
/
lessfilter
executable file
·83 lines (79 loc) · 2.37 KB
/
lessfilter
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
76
77
78
79
80
81
82
#!/bin/bash
function cmdexist() {
which "$1" &> /dev/null
}
# Determine file type of $1
# and determine which viewer is appropriate
case "$(file -Lb "$1")" in
# Image
*image[[:space:]]data*)
width=$(stty size | cut -d ' ' -f 2)
if cmdexist termpix; then
# https://github.com/hopey-dishwasher/termpix
termpix "$1" --width $width
elif cmdexist terminal-image-viewer; then
# Wrapper around this https://github.com/stefanhaustein/TerminalImageViewer.git
terminal-image-viewer -w $width "$1"
elif cmdexist img2txt; then
# libcaca (caca-utils package)
img2txt -W $width -f utf8 "$1"
fi
;;
POSIX[[:space:]]tar[[:space:]]archive*)
tar tvvf "$1" ;;
gzip[[:space:]]compressed[[:space:]]data*) # Test if tar or man
if gzip -dc "$1" | file - | grep -q 'tar archive' ; then
tar tvvf "$1"
elif gzip -dc "$1" | file - | grep -q 'roff' ; then
gzip -dc "$1" | nroff -S -mandoc -
else
gzip -dc "$1"
fi ;;
bzip2[[:space:]]compressed[[:space:]]data*)
if bzip2 -dc "$1" | file - | grep -q 'tar archive' ; then
tar tvvf "$1"
elif bzip2 -dc "$1" | file - | grep -q 'roff' ; then
bzip2 -dc "$1" | nroff -S -mandoc -
else
bzip2 -dc "$1"
fi ;;
cpio[[:space:]]archive)
cmdexist cpio && cpio -it < "$1" ;;
[Xx][Zz][[:space:]]compressed[[:space:]]data*)
if xz -dc "$1" | file - | grep 'tar archive' ; then
xz -dc "$1" | tar tvvf -
else
xz -dc "$1"
fi ;;
data) # Test if lzma archive
if xz -t "$1" ; then
if xz -F lzma -dc "$1" | file - | grep 'tar archive' ; then
xz -F lzma -dc "$1" | tar tvvf -
else
xz -F lzma -dc "$1"
fi
else
return
fi ;;
ISO[[:space:]]9660[[:space:]]CD-ROM[[:space:]]filesystem[[:space:]]data*)
if cmdexist isoinfo ; then
echo "$1:" ; isoinfo -d -i "$1"
echo
echo '***Contents:' ; isoinfo -f -i "$1"
fi ;;
Zip[[:space:]]archive[[:space:]]data*)
unzip -l "$1" ;;
RAR[[:space:]]archive[[:space:]]data*)
cmdexist unrar && unrar l "$1" ;;
# text
*text*|HTML*)
if [[ "$1" =~ [.]*md$ ]] ; then
if cmdexist mdv; then
mdv "$1"
elif cmdexist pandoc; then
pandoc -s -f markdown -t man "$1" | man -l -
fi
else
bat --paging never --color always --style full "$1"
fi ;;
esac