Krolyxon
Commit 7eea339

Branches

Refs →

dotfiles-x11.configlfscope

44 lines · 1.7 KB · Bourne Again Shell (bash)

1#!/bin/sh
2
3# File preview handler for lf.
4
5set -C -f
6IFS="$(printf '%b_' '\n')"; IFS="${IFS%_}"
7
8image() {
9 if [ -f "$1" ] && [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && command -V ueberzug >/dev/null 2>&1; then
10 printf '{"action": "add", "identifier": "PREVIEW", "x": "%s", "y": "%s", "width": "%s", "height": "%s", "scaler": "contain", "path": "%s"}\n' "$4" "$5" "$(($2-1))" "$(($3-1))" "$1" > "$FIFO_UEBERZUG"
11 else
12 mediainfo "$6"
13 fi
14}
15
16ifub() {
17 [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && command -V ueberzug >/dev/null 2>&1
18}
19
20# Note that the cache file name is a function of file information, meaning if
21# an image appears in multiple places across the machine, it will not have to
22# be regenerated once seen.
23
24case "$(file --dereference --brief --mime-type -- "$1")" in
25 image/*) image "$1" "$2" "$3" "$4" "$5" "$1" ;;
26 text/html) lynx -width="$4" -display_charset=utf-8 -dump "$1" ;;
27 text/troff) man ./ "$1" | col -b ;;
28 text/* | */xml | application/json) cat "$1" ;;
29 application/zip) atool --list -- "$1" ;;
30 audio/* | application/octet-stream) mediainfo "$1" || exit 1 ;;
31 video/* )
32 CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)"
33 [ ! -f "$CACHE" ] && ffmpegthumbnailer -i "$1" -o "$CACHE" -s 0
34 image "$CACHE" "$2" "$3" "$4" "$5" "$1"
35 ;;
36 */pdf)
37 CACHE="${XDG_CACHE_HOME:-$HOME/.cache}/lf/thumb.$(stat --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$(readlink -f "$1")" | sha256sum | cut -d' ' -f1)"
38 [ ! -f "$CACHE.jpg" ] && pdftoppm -jpeg -f 1 -singlefile "$1" "$CACHE"
39 image "$CACHE.jpg" "$2" "$3" "$4" "$5" "$1"
40 ;;
41 *opendocument*) odt2txt "$1" ;;
42 application/pgp-encrypted) gpg -d -- "$1" ;;
43esac
44exit 1