Class | Wirble::History |
In: |
lib/wirble.rb
|
Parent: | Object |
Basic IRB history support. This is based on the tips from wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks
DEFAULTS | = | { :history_path => ENV['IRB_HISTORY_FILE'] || "~/.irb_history", :history_size => (ENV['IRB_HISTORY_SIZE'] || 1000).to_i, :history_perms => File::WRONLY | File::CREAT | File::TRUNC, } |
# File lib/wirble.rb, line 115 115: def initialize(opt = nil) 116: @opt = DEFAULTS.merge(opt || {}) 117: return unless defined? Readline::HISTORY 118: load_history 119: Kernel.at_exit { save_history } 120: end
# File lib/wirble.rb, line 98 98: def load_history 99: # expand history file and make sure it exists 100: real_path = File.expand_path(cfg('path')) 101: unless File.exist?(real_path) 102: say "History file #{real_path} doesn't exist." 103: return 104: end 105: 106: # read lines from file and add them to history 107: lines = File.readlines(real_path).map { |line| line.chomp } 108: Readline::HISTORY.push(*lines) 109: 110: say 'Read %d lines from history file %s' % [lines.size, cfg('path')] 111: end
# File lib/wirble.rb, line 85 85: def save_history 86: path, max_size, perms = %w{path size perms}.map { |v| cfg(v) } 87: 88: # read lines from history, and truncate the list (if necessary) 89: lines = Readline::HISTORY.to_a.uniq 90: lines = lines[-max_size, -1] if lines.size > max_size 91: 92: # write the history file 93: real_path = File.expand_path(path) 94: File.open(real_path, perms) { |fh| fh.puts lines } 95: say 'Saved %d lines to history file %s.' % [lines.size, path] 96: end