#!/usr/bin/env bash

cmd="rg --hidden --max-columns $(($(tput cols) - 27)) --max-columns-preview --multiline-dotall --auto-hybrid-regex"

# to use rg with a pager: `rg args... -p | less -RFX`
# automatically pipe into `less` if `-p` is used
# (but not if `--null` is used, to avoid invoking `less` from `pre`).
# use the long flag, `--pretty`, to skip piping to `less`.
seen_p=''
seen_null=''
for arg in "$@"; do
  if test "${arg}" = '--'; then
    break
  elif test "${arg}" = '-p'; then
    seen_p='true'
  elif test "${arg}" = '--null'; then
    seen_null='true'
  fi
done

if test -n "${seen_p}" -a -z "${seen_null}"; then
  ${cmd} "$@" | less -RFX
else
  ${cmd} "$@"
fi
