#!/usr/bin/env sh

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

OPENER='if [ ${FZF_SELECT_COUNT} -eq 0 ]; then
          "${EDITOR}" {1} +{2}     # no selection. open the current line in $EDITOR.
        elif [ ${EDITOR} = vim ] || [ ${EDITOR} = nvim ]; then
          "${EDITOR}" +cw -q {+f}  # build quickfix list for the selected items.
        else
          "${EDITOR}" {+1}
        fi'

fzf --no-height --tmux 100% --disabled --ansi --multi --query "${*:-}" \
    --bind "start,change:${RELOAD}" \
    --bind "enter:become:${OPENER}" \
    --bind "ctrl-o:execute:${OPENER}" \
    --bind 'alt-a:select-all,alt-d:deselect-all,ctrl-/:toggle-preview' \
    --delimiter : \
    --preview 'bat --color=always --highlight-line {2} {1}' \
    --preview-window 'default:up,border-bottom,~3,+{2}+3/3'
