summaryrefslogtreecommitdiff
path: root/misc/userscripts/format_json
blob: 8a83c25fa067f2d10c57aee1dafaaf8291ae1e67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env bash
set -euo pipefail
#
# Behavior:
#   Userscript for qutebrowser which will take the raw JSON text of the current
#   page, format it using `jq`, will add syntax highlighting using `pygments`,
#   and open the syntax highlighted pretty printed html in a new tab. If the file
#   is larger than 10MB then this script will only indent the json and will forego
#   syntax highlighting using pygments.
#
#   In order to use this script, just start it using `spawn --userscript` from
#   qutebrowser. I recommend using an alias, e.g.
#
#     :config-dict-add aliases json "spawn --userscript /path/to/json_format"
#
#   Note that the color style defaults to monokai, but a different pygments style
#   can be passed as the first parameter to the script. A full list of the pygments
#   styles can be found at: https://help.farbox.com/pygments.html
#
# Bryan Gilbert, 2017

# do not run pygmentize on files larger than this amount of bytes
MAX_SIZE_PRETTIFY=10485760  # 10 MB
# default style to monokai if none is provided
STYLE=${1:-monokai}

TEMP_FILE="$(mktemp --suffix .html)"
jq . "$QUTE_TEXT" >"$TEMP_FILE"

# try GNU stat first and then OSX stat if the former fails
FILE_SIZE=$(
  stat --printf="%s" "$TEMP_FILE" 2>/dev/null ||
  stat -f%z "$TEMP_FILE" 2>/dev/null
)
if [ "$FILE_SIZE" -lt "$MAX_SIZE_PRETTIFY" ]; then
    pygmentize -l json -f html -O full,style="$STYLE" <"$TEMP_FILE" >"${TEMP_FILE}_"
    mv -f "${TEMP_FILE}_" "$TEMP_FILE"
fi

# send the command to qutebrowser to open the new file containing the formatted json
echo "open -t file://$TEMP_FILE" >> "$QUTE_FIFO"