#!/usr/bin/env sh
#
# modified from
# https://github.com/junegunn/fzf/blob/607eacf8c73f60d778c4a0bf303f1593704b17c3/ADVANCED.md#using-fzf-as-the-secondary-filter
# - search for files matching the input using fd
# - narrow down the list with fzf
# - open the file in vim

rg --hidden --color=always --line-number --no-heading --smart-case "${*:-}" |
  fzf --ansi \
    --reverse \
    --color "hl:-1:underline,hl+:-1:underline:reverse" \
    --delimiter : \
    --preview 'fzf-preview {1} {2}' \
    --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
    --height ~60 \
    --bind "enter:become(${EDITOR:-vim} {1} +{2})"



# ,fg - rg fzf vim
#
# - search for files matching the input
# - narrow down the list with fzf
# - open the file in vim

rg --hidden --color=always --line-number --no-heading --smart-case "${*:-}" \
  | fzf --ansi \
    --reverse \
    --color='hl:-1:underline,hl+:-1:underline:reverse' \
    --delimiter=":" \
    --height=~60 \
    --preview='prvw {1} {2}' \
    --preview-window='up,60%,border-bottom,+{2}+3/3,~3' \
    --bind="enter:become(${EDITOR:-vi} {1} +{2})" \
    --bind 'ctrl-/:toggle-preview'


# modified from:
# https://github.com/junegunn/fzf/blob/607eacf8c73f60d778c4a0bf303f1593704b17c3/ADVANCED.md#using-fzf-as-the-secondary-filter
#
# - search for files matching the input using fd
# - narrow down the list with fzf
# - open the file in vim
# rg --hidden --color=always --line-number --no-heading --smart-case "${*:-}" |
#   fzf --ansi \
#       --reverse \
#       --color "hl:-1:underline,hl+:-1:underline:reverse" \
#       --delimiter : \
#       --preview 'fzf-preview {1} {2}' \
#       --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
#       --height ~60 \
#       --bind 'enter:become(nvim {1} +{2})'

# https://github.com/junegunn/fzf/blob/master/ADVANCED.md#using-fzf-as-interactive-ripgrep-launcher
# 1. Search for text in files using Ripgrep
# 2. Interactively restart Ripgrep with reload action
# 3. Open the file in Vim
RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case "
INITIAL_QUERY="${*:-}"
fzf --ansi --disabled --query "${INITIAL_QUERY}" \
    --bind="start:reload:${RG_PREFIX} {q}" \
    --bind="change:reload:sleep 0.1; ${RG_PREFIX} {q} || true" \
    --delimiter=":" \
    --preview='fzf-preview {1} {2}' \
    --preview-window='right,60%,border,+1/1' \
    --bind="enter:become(${EDITOR:-vi} {1} +{2})"

    # replaced with my own custom fzf-preview
    # --preview 'bat --color=always {1} --highlight-line {2}'
    # replaced with my preference for preview window to the right
    # --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' \
