#!/bin/bash
# [c]at the given files as prettified [j]son
# TODO quit on interrupt
set -o pipefail

# jq if file is valid json, otherwise show parseable lines with bat
{ jq --unbuffered '' "$@" ||
# add echo in case file is missing newline at the end
{ for arg; do echo "FILE: $arg ─────────────────────────────────────────────────────────────────────────────────────────"; cat "$arg"; done; echo; } |
  while read -r line
  do
    line="${line%
}"
    if command expr length "$line" \> 2 \& "$line" : FILE = 0 >/dev/null
    then echo "${line%,}" | python3 -m json.tool --no-ensure-ascii --sort-keys
    else echo "$line"
    fi
  done } | bat --language json --style numbers
#bat --language json --style plain --pager json-format "$@"
