mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Refactor gist to new command-class API
This commit is contained in:
parent
d797a209d9
commit
689e47899b
1 changed files with 127 additions and 137 deletions
|
@ -57,151 +57,141 @@ class Pry
|
|||
|
||||
require 'pry/command_context'
|
||||
|
||||
command("gist", "Gist a method or expression history to github. Type `gist --help` for more info.",{:requires_gem => "gist", :shellwords => false, :definition => Pry::CommandContext.new{
|
||||
command_class "gist", "Gist a method or expression history to github. Type `gist --help` for more info.", :requires_gem => "gist", :shellwords => false do
|
||||
attr_accessor :content
|
||||
attr_accessor :code_type
|
||||
attr_accessor :input_ranges
|
||||
|
||||
class << self
|
||||
attr_accessor :opts
|
||||
attr_accessor :content
|
||||
attr_accessor :code_type
|
||||
attr_accessor :input_ranges
|
||||
end
|
||||
def initialize
|
||||
require 'gist'
|
||||
end
|
||||
|
||||
def call(*args)
|
||||
require 'gist'
|
||||
def options(opt)
|
||||
opt.banner unindent <<-USAGE
|
||||
Usage: gist [OPTIONS] [METH]
|
||||
Gist method (doc or source) or input expression to github.
|
||||
Ensure the `gist` gem is properly working before use. http://github.com/defunkt/gist for instructions.
|
||||
e.g: gist -m my_method
|
||||
e.g: gist -d my_method
|
||||
e.g: gist -i 1..10
|
||||
USAGE
|
||||
|
||||
gather_options(args)
|
||||
process_options
|
||||
perform_gist
|
||||
end
|
||||
opt.on :d, :doc, "Gist a method's documentation.", true
|
||||
opt.on :m, :method, "Gist a method's source.", true
|
||||
opt.on :f, :file, "Gist a file.", true
|
||||
opt.on :p, :public, "Create a public gist (default: false)", :default => false
|
||||
opt.on :l, :lines, "Only gist a subset of lines (only works with -m and -f)", :optional => true, :as => Range, :default => 1..-1
|
||||
opt.on :i, :in, "Gist entries from Pry's input expression history. Takes an index or range.", :optional => true,
|
||||
:as => Range, :default => -5..-1 do |range|
|
||||
input_ranges << absolute_index_range(range, _pry_.input_array.length)
|
||||
end
|
||||
end
|
||||
|
||||
def gather_options(args)
|
||||
self.input_ranges = []
|
||||
def run
|
||||
if opts.present?(:in)
|
||||
in_option
|
||||
elsif opts.present?(:file)
|
||||
file_option
|
||||
elsif opts.present?(:doc)
|
||||
doc_option
|
||||
elsif opts.present?(:method)
|
||||
method_option
|
||||
end
|
||||
|
||||
self.opts = parse_options!(args) do |opt|
|
||||
opt.banner unindent <<-USAGE
|
||||
Usage: gist [OPTIONS] [METH]
|
||||
Gist method (doc or source) or input expression to github.
|
||||
Ensure the `gist` gem is properly working before use. http://github.com/defunkt/gist for instructions.
|
||||
e.g: gist -m my_method
|
||||
e.g: gist -d my_method
|
||||
e.g: gist -i 1..10
|
||||
USAGE
|
||||
perform_gist
|
||||
end
|
||||
|
||||
opt.on :d, :doc, "Gist a method's documentation.", true
|
||||
opt.on :m, :method, "Gist a method's source.", true
|
||||
opt.on :f, :file, "Gist a file.", true
|
||||
opt.on :p, :public, "Create a public gist (default: false)", :default => false
|
||||
opt.on :l, :lines, "Only gist a subset of lines (only works with -m and -f)", :optional => true, :as => Range, :default => 1..-1
|
||||
opt.on :i, :in, "Gist entries from Pry's input expression history. Takes an index or range.", :optional => true,
|
||||
:as => Range, :default => -5..-1 do |range|
|
||||
input_ranges << absolute_index_range(range, _pry_.input_array.length)
|
||||
end
|
||||
end
|
||||
end
|
||||
def in_option
|
||||
self.code_type = :ruby
|
||||
self.content = ""
|
||||
|
||||
def process_options
|
||||
if opts.present?(:in)
|
||||
in_option
|
||||
elsif opts.present?(:file)
|
||||
file_option
|
||||
elsif opts.present?(:doc)
|
||||
doc_option
|
||||
elsif opts.present?(:method)
|
||||
method_option
|
||||
end
|
||||
end
|
||||
|
||||
def in_option
|
||||
self.code_type = :ruby
|
||||
self.content = ""
|
||||
|
||||
input_ranges.each do |range|
|
||||
input_expressions = _pry_.input_array[range] || []
|
||||
input_expressions.each_with_index.map do |code, index|
|
||||
corrected_index = index + range.first
|
||||
if code && code != ""
|
||||
self.content << code
|
||||
if code !~ /;\Z/
|
||||
self.content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def file_option
|
||||
whole_file = File.read(File.expand_path(opts[:f]))
|
||||
if opts.present?(:lines)
|
||||
self.content = restrict_to_lines(whole_file, opts[:l])
|
||||
else
|
||||
self.content = whole_file
|
||||
end
|
||||
end
|
||||
|
||||
def doc_option
|
||||
meth = get_method_or_raise(opts[:d], target, {})
|
||||
self.content = meth.doc
|
||||
self.code_type = meth.source_type
|
||||
|
||||
text.no_color do
|
||||
self.content = process_comment_markup(self.content, self.code_type)
|
||||
end
|
||||
self.code_type = :plain
|
||||
end
|
||||
|
||||
def method_option
|
||||
meth = get_method_or_raise(opts[:m], target, {})
|
||||
method_source = meth.source
|
||||
if opts.present?(:lines)
|
||||
self.content = restrict_to_lines(method_source, opts[:l])
|
||||
else
|
||||
self.content = method_source
|
||||
end
|
||||
|
||||
self.code_type = meth.source_type
|
||||
end
|
||||
|
||||
def perform_gist
|
||||
type_map = { :ruby => "rb", :c => "c", :plain => "plain" }
|
||||
|
||||
# prevent Gist from exiting the session on error
|
||||
begin
|
||||
extname = opts.present?(:file) ? ".#{gist_file_extension(opts[:f])}" : ".#{type_map[self.code_type]}"
|
||||
|
||||
link = Gist.write([:extension => extname,
|
||||
:input => self.content],
|
||||
!opts[:p])
|
||||
rescue SystemExit
|
||||
end
|
||||
|
||||
if link
|
||||
Gist.copy(link)
|
||||
output.puts "Gist created at #{link} and added to clipboard."
|
||||
end
|
||||
end
|
||||
|
||||
def restrict_to_lines(content, lines)
|
||||
line_range = one_index_range(lines)
|
||||
content.lines.to_a[line_range].join
|
||||
end
|
||||
|
||||
def gist_file_extension(file_name)
|
||||
file_name.split(".").last
|
||||
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}"
|
||||
end
|
||||
end
|
||||
content
|
||||
end
|
||||
}})
|
||||
input_ranges.each do |range|
|
||||
input_expressions = _pry_.input_array[range] || []
|
||||
input_expressions.each_with_index.map do |code, index|
|
||||
corrected_index = index + range.first
|
||||
if code && code != ""
|
||||
self.content << code
|
||||
if code !~ /;\Z/
|
||||
self.content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def file_option
|
||||
whole_file = File.read(File.expand_path(opts[:f]))
|
||||
if opts.present?(:lines)
|
||||
self.content = restrict_to_lines(whole_file, opts[:l])
|
||||
else
|
||||
self.content = whole_file
|
||||
end
|
||||
end
|
||||
|
||||
def doc_option
|
||||
meth = get_method_or_raise(opts[:d], target, {})
|
||||
self.content = meth.doc
|
||||
self.code_type = meth.source_type
|
||||
|
||||
text.no_color do
|
||||
self.content = process_comment_markup(self.content, self.code_type)
|
||||
end
|
||||
self.code_type = :plain
|
||||
end
|
||||
|
||||
def method_option
|
||||
meth = get_method_or_raise(opts[:m], target, {})
|
||||
method_source = meth.source
|
||||
if opts.present?(:lines)
|
||||
self.content = restrict_to_lines(method_source, opts[:l])
|
||||
else
|
||||
self.content = method_source
|
||||
end
|
||||
|
||||
self.code_type = meth.source_type
|
||||
end
|
||||
|
||||
def perform_gist
|
||||
type_map = { :ruby => "rb", :c => "c", :plain => "plain" }
|
||||
|
||||
# prevent Gist from exiting the session on error
|
||||
begin
|
||||
extname = opts.present?(:file) ? ".#{gist_file_extension(opts[:f])}" : ".#{type_map[self.code_type]}"
|
||||
|
||||
link = Gist.write([:extension => extname,
|
||||
:input => self.content],
|
||||
!opts[:p])
|
||||
rescue SystemExit
|
||||
end
|
||||
|
||||
if link
|
||||
Gist.copy(link)
|
||||
output.puts "Gist created at #{link} and added to clipboard."
|
||||
end
|
||||
end
|
||||
|
||||
def restrict_to_lines(content, lines)
|
||||
line_range = one_index_range(lines)
|
||||
content.lines.to_a[line_range].join
|
||||
end
|
||||
|
||||
def gist_file_extension(file_name)
|
||||
file_name.split(".").last
|
||||
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}"
|
||||
end
|
||||
end
|
||||
content
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue