#!/bin/sh -e
#
# ---
# source: https://github.com/dandavison/tools

hyperlink() {
  url="$1"
  text="$2"
  printf '\e]8;;%s\e\\%s\e]8;;\e\\\n' "${url}" "${text}"
}

if echo "$@" | rg --quiet '(^| )\-[^- ]*l[^-]+' || [ "$*" = "* --files-with-matches *" ]; then
  rg --hidden --glob '!.git/*' "$@" | while read -r path; do
    abspath=$(readlink -f -- "${path}")
    url="cursor://file/${abspath}"
    hyperlink "${url}" "${path}"
  done
else
  rg --hidden --glob '!.git/*' --json "$@" | delta
fi
