class Brice::History

IRb history support.

Configure with config.history.opt = { ... }, where the following keys are recognized (see DEFAULTS):

:path

The path to your .irb_history file.

:size

The number of entries to keep in the history file.

:perms

The mode to open the history file with.

:uniq

Whether only unique history entries shall be saved. May also be :reverse to keep the most recent ones.

:merge

Whether to preserve, i.e. merge, the history across overlapping sessions.

Constants

DEFAULTS

Public Class Methods

init(opt = {}) click to toggle source
   # File lib/brice/history.rb
53 def self.init(opt = {})
54   new(opt)
55 end
new(opt = {}, history = defined?(Readline::HISTORY) && Readline::HISTORY) click to toggle source
   # File lib/brice/history.rb
57 def initialize(opt = {}, history = defined?(Readline::HISTORY) && Readline::HISTORY)
58   DEFAULTS.each { |key, val|
59     instance_variable_set("@#{key}", Brice.opt(opt, key, val))
60   }
61 
62   @path    = File.expand_path(@path)
63   @reverse = @uniq.to_s == 'reverse'
64 
65   init_history(history) if history
66 end

Private Instance Methods

extend_history() click to toggle source
    # File lib/brice/history.rb
103 def extend_history
104   @history.extend(Tee) if @merge && class << @history; !include?(Tee); end
105 end
init_history(history) click to toggle source
   # File lib/brice/history.rb
70 def init_history(history)
71   @history = history
72 
73   @libedit = begin
74     Readline.emacs_editing_mode
75     true
76   rescue NotImplementedError, NoMethodError
77     false
78   end
79 
80   load_history
81   extend_history
82 
83   Kernel.at_exit { save_history }
84 end
load_history(history = @history) click to toggle source
    # File lib/brice/history.rb
 93 def load_history(history = @history)
 94   @first_line = nil
 95 
 96   return unless File.readable?(@path)
 97   read_history { |line| history << line }
 98 
 99   return unless @libedit
100   @first_line = read_history { |line| break line }
101 end
read_history() { |line| ... } click to toggle source
   # File lib/brice/history.rb
86 def read_history
87   File.foreach(@path) { |line|
88     line.chomp!
89     yield line
90   }
91 end
save_history() click to toggle source
    # File lib/brice/history.rb
107 def save_history
108   if @merge
109     load_history(lines = [])
110     @history.tee! { |t| lines.concat(t) }
111   else
112     lines = @history.to_a
113   end
114 
115   lines.unshift(@first_line) if @first_line
116 
117   lines.reverse! if @reverse
118   lines.uniq!    if @uniq
119   lines.reverse! if @reverse
120 
121   lines.slice!(0, lines.size - @size)
122 
123   File.open(@path, @perms) { |f| f.puts(lines) }
124 end