2012-06-23 15:56:54 -04:00
|
|
|
class Pry
|
2012-12-25 16:35:17 -05:00
|
|
|
class Command::Gist < Pry::ClassCommand
|
|
|
|
match 'gist'
|
2012-08-11 21:26:59 -04:00
|
|
|
group 'Misc'
|
2013-04-20 19:55:01 -04:00
|
|
|
description 'Upload code, docs, history to https://gist.github.com/.'
|
2018-10-12 15:09:29 -04:00
|
|
|
command_options requires_gem: "gist"
|
2012-08-11 21:26:59 -04:00
|
|
|
|
2013-01-15 14:08:32 -05:00
|
|
|
banner <<-'BANNER'
|
|
|
|
Usage: gist [OPTIONS] [--help]
|
2013-01-09 15:23:19 -05:00
|
|
|
|
2013-01-15 14:08:32 -05:00
|
|
|
The gist command enables you to gist code from files and methods to github.
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2013-01-15 14:08:32 -05:00
|
|
|
gist -i 20 --lines 1..3
|
|
|
|
gist Pry#repl --lines 1..-1
|
|
|
|
gist Rakefile --lines 5
|
|
|
|
BANNER
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def setup
|
2014-04-28 01:23:54 -04:00
|
|
|
require 'gist'
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def options(opt)
|
2013-01-15 14:08:32 -05:00
|
|
|
CodeCollector.inject_options(opt)
|
2014-04-28 01:23:54 -04:00
|
|
|
opt.on :login, "Authenticate the gist gem with GitHub"
|
2018-10-12 15:09:29 -04:00
|
|
|
opt.on :p, :public, "Create a public gist (default: false)", default: false
|
|
|
|
opt.on :clip, "Copy the selected content to clipboard instead, do NOT gist it", default: false
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
def process
|
2014-04-28 01:23:54 -04:00
|
|
|
return ::Gist.login! if opts.present?(:login)
|
2018-10-14 09:44:58 -04:00
|
|
|
|
2013-01-15 14:08:32 -05:00
|
|
|
cc = CodeCollector.new(args, opts, _pry_)
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2013-01-15 14:08:32 -05:00
|
|
|
if cc.content =~ /\A\s*\z/
|
2012-08-11 20:22:29 -04:00
|
|
|
raise CommandError, "Found no code to gist."
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts.present?(:clip)
|
2013-01-15 14:08:32 -05:00
|
|
|
clipboard_content(cc.content)
|
2012-08-11 20:22:29 -04:00
|
|
|
else
|
2013-01-15 14:08:32 -05:00
|
|
|
# we're overriding the default behavior of the 'in' option (as
|
|
|
|
# defined on CodeCollector) with our local behaviour.
|
|
|
|
content = opts.present?(:in) ? input_content : cc.content
|
|
|
|
gist_content content, cc.file
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
end
|
2012-06-23 15:56:54 -04:00
|
|
|
|
2013-01-15 14:08:32 -05:00
|
|
|
def clipboard_content(content)
|
2014-04-28 01:23:54 -04:00
|
|
|
::Gist.copy(content)
|
2012-08-11 20:22:29 -04:00
|
|
|
output.puts "Copied content to clipboard!"
|
|
|
|
end
|
|
|
|
|
2013-01-15 14:08:32 -05:00
|
|
|
def input_content
|
|
|
|
content = ""
|
|
|
|
CodeCollector.input_expression_ranges.each do |range|
|
2018-10-19 10:27:37 -04:00
|
|
|
input_expressions = _pry_.input_ring[range] || []
|
2013-01-15 14:08:32 -05:00
|
|
|
Array(input_expressions).each_with_index do |code, index|
|
|
|
|
corrected_index = index + range.first
|
|
|
|
if code && code != ""
|
|
|
|
content << code
|
|
|
|
if code !~ /;\Z/
|
2018-10-19 10:27:37 -04:00
|
|
|
content << "#{comment_expression_result_for_gist(_pry_.config.gist.inspecter.call(_pry_.output_ring[corrected_index]))}"
|
2013-01-15 14:08:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
|
2013-01-15 14:08:32 -05:00
|
|
|
content
|
2012-08-11 20:22:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def comment_expression_result_for_gist(result)
|
|
|
|
content = ""
|
|
|
|
result.lines.each_with_index do |line, index|
|
|
|
|
if index == 0
|
|
|
|
content << "# => #{line}"
|
|
|
|
else
|
|
|
|
content << "# #{line}"
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|
|
|
|
end
|
2013-01-15 14:08:32 -05:00
|
|
|
|
2012-08-11 20:22:29 -04:00
|
|
|
content
|
|
|
|
end
|
|
|
|
|
2013-01-15 14:08:32 -05:00
|
|
|
def gist_content(content, filename)
|
2018-10-12 15:09:29 -04:00
|
|
|
response = ::Gist.gist(content, filename: filename || "pry_gist.rb", public: !!opts[:p])
|
2013-01-15 14:08:32 -05:00
|
|
|
if response
|
|
|
|
url = response['html_url']
|
2013-01-16 11:31:03 -05:00
|
|
|
message = "Gist created at URL #{url}"
|
|
|
|
begin
|
2014-04-28 01:23:54 -04:00
|
|
|
::Gist.copy(url)
|
2013-01-16 11:31:03 -05:00
|
|
|
message << ", which is now in the clipboard."
|
2014-04-28 01:23:54 -04:00
|
|
|
rescue ::Gist::ClipboardError
|
2013-01-16 11:31:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
output.puts message
|
2013-01-15 14:08:32 -05:00
|
|
|
end
|
|
|
|
end
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|
2012-08-11 20:22:29 -04:00
|
|
|
|
2012-12-25 16:35:17 -05:00
|
|
|
Pry::Commands.add_command(Pry::Command::Gist)
|
|
|
|
Pry::Commands.alias_command 'clipit', 'gist --clip'
|
2012-06-23 15:56:54 -04:00
|
|
|
end
|