mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
added gist -f, and the gist --lines modifier
This commit is contained in:
parent
7b580be500
commit
6823279860
3 changed files with 60 additions and 37 deletions
|
@ -73,8 +73,11 @@ class Pry
|
||||||
|
|
||||||
opt.on :d, :doc, "Gist a method's documentation.", true
|
opt.on :d, :doc, "Gist a method's documentation.", true
|
||||||
opt.on :m, :method, "Gist a method's source.", 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 :p, :public, "Create a public gist (default: false)", :default => false
|
||||||
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|
|
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)
|
input_ranges << absolute_index_range(range, _pry_.input_array.length)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -83,16 +86,24 @@ class Pry
|
||||||
if opts.present?(:in)
|
if opts.present?(:in)
|
||||||
code_type = :ruby
|
code_type = :ruby
|
||||||
content = ""
|
content = ""
|
||||||
normalized_range = absolute_index_range(opts[:i], _pry_.input_array.length)
|
|
||||||
input_items = input_ranges.map { |v| _pry_.input_array[v] || [] }.flatten
|
|
||||||
|
|
||||||
input_items.each_with_index.map do |code, index|
|
input_ranges.each do |range|
|
||||||
corrected_index = index + normalized_range.first
|
input_expressions = _pry_.input_array[range] || []
|
||||||
if code && code != ""
|
input_expressions.each_with_index.map do |code, index|
|
||||||
content << code
|
corrected_index = index + range.first
|
||||||
content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}" if code !~ /;\Z/
|
if code && code != ""
|
||||||
|
content << code
|
||||||
|
content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}" if code !~ /;\Z/
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
elsif opts.present?(:file)
|
||||||
|
whole_file = File.read(File.expand_path(opts[:f]))
|
||||||
|
if opts.present?(:lines)
|
||||||
|
content = restrict_to_lines(whole_file, opts[:l])
|
||||||
|
else
|
||||||
|
content = whole_file
|
||||||
|
end
|
||||||
elsif opts.present?(:doc)
|
elsif opts.present?(:doc)
|
||||||
meth = get_method_or_raise(opts[:d], target, {})
|
meth = get_method_or_raise(opts[:d], target, {})
|
||||||
content = meth.doc
|
content = meth.doc
|
||||||
|
@ -104,13 +115,21 @@ class Pry
|
||||||
code_type = :plain
|
code_type = :plain
|
||||||
elsif opts.present?(:method)
|
elsif opts.present?(:method)
|
||||||
meth = get_method_or_raise(opts[:m], target, {})
|
meth = get_method_or_raise(opts[:m], target, {})
|
||||||
content = meth.source
|
method_source = meth.source
|
||||||
|
if opts.present?(:lines)
|
||||||
|
content = restrict_to_lines(method_source, opts[:l])
|
||||||
|
else
|
||||||
|
content = method_source
|
||||||
|
end
|
||||||
|
|
||||||
code_type = meth.source_type
|
code_type = meth.source_type
|
||||||
end
|
end
|
||||||
|
|
||||||
# prevent Gist from exiting the session on error
|
# prevent Gist from exiting the session on error
|
||||||
begin
|
begin
|
||||||
link = Gist.write([:extension => ".#{type_map[code_type]}",
|
extname = opts.present?(:file) ? ".#{gist_file_extension(opts[:f])}" : ".#{type_map[code_type]}"
|
||||||
|
|
||||||
|
link = Gist.write([:extension => extname,
|
||||||
:input => content],
|
:input => content],
|
||||||
!opts[:p])
|
!opts[:p])
|
||||||
rescue SystemExit
|
rescue SystemExit
|
||||||
|
@ -123,6 +142,15 @@ class Pry
|
||||||
end
|
end
|
||||||
|
|
||||||
helpers do
|
helpers do
|
||||||
|
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)
|
def comment_expression_result_for_gist(result)
|
||||||
content = ""
|
content = ""
|
||||||
result.lines.each_with_index do |line, index|
|
result.lines.each_with_index do |line, index|
|
||||||
|
@ -136,7 +164,6 @@ class Pry
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -283,32 +283,6 @@ class Pry
|
||||||
|
|
||||||
alias_command "history", "hist"
|
alias_command "history", "hist"
|
||||||
|
|
||||||
helpers do
|
|
||||||
def one_index_number(line_number)
|
|
||||||
if line_number > 0
|
|
||||||
line_number - 1
|
|
||||||
elsif line_number < 0
|
|
||||||
line_number
|
|
||||||
else
|
|
||||||
line_number
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def one_index_range(range)
|
|
||||||
Range.new(one_index_number(range.begin), one_index_number(range.end))
|
|
||||||
end
|
|
||||||
|
|
||||||
def one_index_range_or_number(range_or_number)
|
|
||||||
case range_or_number
|
|
||||||
when Range
|
|
||||||
one_index_range(range_or_number)
|
|
||||||
else
|
|
||||||
one_index_number(range_or_number)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -253,6 +253,28 @@ class Pry
|
||||||
text.gsub(/^#{margin}/, '')
|
text.gsub(/^#{margin}/, '')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def one_index_number(line_number)
|
||||||
|
if line_number > 0
|
||||||
|
line_number - 1
|
||||||
|
else
|
||||||
|
line_number
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# convert a 1-index range to a 0-indexed one
|
||||||
|
def one_index_range(range)
|
||||||
|
Range.new(one_index_number(range.begin), one_index_number(range.end))
|
||||||
|
end
|
||||||
|
|
||||||
|
def one_index_range_or_number(range_or_number)
|
||||||
|
case range_or_number
|
||||||
|
when Range
|
||||||
|
one_index_range(range_or_number)
|
||||||
|
else
|
||||||
|
one_index_number(range_or_number)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def absolute_index_number(line_number, array_length)
|
def absolute_index_number(line_number, array_length)
|
||||||
if line_number >= 0
|
if line_number >= 0
|
||||||
line_number
|
line_number
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue