summaryrefslogtreecommitdiff
path: root/.config/qutebrowser/misc/userscripts/format_json
diff options
context:
space:
mode:
Diffstat (limited to '.config/qutebrowser/misc/userscripts/format_json')
-rwxr-xr-x.config/qutebrowser/misc/userscripts/format_json42
1 files changed, 42 insertions, 0 deletions
diff --git a/.config/qutebrowser/misc/userscripts/format_json b/.config/qutebrowser/misc/userscripts/format_json
new file mode 100755
index 0000000..0d476b3
--- /dev/null
+++ b/.config/qutebrowser/misc/userscripts/format_json
@@ -0,0 +1,42 @@
+#!/bin/sh
+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. put this in the
+# [alias]-section of qutebrowser.conf:
+#
+# 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)"
+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"