Last active
December 22, 2015 13:28
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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