浏览代码

Refactored the persistent history to be reusable in other places.

Lucas Stadler 14 年之前
父节点
当前提交
423a82ca78
共有 1 个文件被更改,包括 20 次插入14 次删除
  1. 20 14
      .irbrc

+ 20 - 14
.irbrc

@ -4,19 +4,25 @@ require 'irb/completion'
4 4
require 'wirb'
5 5
Wirb.start
6 6
7
# Make irb's history persistent
8
irbhistory = File.join Dir.home, ".irbhistory"
9
10
last_history = File.read irbhistory rescue ""
11
last_history.lines.each do |line|
12
  Readline::HISTORY.push line.strip
13
end
14
15
def save_history
16
  puts "Writing history back..."
17
  File.write irbhistory, Readline::HISTORY.to_a.uniq.reduce { |x,y| "#{x}\n#{y}" }
7
# A history of commands separated by lines.
8
#  @param hist_file the path to the history file (like ~/.bash_history)
9
#  @param hist_src the 'backend' that records the history.
10
#                  Must respond to #to_a and #push.
11
class StoredHistory
12
  def initialize hist_file, hist_src=Readline::HISTORY
13
    @history = hist_file
14
    @src     = hist_src
15
    
16
    last_history = File.read @history rescue ""
17
    last_history.lines.each do |line|
18
      @src.push line.strip
19
    end
20
    
21
    at_exit do
22
      puts "Writing history back to #{@history}"
23
      File.write @history, @src.to_a.uniq.reduce { |x,y| "#{x}\n#{y}" }
24
    end
25
  end
18 26
end
19 27
20
at_exit do
21
  save_history
22
end
28
StoredHistory.new File.join(Dir.home, ".irbhistory")