#!/bin/sh -e

result=$(rg --ignore-case --color=always --line-number --no-heading "$@" |
  fzf --ansi \
      --color 'hl:-1:underline,hl+:-1:underline:reverse' \
      --delimiter ':' \
      --preview "bat --style=snip --color=always {1} --theme=ansi --highlight-line {2}" \
      --preview-window 'up,60%,border-bottom,+{2}+3/3,~3')

file="${result%%:*}"
linenumber="$(echo "${result}" | cut -d: -f2)"

if [ -n "${file}" ]; then
  ${EDITOR:-vi} +"${linenumber}" "${file}"
fi
