-
Notifications
You must be signed in to change notification settings - Fork 10
/
previewer
executable file
·131 lines (122 loc) · 4.2 KB
/
previewer
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
#!/usr/bin/env bash
FILE_PATH="${1}"
FILE_EXTENSION="${FILE_PATH##*.}"
FILE_EXTENSION_LOWER="$(printf "%s" "${FILE_EXTENSION}" | tr '[:upper:]' '[:lower:]')"
PV_WIDTH="${2}"
PV_HEIGHT="${3}"
HORIZONTAL_POS="${4}"
VERTICAL_POS="${5}"
bat() {
command bat \
--color=always --paging=never \
--style=plain \
--wrap=character \
--line-range :"${PV_HEIGHT}" \
--terminal-width="${PV_WIDTH}" "$@"
}
image_previewer() {
# In Kitty, only lf running in normal terminal, not in tmux or Neovim's builtin terminal, could
# use kitty image protocal. In other situations, chafa could be used, however, lf running in
# tmux displays the image with abnormal colors.
if [[ $KITTY_WINDOW_ID ]]; then
# In Kitty
if [[ ! $TERM =~ "tmux" && -z $NVIM ]]; then
local place="${PV_WIDTH}x${PV_HEIGHT}@${HORIZONTAL_POS}x${VERTICAL_POS}"
kitty icat --clear --transfer-mode memory --stdin no --align left --place "$place" "$1" < /dev/null > /dev/tty
elif [[ $TERM =~ "tmux" && -z $NVIM ]]; then
printf "Preview is not supported here!\n"
printf "Instead, preview it in FZF (CTRL-F)."
else
chafa -f symbols -s "${PV_WIDTH}x${PV_HEIGHT}" --animate off --polite on "$1"
fi
elif [[ $WEZTERM_PANE || $ALACRITTY_WINDOW_ID ]]; then
# In Wezterm or Alacritty
if [[ ! $TERM =~ "tmux" && -z $NVIM ]]; then
chafa -s "${PV_WIDTH}x${PV_HEIGHT}" --animate off --polite on "$1"
elif [[ $TERM =~ "tmux" && -z $NVIM ]]; then
printf "Preview is not supported here!\n"
printf "Instead, preview it in FZF (CTRL-F)."
else
chafa -f symbols -s "${PV_WIDTH}x${PV_HEIGHT}" --animate off --polite on "$1"
fi
else
printf "Preview is NOT supported!"
fi
}
handle_extension() {
case "${FILE_EXTENSION_LOWER}" in
# Markdown
md)
glow -s dark --width "${PV_WIDTH}" -- "${FILE_PATH}" && exit 1;;
# Archive
bz2|gz|lz|tar|xz|zip)
atool --list -- "${FILE_PATH}" && exit 1;;
rar)
unrar lt -p- -- "${FILE_PATH}" && exit 1;;
7z)
7z l -p -- "${FILE_PATH}" && exit 1;;
# PDF
pdf)
pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - | fmt -w "${PV_WIDTH}" && exit 1;;
# OpenDocument
odt|sxw|ods|odp)
odt2txt "${FILE_PATH}" && exit 1;;
# XLSX
xlsx)
xlsx2csv -- "${FILE_PATH}" | head -n 500 && exit 1;;
# JSON
json)
jq --color-output . "${FILE_PATH}" && exit1 ;;
# BitTorrent
torrent)
transmission-show -- "${FILE_PATH}" && exit 1;;
# Dmg
dmg)
hdiutil imageinfo "${FILE_PATH}" | bat -l yaml && exit 1;;
# CSV and TSV
csv|tsv)
mlr --icsv --opprint -C --key-color darkcyan --value-color grey70 head -n 500 "${FILE_PATH}" && exit 1;;
# Sqlite3 and sqlite
sqlite3 | sqlite)
sqlite3 "${FILE_PATH}" .schema | sed "s/;/;\n/g" | bat -l sql && exit 1;;
# so and dylib
so|dylib)
nm "${FILE_PATH}" && exit 1
nm -D "${FILE_PATH}" && exit 1
esac
}
handle_mime() {
local mimetype="${1}"
case "${mimetype}" in
# DOCX, ePub, FB2
*wordprocessingml.document|*/epub+zip|*/x-fictionbook+xml)
pandoc -s -t markdown -- "${FILE_PATH}" | glow -s dark --width "${PV_WIDTH}" && exit 1;;
# Image
image/*)
image_previewer "${FILE_PATH}"
exit 1;;
# Video
video/*)
local thumbnail=$($HOME/.config/lf/vidthumb "${FILE_PATH}")
image_previewer "$thumbnail"
exit 1;;
# Text
text/* | */xml)
(bat --style=numbers,changes -- "${FILE_PATH}" \
|| highlight --out-format truecolor --style darkplus --force \
--line-numbers --line-range=1-"${PV_HEIGHT}" -- "${FILE_PATH}" \
|| cat -- "${FILE_PATH}") && exit 1;;
esac
}
handle_fallback() {
# Use file command as the fallback. It outputs the file properties, separated by comma, in a
# single lone line. In order to fit the width of the preview window, replace each comma with a
# line break, but leave the commas inside square brackets unchanged.
file --dereference --brief -- "${FILE_PATH}" | gsed -r ':a; s/(\[[^][]*),([^][]*\])/\1TTEEMMPP\2/g; ta; s/, /\n/g; s/TTEEMMPP/,/g' && exit 1
exit 1
}
MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )"
handle_extension
handle_mime "${MIMETYPE}"
handle_fallback
exit 1