strip_ansi() {
  # remove ANSI escape sequences (CSI/SGR, etc.)
  sed -E 's/\x1B\[[0-9;]*[[:alpha:]]//g'
}
trim() {
  sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//'
}
FILE_IN="${1:-}"
LINE_SPEC_IN="${2:-}"
# --- sanitize inputs ---
FILE="$(printf '%s' "${FILE_IN}" | strip_ansi | trim)"
LINE_SPEC_RAW="$(printf '%s' "${LINE_SPEC_IN}" | strip_ansi | trim)"


# If no file provided, nothing to preview
if [ -z "${FILE}" ] || ! [ -e "${FILE}" ]; then
  echo "No file or file not found: ${FILE}" >&2
  exit 0
fi

# If no line spec, just show the file
if [ -z "${LINE_SPEC_RAW}" ]; then
  # shellcheck disable=SC2086
  exec bat -- "${FILE}"
fi

# extract up to two integers from the line spec robustly (no mapfile needed)
# NUMS_STR="$(printf '%s\n' "${LINE_SPEC_RAW}" |
#   tr -c '0-9' ' ' |
#   awk '{for(i=1;i<=NF;i++) if($i!="") print $i}' |
#   head -n2 |
#   paste -sd' ' -)"
