-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfzf-file-previewer
executable file
·194 lines (177 loc) · 6.8 KB
/
fzf-file-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
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
#!/usr/bin/env sh
# vim: ft=sh ts=4 sw=4 sts=4 et :
has() {
# $1: command to test
command -v "$1" >/dev/null 2>&1
}
which_exe() {
# $@: list of commands to test
for cmd in "$@"; do
if has "$cmd"; then
echo "$cmd"
return
fi
done
}
fzf_file_previewer() {
# $1: file to preview
# In case that the file name starts with a hyphen
# and command does not support '--' convention
fpath=$(realpath -- "$1" 2>/dev/null)
if [ ! -r "$1" ]; then
printf "\033[1m%s\033[0m\n" "File not readable:" >&2
printf "%s\n" "$1" >&2
return 1
fi
# Clear possible ueberzugpp image preview
if [ -S "$UB_SOCK" ] && has ueberzugpp; then
ueberzugpp cmd -s "$UB_SOCK" -a remove -i fzf-file-preview-ub 2>/dev/null
fi
# Preview directories
if [ -d "$1" ]; then
if has tree; then
tree_output=$(tree -N -L 4 -C -- "$1")
info=$(echo "$tree_output" | tail -n 1)
printf "\033[1m%s\033[0m\n\n" "$info"
printf "%s\n" "$tree_output" | head -n -1
else
if has find && has wc; then
num_files=$(find "$fpath" -type f | wc -l)
num_dirs=$(find "$fpath" -type d | wc -l)
printf "\033[1m%d directories, %d files\033[0m\n" \
"$num_dirs" "$num_files"
fi
ls -l --color=always -- "$1"
fi
return
fi
# Preview regular files
if [ -f "$1" ]; then
case "$(file -b --mime -- "$1")" in
image/*)
# ueberzugpp is up and fzf is called from wrapper
if [ -S "$UB_SOCK" ] && has ueberzugpp; then
if [ "$((FZF_MARGIN_RIGHT + FZF_MARGIN_LEFT + \
FZF_PADDING_RIGHT + FZF_PADDING_LEFT))" -lt \
"$FZF_TERM_COLUMNS" ]; then
if [ "$FZF_PREVIEW_ORIENTATION" = 'right' ]; then
ub_x=$((FZF_TERM_COLUMNS - FZF_PREVIEW_COLUMNS - \
FZF_MARGIN_RIGHT - FZF_PADDING_RIGHT - 2))
else
ub_x=$((FZF_MARGIN_LEFT + FZF_PADDING_LEFT + 2))
fi
fi
if [ "$((FZF_MARGIN_UP + FZF_MARGIN_BOTTOM + \
FZF_PADDING_UP + FZF_PADDING_BOTTOM))" -lt \
"$FZF_TERM_LINES" ]; then
if [ "$FZF_PREVIEW_ORIENTATION" = 'up' ]; then
ub_y=$((FZF_TERM_LINES - FZF_HEIGHT + \
FZF_MARGIN_TOP + FZF_PADDING_TOP + 1))
else
ub_y=$((FZF_TERM_LINES - FZF_MARGIN_BOTTOM - \
FZF_PADDING_BOTTOM - FZF_PREVIEW_LINES - 1))
fi
fi
if [ -n "$ub_x" ] && [ -n "$ub_y" ]; then
ueberzugpp cmd \
-s "$UB_SOCK" \
-i fzf-file-preview-ub \
-a add \
-x "$ub_x" \
-y "$ub_y" \
--max-width "$FZF_PREVIEW_COLUMNS" \
--max-height "$((FZF_PREVIEW_LINES - 1))" \
-f "$1" 2>/dev/null
return
fi
fi
# Use ASCII image previewers if ueberzugpp not available
case "$(which_exe chafa imgcat tiv)" in
chafa)
if [ -n "$TMUX" ]; then
chafa -f symbols \
-s "$FZF_PREVIEW_COLUMNS"x"$FZF_PREVIEW_LINES" "$fpath"
else
chafa -s "$FZF_PREVIEW_COLUMNS"x"$FZF_PREVIEW_LINES" "$fpath"
fi
;;
imgcat)
imgcat -H -w "$FZF_PREVIEW_COLUMNS" \
-r "$FZF_PREVIEW_LINES" "$fpath"
;;
tiv)
tiv -w "$FZF_PREVIEW_COLUMNS" -h "$FZF_PREVIEW_LINES" "$fpath"
;;
*)
echo "No ascii image previewer found, install chafa, imgcat or tiv" >&2
;;
esac
;;
application/pdf*)
if has pdftotext; then
pdftotext -l 10 -nopgbrk -nodiag -q "$1" -
return
else
echo 'pdftotext not found' >&2
fi
;;
application/zip*)
if has unzip; then
unzip -l "$fpath"
elif has zip; then
zip -sf -- "$1"
else
echo 'unzip and zip not found' >&2
fi
;;
application/x-tar* | application/gzip*)
if has tar; then
tar --ignore-command-error -tf "$1" 2>/dev/null
else
echo 'tar not found' >&2
fi
;;
text/html*)
if has w3m; then
w3m -dump "$fpath"
return
elif has html2text; then
html2text -- "$1"
return
fi
;& # fallthrough
*)
# Use glow for markdown files
case "$1" in
*.md)
if has glow; then
glow_config="${XDG_CONFIG_HOME-$HOME/.config}/glow/glow.yml"
if [ -e "$glow_config" ]; then
style=$(grep -Po '(?<=style: ")[^"]+' "$glow_config")
else
style='auto'
fi
glow -s="$style" -w="$FZF_PREVIEW_COLUMNS" -- "$1"
return
fi
;;
esac
# Use highlight or cat for remaining files
if has highlight; then
highlight_config="$HOME/.highlight/themes/highlight.theme"
if [ ! -e "$highlight_config" ]; then
highlight -O ansi --force -- "$1"
else
highlight -O ansi --force --config-file "$highlight_config" -- "$1"
fi
return
fi
if has cat; then
cat "$1"
return
fi
;;
esac
fi
}
fzf_file_previewer "$@"