Skip to content

Instantly share code, notes, and snippets.

@spicyjack
Last active December 22, 2015 13:28
Show Gist options
  • Save spicyjack/6478940 to your computer and use it in GitHub Desktop.
Save spicyjack/6478940 to your computer and use it in GitHub Desktop.
download_viewer.sh - download a JSON file using an API request (Doomworld in this case), then pretty print to JSON or YAML using JSON::XS, and pipe to vim for syntax highlighting. JSON output requires the 'json.vim' syntax highlighting scripts, located at: https://github.com/elzr/vim-json viewjson first added in https://github.com/spicyjack/publ…
_download_viewer () {
local OUTPUT_TYPE="$1"
local WGET_URL="$2"
JSON_XS_CMD=$(which json_xs)
VIM_CMD=$(which vim)
WGET_CMD=$(which wget)
if [ ! -x $JSON_XS_CMD -o ! -x $VIM_CMD -o ! $WGET_CMD ]; then
echo "ERROR: missing commands needed to run this function!"
echo "JSON_XS_CMD: ${JSON_XS_CMD}"
echo "VIM_CMD: ${VIM_CMD}"
echo "WGET_CMD: ${WGET_CMD}"
return 1
fi
if [ "x$1" == "x" ]; then
echo 'Usage: viewjson "http://example.com/api/request.json"'
echo "(Hint: quote URL to prevent shell from interpreting URL)"
return 1
else
$WGET_CMD -O - "$WGET_URL" | $JSON_XS_CMD \
| $VIM_CMD --cmd 'let no_plugin_maps = 1' \
-c 'runtime! macros/less.vim' -c "set filetype=$OUTPUT_TYPE" -
fi
unset JSON_XS_CMD VIM_CMD WGET_CMD
unset JSON_XS_CMD VIM_CMD WGET_CMD
} # view a downloaded file, pretty printed and syntax highlighted
viewjson () {
_download_viewer "json" "$1"
} # json front end to _download_viewer
viewyaml () {
_download_viewer "yaml" "$1"
} # yaml front end to _download_viewer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment