diff options
| author | Akshay Nair <phenax5@gmail.com> | 2020-12-21 18:52:51 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2020-12-21 18:52:51 +0530 |
| commit | f1cf59ce2df9204f818cfd038538a2a31a5c2262 (patch) | |
| tree | 35b0a21501615b7551514262b96e9d8113375cbc /config/qutebrowser/userscripts/format_json | |
| parent | 6ff5a17adcf1ed72cd2f1c7bff606bdd3352b3bb (diff) | |
| download | nixos-config-f1cf59ce2df9204f818cfd038538a2a31a5c2262.tar.gz nixos-config-f1cf59ce2df9204f818cfd038538a2a31a5c2262.zip | |
Adds qutebrowser config
Diffstat (limited to 'config/qutebrowser/userscripts/format_json')
| -rwxr-xr-x | config/qutebrowser/userscripts/format_json | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/config/qutebrowser/userscripts/format_json b/config/qutebrowser/userscripts/format_json new file mode 100755 index 0000000..1977078 --- /dev/null +++ b/config/qutebrowser/userscripts/format_json @@ -0,0 +1,42 @@ +#!/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:-fruity} + +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" |
