-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathscope
More file actions
executable file
·131 lines (127 loc) · 4.11 KB
/
scope
File metadata and controls
executable file
·131 lines (127 loc) · 4.11 KB
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/sh
# @author nate zhou
# @since 2025,2026
# general file previewer for LF & FZF
# `~/.local/bin/fzf-scope` is a modified version of my `lf/scope`
## save a cache file for faster preview
[ -d "$HOME/.cache/lf" ] || mkdir "$HOME/.cache/lf"
show_img(){
if [ -n "$FZF_LEVEL" ] || [ -n "$NVIM" ] || [ -n "$DVTM" ]; then
catimg -t -w 80 "$1" # use catimg when running with `fzf` & `lf.vim`
elif [ "$XDG_SESSION_TYPE" != "tty" ] && [ "$TERM" = "foot" ]; then
/usr/bin/chafa -f sixel\
-s "$2x$3" \
--animate off \
--polite on \
-t 1 \
--bg '#111111' \
"$1"
else
# catimg is faster than chafa for ANSI
catimg -t -w "$(($2 * 2))" "$1"
fi
}
case "$(file -Lb --mime-type -- "$1")" in
image/*xcf|image/heic|image/x-xpixmap)
# create a cache for every file, if 2 files have the same
# size+name+last_modified_date, it has the same hash
# `shasum` is faster than other `sha*sum`
CACHE="$HOME/.cache/lf/$(stat --printf '%s%n%Y' "$(readlink -f "$1")" \
| shasum | cut -d' ' -f1)"
[ -f "${CACHE}.jpg" ] || convert "$1" -flatten \
-quality 50 "${CACHE}.jpg"
show_img "${CACHE}.jpg" "$2" "$3"
;;
image/*)
show_img "$1" "$2" "$3"
;;
video/*)
CACHE="$HOME/.cache/lf/$(stat --printf '%s%n%Y' "$(readlink -f "$1")" \
| shasum | cut -d' ' -f1)"
[ -f "${CACHE}.jpg" ] \
|| /usr/bin/ffmpegthumbnailer -i "$1" -s 480 -q 5 -o "${CACHE}.jpg"
show_img "${CACHE}.jpg" "$2" "$3"
;;
audio/*)
mid3v2 -l "$1"
;;
application/epub+zip)
CACHE="$HOME/.cache/lf/$(stat --printf '%s%n%Y' "$(readlink -f "$1")" \
| shasum | cut -d' ' -f1)"
[ -f "${CACHE}.png" ] \
|| /usr/bin/gnome-epub-thumbnailer "$1" "$CACHE".png
show_img "$CACHE".png "$2" "$3"
;;
application/pdf)
CACHE="$HOME/.cache/lf/$(stat --printf '%s%n%Y' "$(readlink -f "$1")" \
| shasum | cut -d' ' -f1)"
# `pdftoppm` already adds `.jpg` extension, don't duplicate
[ -f "${CACHE}.jpg" ] \
|| /usr/bin/pdftoppm -f 1 -l 1 -singlefile -jpeg "$1" "$CACHE"
show_img "$CACHE".jpg "$2" "$3"
;;
application/zip|application/vnd.android.package-archive)
zipinfo "$1" | "$PAGER"
;;
application/gzip)
case "$1" in
*tar.gz)
tar vvtf "$1" | "$PAGER"
;;
*)
zless "$1" | "$PAGER"
;;
esac
;;
application/x-7z-compressed)
7z l -ba "$1" | grep -oP '\S+$' | "$PAGER"
;;
application/*tar|application/*zip*|application/zstd|application/*xz)
tar vvtf "$1" | "$PAGER"
;;
application/vnd.rar)
unrar-free -t "$1" | tail +8 | "$PAGER"
;;
application/x-bittorrent)
/usr/bin/transmission-show "$1" | "$PAGER"
;;
application/x-iso*)
# `iso-info` command from `libcdio`
iso-info --no-header "$1" | tail +2 | "$PAGER"
;;
application/*opendocument*)
odt2txt "$1";
;;
application/octet-stream)
file -b "$1"
;;
message/rfc822)
"$PAGER" "$1"
;;
text/troff)
man -l "$1"
;;
text/html)
case "$1" in
*svg)
show_img "$1" "$2" "$3"
;;
*)
firejail --net=none w3m "$1"
;;
esac
;;
text/*|application/pgp-signature|application/pgp-keys|application/javascript|application/json|application/mbox)
case "$1" in
*svg)
show_img "$1" "$2" "$3"
;;
*)
bat "$1"
;;
esac
;;
inode/directory)
command du -aLhd1 "$1" 2> /dev/null | sort -rh
;;
esac