Permalink
Browse files

Merge remote-tracking branch 'golive/master'

Conflicts:
	lib/pry/default_commands/input.rb
  • Loading branch information...
2 parents 44a466c + 645821e commit 10b223855e2dc073ab9eaa2c728600262c5452fb @rf- rf- committed Mar 18, 2012
Showing with 59 additions and 6 deletions.
  1. +17 −0 lib/pry/code.rb
  2. +10 −6 lib/pry/default_commands/hist.rb
  3. +32 −0 test/test_default_commands/test_input.rb
View
@@ -173,6 +173,23 @@ def between(start_line, end_line=nil)
end
end
+ # Take `num_lines` from `start_line`, forward or backwards
+ #
+ # @param [Fixnum] start_line
+ # @param [Fixnum] num_lines
+ # @return [Code]
+ def take_lines(start_line, num_lines)
+ if start_line >= 0
+ start_idx = @lines.index { |l| l.last >= start_line } || @lines.length
+ else
+ start_idx = @lines.length + start_line
+ end
+
+ alter do
+ @lines = @lines.slice(start_idx, num_lines)
+ end
+ end
+
# Remove all lines except for the `lines` up to and excluding `line_num`.
#
# @param [Fixnum] line_num
@@ -32,21 +32,25 @@ def options(opt)
def process
@history = Pry::Code(Pry.history.to_a)
+ if opts.present?(:show)
+ @history = @history.between(opts[:show])
+ end
+
+ if opts.present?(:grep)
+ @history = @history.grep(opts[:grep])
+ end
+
@history = case
when opts.present?(:head)
- @history.between(1, opts[:head] || 10)
+ @history.take_lines(1, opts[:head] || 10)
when opts.present?(:tail)
- @history.between(-(opts[:tail] || 10), -1)
+ @history.take_lines(-(opts[:tail] || 10), opts[:tail] || 10)
when opts.present?(:show)
@history.between(opts[:show])
else
@history
end
- if opts.present?(:grep)
- @history = @history.grep(opts[:grep])
- end
-
if opts.present?(:'exclude-pry')
@history = @history.select { |l, ln| !command_set.valid_command?(l) }
end
@@ -347,6 +347,38 @@ def o.test_method
str_output.string.should =~ /x\n\d+:.*y\n\d+:.*z/
end
+ it 'should apply --tail after --grep' do
+ @hist.push "print 1"
+ @hist.push "print 2"
+ @hist.push "puts 3"
+ @hist.push "print 4"
+ @hist.push "puts 5"
+
+ str_output = StringIO.new
+ redirect_pry_io(InputTester.new("hist --tail 2 --grep print", "exit-all"), str_output) do
+ pry
+ end
+
+ str_output.string.each_line.count.should == 2
+ str_output.string.should =~ /\d:.*?print 2\n\d:.*?print 4/
+ end
+
+ it 'should apply --head after --grep' do
+ @hist.push "puts 1"
+ @hist.push "print 2"
+ @hist.push "puts 3"
+ @hist.push "print 4"
+ @hist.push "print 5"
+
+ str_output = StringIO.new
+ redirect_pry_io(InputTester.new("hist --head 2 --grep print", "exit-all"), str_output) do
+ pry
+ end
+
+ str_output.string.each_line.count.should == 2
+ str_output.string.should =~ /\d:.*?print 2\n\d:.*?print 4/
+ end
+
# strangeness in this test is due to bug in Readline::HISTORY not
# always registering first line of input
it 'should return first N lines in history with --head switch' do

0 comments on commit 10b2238

Please sign in to comment.