dotfiles/.local/bin/scripts/cj

19 lines
929 B
Plaintext
Raw Normal View History

2022-08-30 13:19:41 +00:00
#!/bin/bash
2021-07-29 14:02:20 +00:00
# [c]at the given files as prettified [j]son
2022-08-29 15:17:42 +00:00
# TODO quit on interrupt
2022-08-30 13:19:41 +00:00
set -o pipefail
2022-07-18 10:56:48 +00:00
2022-08-30 13:19:41 +00:00
# jq if file is valid json, otherwise show parseable lines with bat
unbuffer jq --unbuffered '' "$@" | less -F ||
2022-07-18 10:56:48 +00:00
# add echo in case file is missing newline at the end
2022-08-30 13:19:41 +00:00
{ 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
2022-08-29 15:17:42 +00:00
done | bat --language json --style numbers
2022-08-30 13:19:41 +00:00
#bat --language json --style plain --pager json-format "$@"