开发者

frequently flush irb history to file

irb can write command history to a file, but it only does this when your irb session ends.

I would like to write out my command history more frequently: as often as on every command (like shell history), but it doesn't have to be so often.

开发者_如何学运维

Is there a .irbrc setting for this? Or will I have to hack the irb source...


Having hacked on irb many a time, good luck with a clean solution. Instead I'd recommend ripl, an irb alternative. By default it saves history more intelligently (i.e. even when you abruptly exit out with Control-D).

If you want to write history after every command, it's easy with ripl since it's built to be extended with plugins:

# add to your ~/.riplrc
module Ripl::ImmediateHistory
  # write to history after every eval
  def eval_input(input)
    super
    File.open(history_file, 'a') {|f| f.puts input }
  end

  # disable writing to history when ripl exits
  def write_history; end
end
Ripl::Shell.send :include, Ripl::ImmediateHistory


From here: http://blog.nicksieger.com/articles/2006/04/23/tweaking-irb

module Readline
  module History
    LOG = "#{ENV['HOME']}/.irb-history"

    def self.write_log(line)
      File.open(LOG, 'ab') {|f| f << "#{line}
"}
    end

    def self.start_session_log
      write_log("
# session start: #{Time.now}

")
      at_exit { write_log("
# session stop: #{Time.now}
") }
    end
  end

  alias :old_readline :readline
  def readline(*args)
    ln = old_readline(*args)
    begin
      History.write_log(ln)
    rescue
    end
    ln
  end
end

Readline::History.start_session_log
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜