From aee2aa3461becc0ab525d2faaa8d74622937276e Mon Sep 17 00:00:00 2001 From: Ricard Illa Date: Mon, 14 Mar 2022 15:20:17 +0100 Subject: [PATCH] feat: added lf --- home/lf/cleaner.nix | 7 ++ home/lf/default.nix | 18 ++++ home/lf/lf-wrapper.nix | 195 +++++++++++++++++++++++++++++++++++++++++ home/lf/pv.nix | 63 +++++++++++++ home/nixos-common.nix | 1 + 5 files changed, 284 insertions(+) create mode 100644 home/lf/cleaner.nix create mode 100644 home/lf/default.nix create mode 100644 home/lf/lf-wrapper.nix create mode 100644 home/lf/pv.nix diff --git a/home/lf/cleaner.nix b/home/lf/cleaner.nix new file mode 100644 index 0000000..de9d83d --- /dev/null +++ b/home/lf/cleaner.nix @@ -0,0 +1,7 @@ +{ config, pkgs, ... }: + +let printf = "${pkgs.coreutils-full}/bin/printf"; +in +pkgs.writeShellScript "lf-cleaner.sh" '' + [ -n "$FIFO_UEBERZUG" ] && ${printf} '{"action": "remove", "identifier": "PREVIEW"}\n' > "$FIFO_UEBERZUG" +'' diff --git a/home/lf/default.nix b/home/lf/default.nix new file mode 100644 index 0000000..bc392a2 --- /dev/null +++ b/home/lf/default.nix @@ -0,0 +1,18 @@ +{ config, pkgs, ... }: + +let + pv = pkgs.callPackage ./pv.nix { inherit config pkgs; }; + lf-wrapper = pkgs.callPackage ./lf-wrapper.nix { inherit config pkgs; }; + cleaner = pkgs.callPackage ./cleaner.nix { inherit config pkgs; }; + +in { + home.packages = [ lf-wrapper ]; + programs.lf = { + enable = true; + previewer.source = pv; + settings.icons = true; + extraConfig = '' + set cleaner ${cleaner} + ''; + }; +} diff --git a/home/lf/lf-wrapper.nix b/home/lf/lf-wrapper.nix new file mode 100644 index 0000000..b41c2de --- /dev/null +++ b/home/lf/lf-wrapper.nix @@ -0,0 +1,195 @@ +{ config, pkgs, ... }: + +let + shell = "${pkgs.dash}/bin/dash"; + lf = "${pkgs.lf}/bin/lf"; + rm = "${pkgs.coreutils-full}/bin/rm"; + mkdir = "${pkgs.coreutils-full}/bin/mkdir"; + mkfifo = "${pkgs.coreutils-full}/bin/mkfifo"; + ueberzug = "${pkgs.ueberzug}/bin/ueberzug"; + lfcache = "${config.home.homeDirectory}/.cache/lf"; +in pkgs.writeScriptBin "lf-wrapper" '' + #!${shell} + + set -e + + export LF_ICONS="\ + tw=:\ + st=:\ + ow=:\ + dt=:\ + di=:\ + fi=:\ + ln=:\ + or=:\ + ex=:\ + *.c=:\ + *.cc=:\ + *.clj=:\ + *.coffee=:\ + *.cpp=:\ + *.css=:\ + *.d=:\ + *.dart=:\ + *.erl=:\ + *.exs=:\ + *.fs=:\ + *.go=:\ + *.h=:\ + *.hh=:\ + *.hpp=:\ + *.hs=:\ + *.html=:\ + *.java=:\ + *.jl=:\ + *.js=:\ + *.json=:\ + *.lua=:\ + *.md=:\ + *.php=:\ + *.pl=:\ + *.pro=:\ + *.py=:\ + *.rb=:\ + *.rs=:\ + *.scala=:\ + *.ts=:\ + *.vim=:\ + *.cmd=:\ + *.ps1=:\ + *.sh=:\ + *.bash=:\ + *.zsh=:\ + *.fish=:\ + *.tar=:\ + *.tgz=:\ + *.arc=:\ + *.arj=:\ + *.taz=:\ + *.lha=:\ + *.lz4=:\ + *.lzh=:\ + *.lzma=:\ + *.tlz=:\ + *.txz=:\ + *.tzo=:\ + *.t7z=:\ + *.zip=:\ + *.z=:\ + *.dz=:\ + *.gz=:\ + *.lrz=:\ + *.lz=:\ + *.lzo=:\ + *.xz=:\ + *.zst=:\ + *.tzst=:\ + *.bz2=:\ + *.bz=:\ + *.tbz=:\ + *.tbz2=:\ + *.tz=:\ + *.deb=:\ + *.rpm=:\ + *.jar=:\ + *.war=:\ + *.ear=:\ + *.sar=:\ + *.rar=:\ + *.alz=:\ + *.ace=:\ + *.zoo=:\ + *.cpio=:\ + *.7z=:\ + *.rz=:\ + *.cab=:\ + *.wim=:\ + *.swm=:\ + *.dwm=:\ + *.esd=:\ + *.jpg=:\ + *.jpeg=:\ + *.mjpg=:\ + *.mjpeg=:\ + *.gif=:\ + *.bmp=:\ + *.pbm=:\ + *.pgm=:\ + *.ppm=:\ + *.tga=:\ + *.xbm=:\ + *.xpm=:\ + *.tif=:\ + *.tiff=:\ + *.png=:\ + *.svg=:\ + *.svgz=:\ + *.mng=:\ + *.pcx=:\ + *.mov=:\ + *.mpg=:\ + *.mpeg=:\ + *.m2v=:\ + *.mkv=:\ + *.webm=:\ + *.ogm=:\ + *.mp4=:\ + *.m4v=:\ + *.mp4v=:\ + *.vob=:\ + *.qt=:\ + *.nuv=:\ + *.wmv=:\ + *.asf=:\ + *.rm=:\ + *.rmvb=:\ + *.flc=:\ + *.avi=:\ + *.fli=:\ + *.flv=:\ + *.gl=:\ + *.dl=:\ + *.xcf=:\ + *.xwd=:\ + *.yuv=:\ + *.cgm=:\ + *.emf=:\ + *.ogv=:\ + *.ogx=:\ + *.aac=:\ + *.au=:\ + *.flac=:\ + *.m4a=:\ + *.mid=:\ + *.midi=:\ + *.mka=:\ + *.mp3=:\ + *.mpc=:\ + *.ogg=:\ + *.ra=:\ + *.wav=:\ + *.oga=:\ + *.opus=:\ + *.spx=:\ + *.xspf=:\ + *.pdf=:\ + *.nix=:\ + " + + cleanup () { + exec 3>&- + rm "$FIFO_UEBERZUG" + } + + if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then + ${lf} "$@" + else + [ ! -d "${lfcache}" ] && ${mkdir} -p "${lfcache}" + export FIFO_UEBERZUG="${lfcache}/ueberzug-''$''$" + ${mkfifo} "$FIFO_UEBERZUG" + ${ueberzug} layer -s <"$FIFO_UEBERZUG" -p json & + exec 3>"$FIFO_UEBERZUG" + trap cleanup HUP INT QUIT TERM PWR EXIT + ${lf} "$@" + fi +'' diff --git a/home/lf/pv.nix b/home/lf/pv.nix new file mode 100644 index 0000000..e521272 --- /dev/null +++ b/home/lf/pv.nix @@ -0,0 +1,63 @@ +{ 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 ;; + text/* | */xml) ${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 +'' diff --git a/home/nixos-common.nix b/home/nixos-common.nix index 5cac7d0..0863dcf 100644 --- a/home/nixos-common.nix +++ b/home/nixos-common.nix @@ -22,6 +22,7 @@ ./gotify ./gpg ./idasen + ./lf ./mail ./maker ./minidisc