mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
changed some commands to use one-indexing (for consistency): show-input, amend-line, play
This commit is contained in:
parent
4a0329a032
commit
77385e66b2
2 changed files with 40 additions and 6 deletions
|
@ -8,8 +8,8 @@ class Pry
|
|||
eval_string.replace("")
|
||||
end
|
||||
|
||||
command "show-input", "Show the current eval_string" do
|
||||
render_output(false, 0, Pry.color ? CodeRay.scan(eval_string, :ruby).term : eval_string)
|
||||
command "show-input", "Show the contents of the input buffer for the current multi-line expression." do
|
||||
render_output(false, 1, Pry.color ? CodeRay.scan(eval_string, :ruby).term : eval_string)
|
||||
end
|
||||
|
||||
command(/amend-line.?(-?\d+)?(?:\.\.(-?\d+))?/, "Amend a line of input in multi-line mode. Type `amend-line --help` for more information. Aliases %",
|
||||
|
@ -37,7 +37,7 @@ e.g amend-line puts 'hello again' # no line number modifies immediately preced
|
|||
input_array = eval_string.each_line.to_a
|
||||
|
||||
end_line_number = start_line_number.to_i if !end_line_number
|
||||
line_range = start_line_number ? (start_line_number.to_i..end_line_number.to_i) : input_array.size - 1
|
||||
line_range = start_line_number ? (one_index_number(start_line_number.to_i)..one_index_number(end_line_number.to_i)) : input_array.size - 1
|
||||
|
||||
# delete selected lines if replacement line is '!'
|
||||
if arg_string == "!"
|
||||
|
@ -78,7 +78,7 @@ e.g amend-line puts 'hello again' # no line number modifies immediately preced
|
|||
code, code_type = code_and_code_type_for(meth)
|
||||
next if !code
|
||||
|
||||
range = opts.l? ? opts[:l] : (0..-1)
|
||||
range = opts.l? ? one_index_range_or_number(opts[:l]) : (0..-1)
|
||||
range = (0..-2) if opts.o?
|
||||
|
||||
Pry.active_instance.input = StringIO.new(Array(code.each_line.to_a[range]).join)
|
||||
|
@ -88,7 +88,7 @@ e.g amend-line puts 'hello again' # no line number modifies immediately preced
|
|||
file_name = File.expand_path(opts[:f])
|
||||
next output.puts "No such file: #{opts[:f]}" if !File.exists?(file_name)
|
||||
text_array = File.readlines(file_name)
|
||||
range = opts.l? ? opts[:l] : (0..-1)
|
||||
range = opts.l? ? one_index_range_or_number(opts[:l]) : (0..-1)
|
||||
range = (0..-2) if opts.o?
|
||||
|
||||
Pry.active_instance.input = StringIO.new(Array(text_array[range]).join)
|
||||
|
@ -102,7 +102,7 @@ e.g amend-line puts 'hello again' # no line number modifies immediately preced
|
|||
opt.banner "Usage: hist [--replay START..END] [--clear] [--grep PATTERN] [--head N] [--tail N] [--help] [--save [START..END] file.txt]\n"
|
||||
|
||||
opt.on :g, :grep, 'A pattern to match against the history.', true do |pattern|
|
||||
pattern = Regexp.new arg_string.split(/ /)[1]
|
||||
pattern = Regexp.new arg_string.strip.split(/ /, 2).last.strip
|
||||
history.pop
|
||||
|
||||
history.map!.with_index do |element, index|
|
||||
|
@ -212,6 +212,34 @@ e.g amend-line puts 'hello again' # no line number modifies immediately preced
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
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
|
||||
|
|
|
@ -12,6 +12,8 @@ class Pry
|
|||
"e.g: show-method hello_method"
|
||||
|
||||
opt.on :l, "line-numbers", "Show line numbers."
|
||||
opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)."
|
||||
|
||||
opt.on :M, "instance-methods", "Operate on instance methods."
|
||||
opt.on :m, :methods, "Operate on methods."
|
||||
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
|
||||
|
@ -44,6 +46,9 @@ class Pry
|
|||
start_line = meth.source_location ? meth.source_location.last : 1
|
||||
end
|
||||
|
||||
start_line = opts.b? ? 1 : start_line
|
||||
|
||||
|
||||
render_output(opts.flood?, start_line, code)
|
||||
code
|
||||
end
|
||||
|
@ -118,6 +123,7 @@ class Pry
|
|||
file_name = File.expand_path(args.first)
|
||||
|
||||
invoke_editor(file_name, opts[:l].to_i)
|
||||
set_file_and_dir_locals(file_name)
|
||||
|
||||
if opts[:r]
|
||||
silence_warnings do
|
||||
|
|
Loading…
Reference in a new issue