nix-config/home/lf/pv.nix

61 lines
2.1 KiB
Nix
Raw Permalink Normal View History

2022-03-14 15:20:17 +01:00
{ config, pkgs, ... }:
let
2023-01-29 20:00:03 +01:00
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";
2022-03-14 15:20:17 +01:00
file = "${pkgs.file}/bin/file";
2023-01-29 20:00:03 +01:00
gpg = "${pkgs.gnupg}/bin/gpg";
lfcache = "${config.home.homeDirectory}/.cache/lf";
2022-03-14 15:20:17 +01:00
lynx = "${pkgs.lynx}/bin/lynx";
man = "${pkgs.man}/bin/man";
mediainfo = "${pkgs.mediainfo}/bin/mediainfo";
2023-01-29 20:00:03 +01:00
odt2txt = "${pkgs.odt2txt}/bin/odt2txt";
pdftoppm = "${pkgs.poppler_utils}/bin/pdftoppm";
2022-03-14 15:20:17 +01:00
printf = "${pkgs.coreutils-full}/bin/printf";
readlink = "${pkgs.coreutils-full}/bin/readlink";
sha256sum = "${pkgs.coreutils-full}/bin/sha256sum";
2023-01-29 20:00:03 +01:00
stat = "${pkgs.coreutils-full}/bin/stat";
2022-03-14 15:20:17 +01:00
in pkgs.writeShellScript "pv.sh" ''
image() {
2023-01-29 20:00:03 +01:00
${chafa} "$1" -f sixel -s "$(($2-2))x$3" | sed 's/#/\n#/g'
2022-03-14 15:20:17 +01:00
}
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
''