Skip to content
This repository
Browse code

Merge remote-tracking branch 'golive/master'

Conflicts:
	lib/pry/default_commands/input.rb
  • Loading branch information...
commit 10b223855e2dc073ab9eaa2c728600262c5452fb 2 parents 44a466c + 645821e
Ryan Fitzgerald rf- authored
17 lib/pry/code.rb
@@ -173,6 +173,23 @@ def between(start_line, end_line=nil)
173 173 end
174 174 end
175 175
  176 + # Take `num_lines` from `start_line`, forward or backwards
  177 + #
  178 + # @param [Fixnum] start_line
  179 + # @param [Fixnum] num_lines
  180 + # @return [Code]
  181 + def take_lines(start_line, num_lines)
  182 + if start_line >= 0
  183 + start_idx = @lines.index { |l| l.last >= start_line } || @lines.length
  184 + else
  185 + start_idx = @lines.length + start_line
  186 + end
  187 +
  188 + alter do
  189 + @lines = @lines.slice(start_idx, num_lines)
  190 + end
  191 + end
  192 +
176 193 # Remove all lines except for the `lines` up to and excluding `line_num`.
177 194 #
178 195 # @param [Fixnum] line_num
16 lib/pry/default_commands/hist.rb
@@ -32,21 +32,25 @@ def options(opt)
32 32 def process
33 33 @history = Pry::Code(Pry.history.to_a)
34 34
  35 + if opts.present?(:show)
  36 + @history = @history.between(opts[:show])
  37 + end
  38 +
  39 + if opts.present?(:grep)
  40 + @history = @history.grep(opts[:grep])
  41 + end
  42 +
35 43 @history = case
36 44 when opts.present?(:head)
37   - @history.between(1, opts[:head] || 10)
  45 + @history.take_lines(1, opts[:head] || 10)
38 46 when opts.present?(:tail)
39   - @history.between(-(opts[:tail] || 10), -1)
  47 + @history.take_lines(-(opts[:tail] || 10), opts[:tail] || 10)
40 48 when opts.present?(:show)
41 49 @history.between(opts[:show])
42 50 else
43 51 @history
44 52 end
45 53
46   - if opts.present?(:grep)
47   - @history = @history.grep(opts[:grep])
48   - end
49   -
50 54 if opts.present?(:'exclude-pry')
51 55 @history = @history.select { |l, ln| !command_set.valid_command?(l) }
52 56 end
32 test/test_default_commands/test_input.rb
@@ -347,6 +347,38 @@ def o.test_method
347 347 str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/
348 348 end
349 349
  350 + it 'should apply --tail after --grep' do
  351 + @hist.push "print 1"
  352 + @hist.push "print 2"
  353 + @hist.push "puts 3"
  354 + @hist.push "print 4"
  355 + @hist.push "puts 5"
  356 +
  357 + str_output = StringIO.new
  358 + redirect_pry_io(InputTester.new("hist --tail 2 --grep print", "exit-all"), str_output) do
  359 + pry
  360 + end
  361 +
  362 + str_output.string.each_line.count.should == 2
  363 + str_output.string.should =~ /\d:.*?print 2\n\d:.*?print 4/
  364 + end
  365 +
  366 + it 'should apply --head after --grep' do
  367 + @hist.push "puts 1"
  368 + @hist.push "print 2"
  369 + @hist.push "puts 3"
  370 + @hist.push "print 4"
  371 + @hist.push "print 5"
  372 +
  373 + str_output = StringIO.new
  374 + redirect_pry_io(InputTester.new("hist --head 2 --grep print", "exit-all"), str_output) do
  375 + pry
  376 + end
  377 +
  378 + str_output.string.each_line.count.should == 2
  379 + str_output.string.should =~ /\d:.*?print 2\n\d:.*?print 4/
  380 + end
  381 +
350 382 # strangeness in this test is due to bug in Readline::HISTORY not
351 383 # always registering first line of input
352 384 it 'should return first N lines in history with --head switch' do

0 comments on commit 10b2238

Please sign in to comment.
Something went wrong with that request. Please try again.