#!/usr/bin/env sh
#
# rfv - ripgrep->fzf->vim [QUERY] with quickfix list function
# dependencies: fzf | ripgrep | vim/nvim
#
# ---
# see: https://junegunn.github.io/fzf/tips/ripgrep-integration/
# see: https://github.com/junegunn/everything.fzf/blob/main/rg.fzf

# tmpdir="${TMPDIR:-/tmp}"
# rm -f "${tmpdir}"/fzf-temp-*

RELOAD='reload:rg --hidden --follow --column --color=always --multiline --smart-case {q} || true'

# shellcheck disable=SC2016
# no selection => open current line in $EDITOR || build quickfix list
OPENER='if [ "${FZF_SELECT_COUNT}" -eq 0 ]; then
          "${EDITOR}" {1} +{2}
        else
          "${EDITOR}" +cw -q {+f}
        fi'

# shellcheck disable=SC2016
# fzf --layout=default --disabled --ansi --multi --query "${*:-}" \
# --preview-window 'up,border-bottom,~3,+{2}+3/3'
# --terminal-width=${FZF_PREVIEW_COLUMNS}
fzf --no-height --tmux 100% --disabled --ansi --multi --query "${*:-}" \
  --bind="start,change:${RELOAD}" \
  --bind="enter:become:${OPENER}" \
  --bind="ctrl-o:execute:${OPENER}" \
  --bind="alt-e:execute:${OPENER}" \
  --bind='alt-a:select-all,alt-d:deselect-all' \
  --bind='ctrl-y:execute-silent(printf %s {1} | pbcopy)+bell' \
  --bind='ctrl-y:+transform-footer(echo Copied {} to clipboard)' \
  --bind='ctrl-y:+bg-cancel+bg-transform-footer(sleep 1)' \
  --bind='ctrl-/:toggle-preview,alt-w:toggle-preview-wrap' \
  --delimiter=":" \
  --preview='bat --style=header-filename --color=always --highlight-line {2} {1}' \
  --preview-window='~4,+{2}+4/3,<80(up,65%)'
