-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfbundle-previewer
executable file
·87 lines (79 loc) · 3.29 KB
/
lfbundle-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
#!/usr/bin/env zsh
preview_image() {
# gif animations of chafa don't work in lf, prints frames below each other
chafa --animate false --size ${IMAGE_PREVIEWBOX_W}x${IMAGE_PREVIEWBOX_H} "$1"
}
# Preview "$1"'s file content as text using '-' for stdin. Use as common text
# preview wrapper, so that pagination will apply to text aswell. '-p' disables
# fancy output.
preview_text() {
if [ "$1" = "-p" ]; then
shift
bat -P -f -r "$page:" --style plain "$1"
else
bat -P -f -r "$page:" --style "grid,numbers" "$1"
fi
}
thumbnail_abspath="$LFBUNDLE_TEMPDIR/thumbnail.png" # thumbnail path for ad-hoc conversions
page="$(cat "$LFBUNDLE_TEMPDIR/page")" # pagination offset
# Assign inputs to variables. The unusual order in which the coordinates are
# passed into this script is determined by lf. 'abspath' is the absolute path
# of the file to be previewed and the coordinates refer the drawing area for
# image previews.
abspath="$1"; lf_previewbox_x="$4"; lf_previewbox_y="$5"; lf_previewbox_w="$2"; lf_previewbox_h="$3"
# BUG: lf displays stdout output offset by two cells to the right with respect
# to the preview box's boundaries. In order to align image previews with text
# we add that offset to the preview coordinates.
IMAGE_PREVIEWBOX_X=$((lf_previewbox_x+2)); IMAGE_PREVIEWBOX_W=$((lf_previewbox_w-2))
# Determine the number of lines required for the full filename to be displayed
# and add that to the y-offset for image previews. Add another offset for the
# horizontal separator line.
filename="$(basename "$abspath")"
filename_wrapped="$(echo "$filename" | fold -w "$IMAGE_PREVIEWBOX_W")"
num_lines="$(echo "$filename_wrapped" | wc -l)"
IMAGE_PREVIEWBOX_Y=$((lf_previewbox_y+num_lines+1)); IMAGE_PREVIEWBOX_H=$((lf_previewbox_h-lf_previewbox_y-1))
# Print the filename and a separator line. The length of the separator line matches the
# length of the file name.
printf "\033[1;38;5;253m$filename_wrapped\033[0m\n"
w_sepline=$IMAGE_PREVIEWBOX_W
if [ "$num_lines" -eq 1 ]; then
w_sepline="$(echo "$filename_wrapped" | tail -1 | tr -d '\n' | wc -m)"
fi
printf '─%.0s' $(seq 1 $w_sepline); echo
case "$(echo "$filename" | tr '[:upper:]' '[:lower:]')" in
*.tar*) tar tf "$abspath" | preview_text -p - ;;
*.zip) unzip -l "$abspath" | preview_text -p - ;;
*.rar) unrar l "$abspath" | preview_text -p - ;;
*.7z) 7z l "$abspath" | sed -n -e '/^----------/,$p' | tail +2 | head -n-2 | rev | cut -d' ' -f1 | rev | preview_text -p - ;;
*.avi|*.mp4|*.mkv|*.webm)
ffmpeg -y -i "$abspath" -vframes 1 "$thumbnail_abspath"
preview_image "$thumbnail_abspath"
;;
*.pdf|*.ps)
gs -o "$thumbnail_abspath" -sDEVICE=pngalpha -dFirstPage="$page" -dLastPage="$page" "$abspath" >/dev/null
preview_image "$thumbnail_abspath"
;;
*.odt|*.ods|*.odp|*.odg|*.odf|*.odb|*.doc|*.docx)
echo
soffice --cat "$abspath" | preview_text -p -
;;
*.djvu)
buffer="$LFBUNDLE_TEMPDIR/buffer.tiff"
ddjvu -page="$page" -format=tiff "$abspath" "$buffer"
gm convert "$buffer" "$thumbnail_abspath"
preview_image "$thumbnail_abspath"
;;
*.jpg|*.jpeg|*.png|*.bmp|*.tiff|*.gif|*.svg)
preview_image "$abspath"
;;
*.svg)
gm convert "$abspath" "$thumbnail_abspath"
preview_image "$thumbnail_abspath"
;;
*.iso)
iso-info --no-header "$abspath" | tail -n +2 | preview_text -p -
;;
*) preview_text "$abspath"
;;
esac
return 127 # nonzero retcode to disable preview caching