mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Append to the .pry_history file instead of overwriting it.
This is more consistent with what other shells do, and allows you to keep history from multiple consecutively-open Pry windows.
This commit is contained in:
parent
ec3b22ede5
commit
d326f2e57a
2 changed files with 95 additions and 3 deletions
|
@ -140,13 +140,27 @@ class Pry
|
|||
# Load Readline history if required.
|
||||
def self.load_history
|
||||
Readline::HISTORY.push(*File.readlines(history_file).map(&:chomp)) if File.exists?(history_file)
|
||||
@loaded_history = Readline::HISTORY.to_a
|
||||
end
|
||||
|
||||
# Save Readline history if required.
|
||||
# Save new lines of Readline history if required.
|
||||
def self.save_history
|
||||
File.open(history_file, 'w') do |f|
|
||||
f.write Readline::HISTORY.to_a.join("\n")
|
||||
history_to_save = Readline::HISTORY.to_a
|
||||
|
||||
# Omit any history we read from the file.This check is needed because
|
||||
# `hist --clear` would otherwise cause us to not save history in this
|
||||
# session.
|
||||
if history_to_save[0...@loaded_history.size] == @loaded_history
|
||||
history_to_save = history_to_save[@loaded_history.size..-1]
|
||||
end
|
||||
|
||||
File.open(history_file, 'a') do |f|
|
||||
f.puts history_to_save.join("\n") if history_to_save.size > 0
|
||||
end
|
||||
|
||||
# Update @loaded_history so that future calls to save_history
|
||||
# will do the right thing.
|
||||
@loaded_history = Readline::HISTORY.to_a
|
||||
end
|
||||
|
||||
# Get the full path of the history_path for pry.
|
||||
|
|
78
test/test_pry_history.rb
Normal file
78
test/test_pry_history.rb
Normal file
|
@ -0,0 +1,78 @@
|
|||
require 'helper'
|
||||
require 'tempfile'
|
||||
|
||||
describe Pry do
|
||||
|
||||
before do
|
||||
Readline::HISTORY.shift while Readline::HISTORY.length > 0
|
||||
@hist = Tempfile.new(["tmp", ".pry_history"]).tap(&:close).path
|
||||
File.open(@hist, 'w') {|f| f << "1\n2\n3\n" }
|
||||
@old_hist = Pry.config.history.file
|
||||
Pry.config.history.file = @hist
|
||||
Pry.load_history
|
||||
end
|
||||
|
||||
after do
|
||||
File.unlink @hist
|
||||
Pry.config.history.file = @old_hist
|
||||
end
|
||||
|
||||
describe ".load_history" do
|
||||
it "should read the contents of the file" do
|
||||
Readline::HISTORY.to_a.should === ["1", "2", "3"]
|
||||
end
|
||||
end
|
||||
|
||||
describe ".save_history" do
|
||||
it "should include a trailing newline" do
|
||||
Readline::HISTORY << "4"
|
||||
Pry.save_history
|
||||
File.read(@hist).should =~ /4\n\z/
|
||||
end
|
||||
|
||||
it "should not change anything if history is not changed" do
|
||||
File.open(@hist, 'w') {|f| f << "4\n5\n6\n" }
|
||||
Pry.save_history
|
||||
File.read(@hist).should == "4\n5\n6\n"
|
||||
end
|
||||
|
||||
it "should append new lines to the file" do
|
||||
Readline::HISTORY << "4"
|
||||
Pry.save_history
|
||||
File.read(@hist).should == "1\n2\n3\n4\n"
|
||||
end
|
||||
|
||||
it "should not clobber lines written by other Pry's in the meantime" do
|
||||
Readline::HISTORY << "5"
|
||||
File.open(@hist, 'a') {|f| f << "4\n" }
|
||||
Pry.save_history
|
||||
|
||||
Readline::HISTORY.to_a.should == ["1", "2", "3", "5"]
|
||||
File.read(@hist).should == "1\n2\n3\n4\n5\n"
|
||||
end
|
||||
|
||||
it "should not delete lines from the file if this session's history was cleared" do
|
||||
Readline::HISTORY.pop while Readline::HISTORY.size > 0
|
||||
Pry.save_history
|
||||
File.read(@hist).should == "1\n2\n3\n"
|
||||
end
|
||||
|
||||
it "should save new lines that are added after the history was cleared" do
|
||||
Readline::HISTORY.pop while Readline::HISTORY.size > 0
|
||||
Readline::HISTORY << "4"
|
||||
Pry.save_history
|
||||
File.read(@hist).should == "1\n2\n3\n4\n"
|
||||
end
|
||||
|
||||
it "should only append new lines the second time it is saved" do
|
||||
Readline::HISTORY << "4"
|
||||
Pry.save_history
|
||||
File.open(@hist, 'a') {|f| f << "5\n" }
|
||||
Readline::HISTORY << "6"
|
||||
Pry.save_history
|
||||
|
||||
Readline::HISTORY.to_a.should == ["1", "2", "3", "4", "6"]
|
||||
File.read(@hist).should == "1\n2\n3\n4\n5\n6\n"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue