bin: handle exotic json in cj and b

This commit is contained in:
xeruf 2022-08-30 15:19:41 +02:00
parent cee9843528
commit 936c4ab5e2
3 changed files with 43 additions and 7 deletions

View File

@ -99,7 +99,7 @@ for arg; do
case "$(file --dereference "$arg")" in case "$(file --dereference "$arg")" in
(*\ ?udio*) (*\ ?udio*)
# TODO preconvert aac - |*\ ADTS\ * # TODO preconvert aac - |*\ ADTS\ *
if ! $inspect && which audiowaveform 2>/dev/null >&2; then if ! $inspect && which audiowaveform >/dev/null; then
img="$tmpfile.png" img="$tmpfile.png"
case $TERM in (*-kitty) kitty=true; audioheight=2;; (*) audioheight=5;; esac case $TERM in (*-kitty) kitty=true; audioheight=2;; (*) audioheight=5;; esac
find "$img" -not -empty 2>/dev/null | grep --quiet . || find "$img" -not -empty 2>/dev/null | grep --quiet . ||
@ -177,7 +177,8 @@ if test "$bat" -o "$batplain"; then
batcommand="$elevate bat $cut $flags --pager" batcommand="$elevate bat $cut $flags --pager"
batstyle="--style plain$rule" batstyle="--style plain$rule"
test "$batplain" && $batcommand "${batpager:-$pager}" $batstyle "${batplain[@]}" test "$batplain" && $batcommand "${batpager:-$pager}" $batstyle "${batplain[@]}"
test "$bat" && if test "$cut" && ! $inspect test "$bat" &&
if test "$cut" && ! $inspect
then case $TERM in (*kitty) then case $TERM in (*kitty)
declare -a timgtxt declare -a timgtxt
for file in "${bat[@]}" for file in "${bat[@]}"
@ -193,8 +194,23 @@ if test "$bat" -o "$batplain"; then
fileinfo "$file" fileinfo "$file"
done;; done;;
esac esac
else $inspect || $batcommand "${batpager:-$pager}" $batstyle,header$(test $# -gt 1 && echo ",numbers") "${bat[@]}" else
{ $inspect || test $# -lt $(expr $(tput lines) / 3); } && fileinfo "${bat[@]}" if ! $inspect
then json=false
if which cj >/dev/null; then
json=true
for file in "${bat[@]}"; do
case $file in (*.json);; (*) json=false;; esac
done
fi
if $json
then cj "${bat[@]}"
else $batcommand "${batpager:-$pager}" $batstyle,header$(test $# -gt 1 && echo ",numbers") "${bat[@]}"
fi
fi
if $inspect || test $# -lt $(expr $(tput lines) / 3);
then fileinfo "${bat[@]}"
fi
fi fi
fi fi

View File

@ -1,8 +1,18 @@
#!/bin/sh #!/bin/bash
# [c]at the given files as prettified [j]son # [c]at the given files as prettified [j]son
# TODO quit on interrupt # TODO quit on interrupt
set -o pipefail
# jq if file is valid json, otherwise show parseable lines with bat
unbuffer jq --unbuffered '' "$@" | less -F ||
# add echo in case file is missing newline at the end # add echo in case file is missing newline at the end
{ cat "$@"; echo; } | while read -r line { for arg; do echo "FILE: $arg ─────────────────────────────────────────────────────────────────────────────────────────"; cat "$arg"; done; echo; } |
do test -n "$line" && python -c "import json; print(json.dumps($line, ensure_ascii=False))" 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 done | bat --language json --style numbers
#bat --language json --style plain --pager json-format "$@"

10
.local/bin/scripts/json-format Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh -e
# Format json as bat pager with python json.tool
while read -r line
do
echo $line | bat -A
line="$(echo ${line% } | sed "s|.*\[0m\(.*\)|\1|")"
echo $line | bat -A
command expr length "$line" \> 2 &&
echo "${line%,}" | python3 -m json.tool --no-ensure-ascii --sort-keys
done