aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/sh/.local/bin/lessfilter
blob: dcd4d019d82c154754726966ab0c73d0c9ab48fe (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/sh

SCRIPT="$(basename "$0")"

long='width,verbose,help'
short='wvh'

if ! opts="$(getopt -o $short -l $long -n "$SCRIPT" -- "$@")"; then
	exit 1
fi

eval set -- "$opts"

help() {
	cat <<-EOF
		$SCRIPT
		Toby Vincent <tobyv13@gmail.com>

		$SCRIPT
		    Filter script used for lesspipe.sh filtering

		USAGE:
		    $SCRIPT [OPTION ...] <PATH> [PATH ...]

		OPTIONS:
		    -w, --width           Specify width. By default uses FZF_PREVIEW_COLUMNS if
		                              set, and falls back to COLUMNS
		    -v, --verbose         Increase verbosity
		    -h, --help            Show this help
	EOF
}

say() {
	printf "%s: %s\n" "$SCRIPT" "$@"
}

say_verbose() {
	if [ "$verbose" -gt "0" ]; then
		say "$@"
	fi
}

say_err() {
	say "$@" >&2
}

err() {
	err_dir="$1"
	shift
	say_err "cannot preview '$err_dir': $*"
	exit 1
}

err_help() {
	help
	err "$*"
}

has() {
	command -v "$1" >/dev/null
}

is_git_repo() {
	[ -d "$1/.git" ] || git -C "$1" rev-parse --is-inside-work-tree >/dev/null 2>&1
}

mime() {
	if [ -e "$1" ]; then
		mtype=$(file -L -s -b --mime "$1" 2>/dev/null)
		fcat="${mtype%/*}"
		mtype="${mtype#*/}"
		mtype="${mtype%;*}"
		mtype="${mtype#x-}"
		mtype="${mtype#vnd\.}"

		if [ "$fcat" = "application" ] &&
			[ "$ftype" = "octet-stream" ] ||
			[ "$fcat" = "text" ] &&
			[ "$ftype" = "plain" ]; then
			mtype=$(file -L -s -b "$1" 2>/dev/null)
		fi
	else
		case "$1" in
		http://*)
			mtype='x-scheme-handler/http'
			;;
		https://*)
			mtype='x-scheme-handler/https'
			;;
		*)
			mtype='repo'
			;;
		esac
	fi

	printf %s\\n "$mtype"
}

fileext() {
	case "$1" in
	.*.*) extension=${1##*.} ;;
	.*) extension= ;;
	*.*) extension=${1##*.} ;;
	esac

	printf %s\\n "$extension"
}

filetype() {
	fname="$1"
	fext=$(fileext "$fname")
	mtype=$(mime "$fname")

	ft="${mtype#*/}"
	ft="${ft%;*}"
	ft="${ft#x-}"
	ftype="${ft#vnd\.}"

	case "$fext" in
	epub) [ "$ftype" = "zip" ] && ftype='epub' ;;
	ipynb) [ "$ftype" = "json" ] && ftype='ipynb' ;;
	mp3) [ "$ftype" = "mpeg" ] && ftype='mp3' ;;
	crt | pem) ftype='x509' ;;
	crl) ftype='crl' ;;
	csr) ftype='csr' ;;
	pod) ftype='pod' ;;
	pm) ftype='perl' ;;
	md | MD | mkd | markdown | rst) ftype='markdown' ;;
	log) ftype='log' ;;
	ebuild | eclass) ftype='sh' ;;
	esac

	case "$ftype" in
	openxmlformats-officedocument.wordprocessingml.document) ftype='docx' ;;
	openxmlformats-officedocument.presentationml.presentation) ftype='pptx' ;;
	openxmlformats-officedocument.spreadsheetml.sheet) ftype='xlsx' ;;
	oasis.opendocument.text*) ftype='odt' ;;
	oasis.opendocument.spreadsheet) ftype='ods' ;;
	oasis.opendocument.presentation) ftype='odp' ;;
	sun.xml.writer) ftype='ooffice1' ;;
	shellscript) ftype='sh' ;;
	makefile) ftype='make' ;;
	epub+zip) ftype='epub' ;;
	matlab-data) ftype='matlab' ;;
	troff) case "${fname##*/}" in
		[Mm]akefile | [Mm]akefile.* | BSDMakefile) ftype='make' ;;
		esac ;;
	*mat-file*) ftype='matlab' ;;
	*POD\ document*) ftype='pod' ;;
	*PEM\ certificate\ request) ftype='csr' ;;
	*PEM\ certificate) ftype='csr' ;;
	*Microsoft\ OOXML) ftype='docx' ;;
	Apple\ binary\ property\ list) ftype='plist' ;;
	PGP\ *ncrypted* | GPG\ encrypted*) ftype='pgp' ;;
	Audio\ file\ with\ ID3\ *) ftype='mp3' ;;
	'OpenOffice.org 1.x Writer document') ftype='ooffice1' ;;
	# if still unspecific, determine file type by extension
	data)
		### binary only file formats, type not guessed by 'file'
		case "$fext" in
		mat) ftype='matlab' ;;
		br | bro | tbr) ftype='brotli' ;;
		lz4 | lt4 | tz4 | tlz4) ftype='lz4' ;;
		esac
		;;
	esac

	printf %s\\n "$ftype"
}

verbose=0
width=${FZF_PREVIEW_COLUMNS:-$COLUMNS}
while true; do
	case "$1" in
	-h | --help)
		help
		exit 0
		;;
	-v | --verbose)
		verbose=$((verbose + 1))
		shift
		;;
	-w | --width)
		width=$2
		shift 2
		;;
	--)
		shift
		break
		;;
	*)
		err_help "Invalid argument: $1"
		;;
	esac
done

if [ "$#" -eq 0 ]; then
	IFS='
'
	set -o noglob
	# shellcheck disable=2046
	set -- $(cat)
fi

if [ -z "$width" ]; then
	width=$(tput cols)
fi

ft=$(filetype "$1")
case "$ft" in
directory)
	if has onefetch && is_git_repo "$1" 2>/dev/null; then
		onefetch --hidden --show-logo="$([ "$width" -lt "80" ] && printf 'never' || printf 'always')" "$1" 2>/dev/null
	elif [ -f "$1/README.md" ]; then
		$SCRIPT "$1/README.md"
	else
		exa --tree --git-ignore --level=3 --icons "$1" 2>/dev/null
	fi
	;;
markdown)
	bat --color always "$1" 2>/dev/null
	;;
http | https)
	if printf %s\\n "$1" | grep -q "git.sr.ht"; then
		has hut && hut git show --repo "$1" 2>/dev/null
	elif printf %s\\n "$1" | grep -q "github.com"; then
		has gh && gh repo view "$1" 2>/dev/null
	fi
	;;
repo)
	hut git show --repo "https://git.sr.ht/$1" 2>/dev/null ||
		gh repo view "https://github.com/$1" 2>/dev/null
	;;
*)
	exit 1
	;;
esac