Command::Hist: change the behaviour

Fix issue #205 (history option to show just history for current session)

Add `--all` switch. `hist` is `hist --all` now. `hist` without any
parameters shows the history of the current session.
This commit is contained in:
Kyrylo Silin 2013-03-17 10:41:21 +02:00
parent 6463387d07
commit ce99ec425a
3 changed files with 51 additions and 12 deletions

View File

@ -6,6 +6,7 @@ class Pry
banner <<-'BANNER'
Usage: hist [--head|--tail]
hist --all
hist --head N
hist --tail N
hist --show START..END
@ -19,6 +20,7 @@ class Pry
BANNER
def options(opt)
opt.on :a, :all, "Display all history"
opt.on :H, :head, "Display the first N items", :optional_argument => true, :as => Integer
opt.on :T, :tail, "Display the last N items", :optional_argument => true, :as => Integer
opt.on :s, :show, "Show the given range of lines", :optional_argument => true, :as => Range
@ -32,8 +34,7 @@ class Pry
end
def process
# The last value in history will be the 'hist' command itself
@history = Pry::Code(Pry.history.to_a[0..-2])
@history = find_history
if opts.present?(:show)
@history = @history.between(opts[:show])
@ -156,6 +157,21 @@ class Pry
false
end
end
# Finds history depending on the given switch.
#
# @return [Pry::Code] if it finds `--all` (or `-a`) switch, returns all
# entries in history. Without the switch returns only the entries from the
# current Pry session.
def find_history
h = if opts.present?(:all)
Pry.history.to_a
else
Pry.history.to_a.last(Pry.history.session_line_count)
end
# The last value in history will be the 'hist' command itself.
Pry::Code(h[0..-2])
end
end
Pry::Commands.add_command(Pry::Command::Hist)

View File

@ -7,22 +7,14 @@ describe "hist" do
@str_output = StringIO.new
@t = pry_tester :history => @hist do
# For looking at what hist pushes into the input stack. The
# implementation of this helper will definitely have to change at some
# point.
# For looking at what hist pushes into the input stack. The implementation
# of this helper will definitely have to change at some point.
def next_input
@pry.input.string
end
end
end
it 'should display the correct history' do
@hist.push "hello"
@hist.push "world"
@t.eval('hist').should =~ /hello\n.*world/
end
it 'should replay history correctly (single item)' do
o = Object.new
@hist.push "@x = 10"
@ -175,4 +167,32 @@ describe "hist" do
@hist.push('ls')
pry_eval('hist -e').should == "1: a = 20\n"
end
describe "sessions" do
before do
@old_file = Pry.config.history.file
Pry.config.history.file = File.expand_path('spec/fixtures/pry_history')
@hist.load
end
after do
Pry.config.history.file = @old_file
end
it "displays history only for current session" do
@hist.push('hello')
@hist.push('world')
@t.eval('hist').should =~ /1:\shello\n2:\sworld/
end
it "displays all history (including the current sesion) with `--all` switch" do
@hist.push('goodbye')
@hist.push('world')
output = @t.eval('hist --all')
output.should =~ /1:\s:athos\n2:\s:porthos\n3:\s:aramis\n/
output.should =~ /4:\sgoodbye\n5:\sworld/
end
end
end

3
spec/fixtures/pry_history vendored Normal file
View File

@ -0,0 +1,3 @@
:athos
:porthos
:aramis