-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpv.sh
More file actions
executable file
·78 lines (67 loc) · 1.8 KB
/
Copy pathpv.sh
File metadata and controls
executable file
·78 lines (67 loc) · 1.8 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
#!/usr/bin/env zsh
# LF PREVIEWER - REFACTORED FOR RELIABILITY
file="$1"
w="$2"
h="$3"
# Setup cleanup trap for temp files
cleanup() {
rm -f /tmp/lf-pdf-*.jpg
}
trap cleanup EXIT
# Check if command exists
check_cmd() {
if ! command -v "$1" &> /dev/null; then
echo "Error: '$1' not found. Install it to enable previews."
return 1
fi
return 0
}
# Display text with fallback
display_text() {
local fallback="$1"
if check_cmd bat 2>/dev/null; then
bat --color=always --style=plain --terminal-width="$w" "$file" 2>/dev/null
else
eval "$fallback"
fi
}
# Get MIME type
mimetype=$(file --mime-type -b "$file")
case "$mimetype" in
# Images
image/*)
check_cmd chafa || exit 0
timeout 5 chafa -f symbols --size "${w}x${h}" --animate false "$file"
exit 0
;;
# PDF
application/pdf)
check_cmd pdftoppm chafa || exit 0
tmpfile="/tmp/lf-pdf-$$.jpg"
if timeout 5 pdftoppm -f 1 -l 1 -scale-to 800 -jpeg -singlefile "$file" "${tmpfile%.*}" 2>/dev/null; then
timeout 5 chafa -f sixel --size "${w}x${h}" --speed 9 --polite on "$tmpfile"
fi
exit 0
;;
# Archives
application/zip|application/x-tar|application/x-7z-compressed|application/x-rar|application/x-gzip)
if check_cmd 7z 2>/dev/null; then
7z l "$file" 2>/dev/null && exit 0
fi
if check_cmd tar 2>/dev/null; then
tar -tf "$file" 2>/dev/null && exit 0
fi
echo "Error: No archive tool available"
exit 1
;;
# Text and code
text/*|application/json|application/javascript|*+xml)
display_text "cat \"\$file\""
exit 0
;;
# Default
*)
display_text "file -b \"\$file\""
exit 0
;;
esac