From 9b0af44766d2a2cc61ae0142ac3ec9f0bed75674 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Wed, 28 Dec 2011 20:09:49 +0100 Subject: Allow piping to the `json` function --- .functions | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to '.functions') diff --git a/.functions b/.functions index b5270b3..a6a576d 100644 --- a/.functions +++ b/.functions @@ -9,9 +9,17 @@ function httpcompression() { encoding="$(curl -LIs -H 'User-Agent: Mozilla/5 Gecko' -H 'Accept-Encoding: gzip,deflate,compress,sdch' "$1" | grep '^Content-Encoding:')" && echo "$1 is encoded using ${encoding#* }" || echo "$1 is not using any encoding" } -# Syntax-highlight JSON files +# Syntax-highlight JSON strings or files function json() { - python -mjson.tool <<< "$*" | pygmentize -l javascript + if [ -p /dev/stdin ]; then + # piping, e.g. `echo '{"foo":42}' | json` + while read data; do + python -mjson.tool <<< "$data" | pygmentize -l javascript + done + else + # e.g. `json '{"foo":42}'` + python -mjson.tool <<< "$*" | pygmentize -l javascript + fi } # All the dig info -- cgit v1.2.3