From e664bba35e48f216e100862cb6d6dc2fd585f755 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Mon, 23 Sep 2013 16:24:01 +0200 Subject: [PATCH] Pretty print JSON --- bin/json | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100755 bin/json diff --git a/bin/json b/bin/json new file mode 100755 index 0000000..254299f --- /dev/null +++ b/bin/json @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby + +require 'rubygems' # 1.8.7 +require 'json' + +if STDIN.tty? + if File.exist?("/usr/bin/pbpaste") + data = IO.popen('/usr/bin/pbpaste', 'r+').read + else + $stderr.puts 'your_command | json [--color]' + exit 1 + end +else + data = STDIN.read +end + +json = JSON.parse(data) +if ARGV.include?('--color') + require 'ap' + ap json +else + puts JSON.pretty_generate(json) +end