Last active
December 16, 2015 14:20
-
-
Save joyrexus/5447983 to your computer and use it in GitHub Desktop.
List gists of a user and select one to open.
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
#!/usr/bin/env coffee | |
### | |
gists - List gists of a user and select one to open. | |
`gists USER` will enumerate the most recent gists of USER: | |
$ gists joyrexus | |
0 Decorator for aliasing class methods. | |
1 Powerset implementations in coffeescript. | |
2 Sample unix-style filter that reads from STDIN sans args. | |
... | |
You're then prompted to open a particular gist by number: | |
... open gist # | |
The selected gist will then be opened in your default browser. | |
### | |
{exec} = require 'child_process' | |
request = require 'request' | |
user = if process.argv.length > 2 then process.argv[2] else 'joyrexus' | |
{stdin, stdout, exit} = process | |
print = console.log | |
ask = (question, format = /.*/, handle) -> | |
stdin.resume() | |
stdout.write question | |
stdin.once 'data', (d) -> | |
input = d.toString() | |
if format.test input then handle input else exit() | |
results = [] # cache results | |
choose = (e, r, gists) -> | |
results = ({id: g.id, desc: g.description} for g in gists) | |
print i, g.desc for i, g of results | |
ask '\n ... open gist # ', /^\d/, (input) -> i = input.toString().trim() | |
url = "https://gist.github.com/#{user}/#{results[i].id}" | |
exec "open #{url}", (err, stdout, stderr) -> if err then console.error stderr | |
exit() | |
options = | |
url: "https://api.github.com/users/#{user}/gists" | |
json: true | |
request.get options, choose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment