-
Notifications
You must be signed in to change notification settings - Fork 0
/
.lessfilter
executable file
·84 lines (82 loc) · 2.39 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
83
84
# #! /usr/bin/env sh
# # this is a example of .lessfilter, you can change it
# mime=$(file -bL --mime-type "$1")
# category=${mime%%/*}
# kind=${mime##*/}
# if [ -d "$1" ]; then
# exa --git -hl --color=always --icons "$1"
# elif [ "$category" = image ]; then
# chafa "$1"
# exiftool "$1"
# elif [ "$kind" = vnd.openxmlformats-officedocument.spreadsheetml.sheet ] || \
# [ "$kind" = vnd.ms-excel ]; then
# in2csv "$1" | xsv table | bat -ltsv --color=always
# elif [ "$category" = text ]; then
# bat --color=always "$1"
# else
# lesspipe.sh "$1" | bat --color=always
# fi
# # lesspipe.sh don't use exa, bat and chafa, it use ls and exiftool. so we create a lessfilter.
#! /usr/bin/env bash
has_cmd() {
for opt in "$@"; do
command -v "$opt" >/dev/null
done
}
mime=$(file -Lbs --mime-type "$1")
category=${mime%%/*}
kind=${mime##*/}
ext=${1##*.}
if [ "$kind" = json ]; then
if has_cmd jupyter bat && [ "$ext" = ipynb ]; then
jupyter nbconvert --to python --stdout "$1" | bat --color=always -plpython
elif has_cmd jq; then
jq -Cr . "$1"
fi
# elif [ "$kind" = vnd.sqlite3 ]; then
# if has_cmd yes sqlite3 bat; then
# yes .q | sqlite3 "$1" | bat --color=always -plsql
# fi
# https://github.com/wofr06/lesspipe/pull/107
elif [ -d "$1" ]; then
if has_cmd eza; then
eza --sort=type --long --no-time --no-permissions --time-style=relative --color=always --icons "$1" # --no-user --git
elif has_cmd exa; then
exa -la --color=always --icons "$1"
fi
# https://github.com/wofr06/lesspipe/pull/110
elif [ "$kind" = pdf ]; then
if has_cmd pdftotext sed; then
pdftotext -q "$1" - | sed "s/\f/$(hr ─)\n/g"
fi
# https://github.com/wofr06/lesspipe/pull/115
elif [ "$kind" = rfc822 ]; then
if has_cmd bat; then
bat --color=always -lEmail "$1"
fi
# https://github.com/wofr06/lesspipe/pull/106
elif [ "$category" = image ]; then
if has_cmd chafa; then
chafa --scale 0.5 --color-space rgb -c 240 --dither none -f symbols "$1"
fi
echo -e "\n"
if has_cmd exiftool; then
exiftool "$1" | bat --color=always -plyaml
fi
# https://github.com/wofr06/lesspipe/pull/117
elif [ "$category" = text ]; then
if has_cmd bat; then
bat -P --style 'plain,changes' --color=always "$1"
elif has_cmd pygmentize; then
pygmentize "$1" | less
fi
# elif [ $(has_cmd "$1") ]; then
# if has_cmd tldr; then
# tldr "$1" | less
# else
# cat $(which "$1") $(man "$1") | less
# fi
else
lesspipe.sh "$1" | bat --color=always
# exit 1
fi