Skip to content

Instantly share code, notes, and snippets.

@solidgoldpig
Last active February 10, 2016 18:23
Show Gist options
  • Save solidgoldpig/b1d095931d5ab32fbb79 to your computer and use it in GitHub Desktop.
Save solidgoldpig/b1d095931d5ab32fbb79 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
'use strict';
// Reads JSON from stdin and writes equivalent nicely-formatted JSON to stdout.
let vm = require('vm')
let spaces = process.argv[2] || '0';
spaces = spaces.replace(/\\t/g, '\t').replace(/\\n/g, '\n');
let numberSpaces = Number(spaces.trim() === '' ? NaN : String(spaces))
if (!isNaN(numberSpaces)) {
spaces = numberSpaces
}
let inputChunks = []
let stdin = process.stdin
stdin.resume()
stdin.setEncoding('utf8')
stdin.on('data', function (chunk) {
inputChunks.push(chunk)
})
stdin.on('end', function () {
let parsedData
let inputJSON = inputChunks.join()
try {
let sandbox = {}
let resultKey = 'SAFE_EVAL_' + Math.floor(Math.random() * 1000000)
sandbox[resultKey] = {}
inputJSON = resultKey + '=' + inputJSON
vm.runInNewContext(inputJSON, sandbox)
let outputJSON = JSON.stringify(sandbox[resultKey], null, spaces);
process.stdout.write(outputJSON);
} catch (e) {
process.stderr.write(`Failed to parse invalid data:\n-----------------\n${inputJSON}\n-----------------\n`)
process.exit(1)
}
})
#!/bin/sh
PBWHICH=$(which xsel)
if [ "$PBWHICH" ]; then
PBCOPY='xsel --clipboard --input'
PBPASTE='xsel --clipboard --output'
fi
PBWHICH=$(which xclip)
if [ "$PBWHICH" ]; then
PBCOPY='xclip -selection clipboard'
PBPASTE='xclip -selection clipboard -o'
fi
PBWHICH=$(which pbcopy)
if [ "$PBWHICH" ]; then
PBCOPY='pbcopy'
PBPASTE='pbpaste'
fi
if [ -z "$PBCOPY" ]; then
echo 'No pbcopy/paste available'
exit 1
fi
$PBPASTE | jsonify.js "$@" | $PBCOPY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment