nix-config/home/lf/pv.nix

64 lines
2.3 KiB
Nix
Raw Normal View History

2022-03-14 15:20:17 +01:00
{ config, pkgs, ... }:
let
file = "${pkgs.file}/bin/file";
lynx = "${pkgs.lynx}/bin/lynx";
man = "${pkgs.man}/bin/man";
col = "${pkgs.util-linux}/bin/col";
bat = "${pkgs.bat}/bin/bat";
atool = "${pkgs.atool}/bin/atool";
odt2txt = "${pkgs.odt2txt}/bin/odt2txt";
gpg = "${pkgs.gnupg}/bin/gpg";
mediainfo = "${pkgs.mediainfo}/bin/mediainfo";
printf = "${pkgs.coreutils-full}/bin/printf";
lfcache = "${config.home.homeDirectory}/.cache/lf";
stat = "${pkgs.coreutils-full}/bin/stat";
readlink = "${pkgs.coreutils-full}/bin/readlink";
sha256sum = "${pkgs.coreutils-full}/bin/sha256sum";
awk = "${pkgs.gawk}/bin/awk";
ffmpegthumbnailer = "${pkgs.ffmpegthumbnailer}/bin/ffmpegthumbnailer";
pdftoppm = "${pkgs.poppler_utils}/bin/pdftoppm";
in pkgs.writeShellScript "pv.sh" ''
image() {
if [ -n "$DISPLAY" ] && [ -z "$WAYLAND_DISPLAY" ] && [ -n "$FIFO_UEBERZUG" ]; then
${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"
else
${mediainfo} "$1"
fi
}
get_cache_file () {
abs_path=$(${readlink} -f "$1")
postfix=$(${stat} --printf '%n\0%i\0%F\0%s\0%W\0%Y' -- "$abs_path" | ${sha256sum} | ${awk} '{print $1}')
echo "${lfcache}/thumb.$postfix"
}
video () {
cache=$(get_cache_file "$1")
[ ! -f "$cache" ] && ${ffmpegthumbnailer} -i "$1" -o "$cache" -s 0
image "$cache" "$2" "$3" "$4" "$5"
}
pdf () {
cache=$(get_cache_file "$1")
[ ! -f "$cache.jpg" ] && ${pdftoppm} -jpeg -f 1 -singlefile "$1" "$cache"
image "$cache.jpg" "$2" "$3" "$4" "$5"
}
case "$(${file} --dereference --brief --mime-type -- "$1")" in
image/*) image "$1" "$2" "$3" "$4" "$5" ;;
video/*) video "$1" "$2" "$3" "$4" "$5" ;;
*/pdf) pdf "$1" "$2" "$3" "$4" "$5" ;;
text/html) ${lynx} -width="$4" -display_charset=utf-8 -dump "$1" ;;
text/troff) ${man} ./ "$1" | ${col} -b ;;
2022-03-15 08:21:12 +01:00
text/* | */xml | application/json) ${bat} --terminal-width "$4" -f "$1" ;;
2022-03-14 15:20:17 +01:00
application/zip) ${atool} --list -- "$1" ;;
*opendocument*) ${odt2txt} "$1" ;;
audio/* | application/octet-stream) ${mediainfo} "$1" || exit 1 ;;
application/pgp-encrypted) ${gpg} -d -- "$1" ;;
esac
exit 1
''