Basic IRB history support. This is based on the tips from wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks
# File lib/wirble.rb, line 115 def initialize(opt = nil) @opt = DEFAULTS.merge(opt || {}) return unless defined? Readline::HISTORY load_history Kernel.at_exit { save_history } end
# File lib/wirble.rb, line 81 def cfg(key) @opt["history_#{key}".intern] end
# File lib/wirble.rb, line 98 def load_history # expand history file and make sure it exists real_path = File.expand_path(cfg('path')) unless File.exist?(real_path) say "History file #{real_path} doesn't exist." return end # read lines from file and add them to history lines = File.readlines(real_path).map { |line| line.chomp } Readline::HISTORY.push(*lines) say 'Read %d lines from history file %s' % [lines.size, cfg('path')] end
# File lib/wirble.rb, line 85 def save_history path, max_size, perms = %w{path size perms}.map { |v| cfg(v) } # read lines from history, and truncate the list (if necessary) lines = Readline::HISTORY.to_a.uniq lines = lines[-max_size, -1] if lines.size > max_size # write the history file real_path = File.expand_path(path) File.open(real_path, perms) { |fh| fh.puts lines } say 'Saved %d lines to history file %s.' % [lines.size, path] end
# File lib/wirble.rb, line 77 def say(*args) puts(*args) if @verbose end