#!/bin/sh -e
#
# vt - list/edit latest files with fzf

preview='([ -f {} ] && (bat --style=snip --color=always {})) || ([ -d {} ] && (eza -TL 3 --color=always --icons {} | less)) || echo {} 2>/dev/null | head -n 200'

if command -v fzf >/dev/null 2>&1; then
  find=$(command -v gfind || echo find)
  # preview='bat --color=always --style=snip {} || file -L -i {}'
  if [ "$#" -eq 0 ]; then
    ${find} -L . -path '*/.git' -prune -o -type f \
      -exec ls -Atp -- {} + \
      | sed "s|${PWD}/||" \
      | fzf --reverse --exit-0 --multi --preview-window='right' \
        --preview="${preview} {}" \
        --bind="enter:become(${EDITOR:-vi} {1})" \
        --bind="ctrl-y:execute-silent(ruby -e 'puts ARGV' {+} | pbcopy)"
    exit
  fi
  if [ "$#" -gt 0 ]; then
    case "$#" in
      2)
        # if echo "$1" | grep -Eq "[0-9]" >/dev/null; then
        # find -- . -path '*/.git' -prune -o -type f -exec ls -Atp -- {} + | head -n 10
        case "$1" in
          *[[:digit:]]*)
          ${find} -L "$2" -path '*/.git' -prune -o -type f \
            -exec ls -Atp -- {} + \
            | head -n "$1" \
            | fzf --reverse --multi --delimiter='/' --with-nth=-1 \
              --preview-window='right' \
              --preview="${preview} {}" \
              --bind "enter:execute(echo {+} | xargs -o ${EDITOR:-vi})" \
              --bind "ctrl-y:execute-silent(ruby -e 'puts ARGV' {+} | pbcopy)"
          ;;
          *)
          ${find} -L "$1" -path '*/.git' -prune -o -type f -exec ls -Atp -- {} + \
            | head -n "$2" \
            | fzf --reverse --multi --delimiter='/' --with-nth=1 \
              --bind "enter:execute(${EDITOR:-vi} {+})" \
              --bind "ctrl-y:execute-silent(ruby -e 'puts ARGV' {+} | pbcopy)" \
              --preview-window='right' \
              --preview="${preview} {}"
        esac
        ;;
      *)
        if echo "$1" | grep -E '^[0-9]+(\.[0-9]+)?$' >/dev/null; then
            # ${find} -- . -path '*/.git' -prune -o -type f
            # ${find} -L . '(' -path '*.git' ')' -prune -o -type f -print0 \
            find -L . '(' -path '*/.git' ')' -prune -o -type f -printf "%P\\n" \
              -exec ls -Atp -- {} + \
              | sed "s|${PWD}/||" \
              | head -n "$1" \
              | fzf --reverse --multi --preview-window='right' \
                --preview 'bat --color=always --style=snip -- {}' \
                --bind "enter:become(${EDITOR:-vi} {1})" \
                --bind "ctrl-y:execute-silent(ruby -e 'puts ARGV' {+} | pbcopy)"
        else
          # find . -path './.git' -prune -o -printf "%P\\n"
          ${find} -L "$1" -path '*/.git' -prune -o -type f \
            -exec ls -Atp -- {} + \
            | head -n 30 \
            | fzf --reverse --multi --delimiter='/' --with-nth=-1 \
              --preview-window='right' \
              --preview 'bat --color=always --style=snip {}' \
              --bind "enter:execute:(${EDITOR:-vi} {} >/dev/tty </dev/tty)" \
              --bind "ctrl-y:execute-silent(ruby -e 'puts ARGV' {+} | pbcopy)"
        fi
        ;;
    esac
  fi
else
  if [ "$#" -eq 0 ]; then
    # find . -type f -exec ls -At {} \;
    file=$(find . -type f -exec ls -Atp {} + | command tail -n1)
    ${EDITOR:-vi} "${file-}"
  else
    tail -n11000 "$1" \
      | ${EDITOR:-vi} -c 'setlocal buftype=nofile bufhidden=hide noswapfile | match | $'
  fi
fi
