#!/usr/bin/env sh
FD_COMMAND5="fd --changed-within 5d"
FD_COMMAND1="fd --changed-within 1d"


$FD_COMMAND5 -t f . "$@" |fzf \
  --info=inline \
  --multi \
  --layout=reverse \
  --prompt='ctl-wetcrfp_CWD5d> ' \
  --bind "ctrl-w:transform:echo 'change-prompt(ctl-wetcrfp_HOME5d> )+reload($FD_COMMAND5 -t f . ~ )'" \
  --bind "ctrl-e:transform:echo 'change-prompt(ctl-wetcrfp_HOME1d)+reload($FD_COMMAND1 -t f . ~)'" \
  --bind "ctrl-t:transform:echo 'change-prompt(ctl-wetcrfp_CWD5d> )+reload($FD_COMMAND5 -t f)'" \
  --bind "alt-c:transform:echo 'change-prompt(ctl-wetcrfp_CWD1d> )+reload($FD_COMMAND1 -t f)'" \
  --bind "ctrl-r:transform:echo 'change-prompt(ctl-wetcrfp_HOMEA> )+reload($FD_COMMAND5 . ~)'" \
  --bind "ctrl-f:transform:echo 'change-prompt(ctl-wetcrfp_HOMEA_WithH> )+reload($FD_COMMAND1 -H . ~)'" \
  --bind 'focus:transform-preview-label:echo {}' \
  --bind 'ctrl-p:change-preview-window(right|down|hidden)' \
  --bind 'focus:transform-preview-label:echo {}' \
  --bind 'ctrl-x:execute-silent(setsid kitty nnn {} &)' \
  --bind 'ctrl-a:execute-silent(setsid kitty yazi {} &)' \
  --bind 'ctrl-s:execute-silent(setsid thunar {} &)' \
  --preview-window 'hidden' \
--preview '
    printf "\033_Ga=d,q=1\033\\"
    [ -d {} ] && eza -aTL 2 --group-directories-first --color=always --icons=always {} ||
    tmp="/tmp/fzf_preview"
    case $(basename {}) in
        *.pdf)
            pdftoppm -q -f 1 -l 1 -jpeg -jpegopt quality=60 -scale-to-x 1000 -scale-to-y -1 -singlefile {} $tmp && \
            kitty icat --transfer-mode=memory --stdin=no --place=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}@0x0 $tmp.jpg
            ;;
        *.doc|*.docx|*.xls|*.xlsx|*.ppt|*.pptx|*.odt|*.ods|*.odp)
            libreoffice --convert-to jpg --outdir /tmp {} >/dev/null 2>&1
            mv "/tmp/${$(basename {})%.*}.jpg" $tmp
            kitty icat --transfer-mode=memory --stdin=no --place=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}@0x0 $tmp
            ;;
        *.djvu)
            ddjvu -format=ppm -page=1 -size 1000x-1 -aspect=yes {} $tmp 2>/dev/null && \
            kitty icat --transfer-mode=memory --stdin=no --place=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}@0x0 $tmp
            ;;
        *)
            if file --mime-type {} | grep -qP "image/(?!vnd\.djvu)"; then
                kitty icat --transfer-mode=memory --stdin=no --place=${FZF_PREVIEW_COLUMNS}x${FZF_PREVIEW_LINES}@0x0 {}
            else
                bat -p {} --color=always 2>/dev/null
            fi
            ;;
    esac
'
