Skip to content

Instantly share code, notes, and snippets.

@nickanderson
Created May 26, 2010 16:30
Show Gist options
  • Save nickanderson/414714 to your computer and use it in GitHub Desktop.
Save nickanderson/414714 to your computer and use it in GitHub Desktop.
#!/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.1.1'
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 = defaults["private"]
gist_filename = nil
gist_extension = defaults["extension"]
opts = OptionParser.new do |opts|
opts.banner = "Usage: gist [options] [filename or stdin]"
opts.on('-p', '--[no-]private', 'Make the gist private') do |priv|
private_gist = priv
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('-v', '--version', 'Print version') do
puts Gist::Version
exit
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_filename = 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, gist_filename)
#browse(url)
#puts url
#puts copy(url)
rescue => e
warn e
puts opts
end
end
def write(content, private_gist = false, gist_extension = nil, gist_filename = nil)
url = URI.parse(CREATE_URL)
proxy = Net::HTTP::Proxy(PROXY_HOST, PROXY_PORT)
req = proxy.post_form(url,
data(gist_filename, gist_extension, content, private_gist))
puts req['Location']
end
def read(gist_id)
open(GIST_URL % gist_id).read
end
def browse(url)
if RUBY_PLATFORM =~ /darwin/
`open #{url}`
elsif ENV[:OS] == 'Windows_NT' or
RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw|wince/i
`start "" "#{url}"`
end
end
def copy(content)
cmd = case true
when system("type pbcopy > /dev/null")
:pbcopy
when system("type xclip > /dev/null")
:xclip
when system("type 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 = config("github.user")
token = config("github.token")
user.empty? ? {} : { :login => user, :token => token }
end
def defaults
priv = config("gist.private")
extension = config("gist.extension")
extension = nil if extension && extension.empty?
return {
"private" => priv,
"extension" => extension
}
end
def config(key)
str_to_bool `git config --global #{key}`.strip
end
def str_to_bool(str)
if str.size > 0 and str[0].chr == '!'
command = str[1, str.length]
value = `#{command}`
else
value = str
end
case value.downcase.strip
when "false", "0", "nil", "", "no", "off"
nil
when "true", "1", "yes", "on"
true
else
value
end
end
end
Gist.execute(*ARGV)
__END__
.\" generated with Ronn/v0.4.1
.\" http://github.com/rtomayko/ronn/
.
.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