Skip to content

Commit

Permalink
Merge pull request #17 from ruby/reline-history
Browse files Browse the repository at this point in the history
Add support for history with Reline backend
  • Loading branch information
aycabta committed May 26, 2019
2 parents e8e79d5 + 31146f0 commit 6a2c389
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/irb/ext/save-history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,29 @@ def history_file=(hist)
end

module HistorySavingAbility # :nodoc:
include Readline

def HistorySavingAbility.extended(obj)
IRB.conf[:AT_EXIT].push proc{obj.save_history}
obj.load_history
obj
end

def load_history
return unless self.class.const_defined?(:HISTORY)
history = self.class::HISTORY
if history_file = IRB.conf[:HISTORY_FILE]
history_file = File.expand_path(history_file)
end
history_file = IRB.rc_file("_history") unless history_file
if File.exist?(history_file)
open(history_file) do |f|
f.each {|l| HISTORY << l.chomp}
f.each {|l| history << l.chomp}
end
end
end

def save_history
return unless self.class.const_defined?(:HISTORY)
history = self.class::HISTORY
if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
if history_file = IRB.conf[:HISTORY_FILE]
history_file = File.expand_path(history_file)
Expand All @@ -96,7 +98,7 @@ def save_history
end

open(history_file, 'w', 0600 ) do |f|
hist = HISTORY.to_a
hist = history.to_a
f.puts(hist[-num..-1] || hist)
end
end
Expand Down

0 comments on commit 6a2c389

Please sign in to comment.