nix-config/modules/home-manager/lf/pv.nix

61 lines
2.1 KiB
Nix

{ config, pkgs, ... }:
let
atool = "${pkgs.atool}/bin/atool";
awk = "${pkgs.gawk}/bin/awk";
bat = "${pkgs.bat}/bin/bat";
chafa = "${pkgs.chafa}/bin/chafa";
col = "${pkgs.util-linux}/bin/col";
ffmpegthumbnailer = "${pkgs.ffmpegthumbnailer}/bin/ffmpegthumbnailer";
file = "${pkgs.file}/bin/file";
gpg = "${pkgs.gnupg}/bin/gpg";
lfcache = "${config.home.homeDirectory}/.cache/lf";
lynx = "${pkgs.lynx}/bin/lynx";
man = "${pkgs.man}/bin/man";
mediainfo = "${pkgs.mediainfo}/bin/mediainfo";
odt2txt = "${pkgs.odt2txt}/bin/odt2txt";
pdftoppm = "${pkgs.poppler_utils}/bin/pdftoppm";
printf = "${pkgs.coreutils-full}/bin/printf";
readlink = "${pkgs.coreutils-full}/bin/readlink";
sha256sum = "${pkgs.coreutils-full}/bin/sha256sum";
stat = "${pkgs.coreutils-full}/bin/stat";
in pkgs.writeShellScript "pv.sh" ''
image() {
${chafa} "$1" -f sixel -s "$(($2-2))x$3" | sed 's/#/\n#/g'
}
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 ;;
text/* | */xml | application/json) ${bat} --terminal-width "$4" -f "$1" ;;
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
''