-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreview
executable file
·121 lines (106 loc) · 2.22 KB
/
preview
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
#!/usr/bin/env sh
TMP="/tmp/fzf-preview"
LTMP="$HOME/.cache/fzf-preview"
CACHEFILE="$TMP/$(echo "$1" | base64).png"
LCACHEFILE="$LTMP/$(echo "$1" | base64).png"
maketemp() {
[ ! -d "$TMP" ] && mkdir -p "$TMP"
[ ! -d "$LTMP" ] && mkdir -p "$LTMP"
}
previewclear() {
# printf '\033[2J\033[H'
printf '\033[2J\033[3J\033[1;1H'
printf "\x1b_Ga=d,d=A\x1b\\"
}
text() {
bat --pager=never --wrap never --style="changes" --color="always" "$1" -p
}
archive() {
local filename="$1"
local extension="${filename##*.}"
# Determine the tool to use based on the file extension
case "$extension" in
zip)
unzip -l "$filename" | awk 'NR>3 {print $4}'
;;
tar | tgz | tar.gz)
tar -tf "$filename"
;;
tar.bz2 | tbz2)
tar -tjf "$filename"
;;
tar.xz | txz)
tar -tJf "$filename"
;;
rar)
unrar l "$filename" | awk '/^[ ]+[0-9]/ {print $NF}'
;;
7z)
7z l "$filename" | awk 'NR>18 {print $NF}'
;;
*)
echo "Unsupported file type: $extension"
;;
esac
}
draw() {
kitty icat --transfer-mode=memory --stdin=no --place=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}@0x0 $1
}
image() {
draw "$1"
}
# https://ffmpeg.org/ffmpeg-filters.html#showspectrum-1
audio() {
[ ! -f "$CACHEFILE" ] && ffmpeg -loglevel 0 -y -i "$1" -lavfi "showspectrumpic=s=hd480:legend=0:gain=5:color=intensity" "$CACHEFILE"
draw "$CACHEFILE"
}
video() {
[ ! -f "$LCACHEFILE" ] && ffmpegthumbnailer -i "$1" -o "$LCACHEFILE" -s 1024 -q 10 &>/dev/null
draw "$LCACHEFILE"
}
pdf() {
[ ! -f "$CACHEFILE.png" ] && pdftoppm -png -singlefile "$1" "$CACHEFILE" -scale-to 1024
draw "$CACHEFILE.png"
}
epub() {
[ ! -f "$CACHEFILE" ] && epub-thumbnailer "$1" "$CACHEFILE" 1024
draw "$CACHEFILE"
}
service() {
SYSTEMD_COLORS=1 systemctl status "$1"
}
main() {
previewclear
maketemp
mimetype=$(file -b --mime-type "$1" 2>/dev/null)
case $mimetype in
application/epub*)
epub "$1"
;;
application/pdf)
pdf "$1"
;;
application/zip | application/x-tar | *rar | application/gzip)
archive "$1"
;;
audio/*)
audio "$1"
;;
image/*)
image "$1"
;;
text/*)
text "$1"
;;
video/*)
video "$1"
;;
inode/directory)
[ "${1##*/..*}" = "" ] && echo || eza --icons --color=always --tree --level=2 --git "$1"
;;
*)
text "$1"
;;
esac
}
main "$1"