Skip to content

Instantly share code, notes, and snippets.

@atzorvas
Last active August 23, 2017 07:10
Show Gist options
  • Select an option

  • Save atzorvas/047b3733caa7635ebef7f02aa1300db0 to your computer and use it in GitHub Desktop.

Select an option

Save atzorvas/047b3733caa7635ebef7f02aa1300db0 to your computer and use it in GitHub Desktop.
beautify clipboard script w ruby/python/xclip
#!/usr/bin/ruby
# Requirements: xclip, python, ruby
#
# Copy your the json or ruby hash you wish to transform into a indented json
# run the script (eg. with a hotkey), and then paste
#
# Handles:
# * json
# * ruby hash
raise "please install xclip (or alter the script)" unless system("which xclip")
raise "please install xclip (or alter the script)" unless system("which python")
require 'json'
clipboard_contents = %x{ xclip -o }
begin
system("echo '#{eval(clipboard_contents).to_json}' | python -c 'import sys,json;print json.dumps(json.load(sys.stdin),indent=2)' | xclip -selection clipboard")
rescue
system("xclip -o | python -c 'import sys,json;print json.dumps(json.load(sys.stdin),indent=2)' | xclip -selection clipboard")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment