Created
August 13, 2010 12:36
-
-
Save mhansen/522794 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ruby | |
# | |
# This file, gist, is generated code. | |
# Please DO NOT EDIT or send patches for it. | |
# | |
# Please take a look at the source from | |
# http://github.com/defunkt/gist | |
# and submit patches against the individual files | |
# that build gist. | |
# | |
module Gist | |
module Manpage | |
extend self | |
def display(name) | |
puts manpage(name) | |
end | |
def manpage(name) | |
return "** Can't find groff(1)" unless groff? | |
require 'open3' | |
out = nil | |
Open3.popen3(groff_command) do |stdin, stdout, _| | |
stdin.puts raw_manpage(name) | |
stdin.close | |
out = stdout.read.strip | |
end | |
out | |
end | |
def raw_manpage(name) | |
if File.exists? file = File.dirname(__FILE__) + "/../../man/#{name}.1" | |
File.read(file) | |
else | |
DATA.read | |
end | |
end | |
def groff? | |
system("which groff > /dev/null") | |
end | |
def groff_command | |
"groff -Wall -mtty-char -mandoc -Tascii" | |
end | |
def puts(*args) | |
page_stdout | |
super | |
end | |
def page_stdout | |
return unless $stdout.tty? | |
read, write = IO.pipe | |
if Kernel.fork | |
$stdin.reopen(read) | |
read.close | |
write.close | |
ENV['LESS'] = 'FSRX' | |
Kernel.select [STDIN] | |
pager = ENV['PAGER'] || 'less -isr' | |
pager = 'cat' if pager.empty? | |
exec pager rescue exec "/bin/sh", "-c", pager | |
else | |
$stdout.reopen(write) | |
$stderr.reopen(write) if $stderr.tty? | |
read.close | |
write.close | |
end | |
end | |
end | |
end | |
module Gist | |
VERSION = Version = '1.0.3' | |
end | |
require 'open-uri' | |
require 'net/http' | |
require 'optparse' | |
require 'gist/manpage' unless defined?(Gist::Manpage) | |
require 'gist/version' unless defined?(Gist::Version) | |
module Gist | |
extend self | |
GIST_URL = 'http://gist.github.com/%s.txt' | |
CREATE_URL = 'http://gist.github.com/gists' | |
PROXY = ENV['HTTP_PROXY'] ? URI(ENV['HTTP_PROXY']) : nil | |
PROXY_HOST = PROXY ? PROXY.host : nil | |
PROXY_PORT = PROXY ? PROXY.port : nil | |
def execute(*args) | |
private_gist = false | |
gist_extension = nil | |
opts = OptionParser.new do |opts| | |
opts.banner = "Usage: gist [options] [filename or stdin]" | |
opts.on('-p', '--private', 'Make the gist private') do | |
private_gist = true | |
end | |
t_desc = 'Set syntax highlighting of the Gist by file extension' | |
opts.on('-t', '--type [EXTENSION]', t_desc) do |extension| | |
gist_extension = '.' + extension | |
end | |
opts.on('-m', '--man', 'Print manual') do | |
Gist::Manpage.display("gist") | |
end | |
opts.on('-h', '--help', 'Display this screen') do | |
puts opts | |
exit | |
end | |
end | |
opts.parse!(args) | |
begin | |
if $stdin.tty? | |
if args.empty? | |
puts opts | |
exit | |
end | |
if File.exists?(file = args[0]) | |
input = File.read(file) | |
gist_extension = File.extname(file) if file.include?('.') | |
else | |
abort "Can't find #{file}" | |
end | |
else | |
input = $stdin.read | |
end | |
url = write(input, private_gist, gist_extension) | |
browse(url) | |
puts copy(url) | |
rescue => e | |
warn e | |
puts opts | |
end | |
end | |
def write(content, private_gist = false, gist_extension = nil) | |
url = URI.parse(CREATE_URL) | |
proxy = Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT) | |
req = proxy.post_form(url, data(nil, gist_extension, content, private_gist)) | |
req['Location'] | |
end | |
def read(gist_id) | |
open(GIST_URL % gist_id).read | |
end | |
def browse(url) | |
if RUBY_PLATFORM =~ /darwin/ | |
`open #{url}` | |
end | |
end | |
def copy(content) | |
cmd = case true | |
when system("which pbcopy &> /dev/null") | |
:pbcopy | |
when system("which xclip &> /dev/null") | |
:xclip | |
when system("which putclip &> /dev/null") | |
:putclip | |
end | |
if cmd | |
IO.popen(cmd.to_s, 'r+') { |clip| clip.print content } | |
end | |
content | |
end | |
private | |
def data(name, ext, content, private_gist) | |
return { | |
'file_ext[gistfile1]' => ext ? ext : '.txt', | |
'file_name[gistfile1]' => name, | |
'file_contents[gistfile1]' => content | |
}.merge(private_gist ? { 'action_button' => 'private' } : {}).merge(auth) | |
end | |
def auth | |
user = `git config --global github.user`.strip | |
token = `git config --global github.token`.strip | |
user.empty? ? {} : { :login => user, :token => token } | |
end | |
end | |
Gist.execute(*ARGV) | |
__END__ | |
.\" generated with Ron/v0.3 | |
.\" http://github.com/rtomayko/ron/ | |
. | |
.TH "GIST" "1" "February 2010" "GITHUB" "Gist Manual" | |
. | |
.SH "NAME" | |
\fBgist\fR \-\- gist on the command line | |
. | |
.SH "SYNOPSIS" | |
\fBgist\fR [\fB\-p\fR] [\fB\-t extension\fR] \fIFILE\fR | |
. | |
.SH "DESCRIPTION" | |
\fBgist\fR can be used to create gists on gist.github.com from the command | |
line. There are two primary methods of creating gists. | |
. | |
.P | |
If standard input is supplied, it will be used as the content of the | |
new gist. If \fIFILE\fR is provided, the content of that file will be used | |
to create the gist. | |
. | |
.P | |
Once your gist is successfully created, the URL will be copied to your | |
clipboard. If you are on OS X, \fBgist\fR will open the gist in your | |
browser, too. | |
. | |
.SH "OPTIONS" | |
\fBgist\fR's default mode of operation is to read content from standard | |
input and create a public, text gist from it, tied to your GitHub | |
account if you user and token are provided (see \fBCONFIGURATION\fR). | |
. | |
.P | |
These options can be used to change this behavior: | |
. | |
.TP | |
\fB\-p\fR, \fB\-\-private\fR | |
Create a private gist instead of a public gist. | |
. | |
.TP | |
\fB\-t\fR, \fB\-\-type\fR | |
Set the file extension explicitly. Passing a type of \fBrb\fR ensure | |
the gist is created as a Ruby file. | |
. | |
.P | |
You may additionally ask for help: | |
. | |
.TP | |
\fB\-h\fR, \fB\-\-help\fR | |
Print help. | |
. | |
.TP | |
\fB\-m\fR, \fB\-\-man\fR | |
Display this man page. | |
. | |
.SH "CONFIGURATION" | |
Use git\-config(1) to display the currently configured GitHub username: | |
. | |
.IP "" 4 | |
. | |
.nf | |
$ git config \-\-global github.user | |
. | |
.fi | |
. | |
.IP "" 0 | |
. | |
.P | |
Or, set the GitHub username with: | |
. | |
.IP "" 4 | |
. | |
.nf | |
$ git config \-\-global github.user <username> | |
. | |
.fi | |
. | |
.IP "" 0 | |
. | |
.P | |
See \fIhttp://github.com/guides/local\-github\-config\fR for more | |
information. | |
. | |
.P | |
\fBgist\fR will check the \fBHTTP_PROXY\fR env variable if supplied: | |
. | |
.IP "" 4 | |
. | |
.nf | |
$ HTTP_PROXY=http://host:port/ gist script.py | |
. | |
.fi | |
. | |
.IP "" 0 | |
. | |
.SH "EXAMPLES" | |
. | |
.nf | |
$ gist < file.txt | |
$ echo secret | gist \-\-private | |
$ echo "puts :hi" | gist \-t rb | |
$ gist script.py | |
. | |
.fi | |
. | |
.SH "BUGS" | |
\fIhttp://github.com/defunkt/gist/issues\fR | |
. | |
.SH "AUTHOR" | |
Chris Wanstrath :: [email protected] | |
. | |
.SH "SEE ALSO" | |
hub(1), git(1), git\-clone(1),\fIhttp://github.com\fR, \fIhttp://github.com/defunkt/gist\fR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment