#!/usr/bin/env sh
#
# dots - easy access to wanted config/dotfiles with fd and fzf

set -e

filepath=${filepath:-${HOME%/}/.dotfiles}
configs='(bash|bookmark|bp|c|datafiles|etc|git|helix|kak|linux|macos|minimal|os|pandoc|perl|python|ruby|rust|scripts|shell|skeleton|snippets|uncrustify|uv|www)'
exclude='{bin,help}'

if [ -d "${filepath}" ]; then
  fd "${configs}" --exclude "${exclude}" --type=file --hidden \
    --full-path "${filepath}" --print0 | xargs -0 ls -t \
    | fzf --reverse --margin=5% \
      --tiebreak=index --delimiter='/' --with-nth=-2,-1 \
      --ansi --prompt="dots • " \
      --multi \
      --bind "enter:become(${EDITOR:-vi} {+} >/dev/tty)" \
      --bind "ctrl-e:execute(${EDITOR:-vi} {+} >/dev/tty)" \
      --bind "alt-e:execute(echo {+} | xe -N0 ${EDITOR:-vi})" \
      --bind 'ctrl-y:execute-silent(echo {} | pbcopy)' \
      --bind '?:toggle-preview' \
      --preview-window 'right,60%,border-left' \
      --preview 'bat --color=always --style=numbers --line-range=:500 {}'
else
  exit 2
fi
