module Nydp::ReadlineHistory

Constants

HISTFILE
MAXHISTSIZE

Public Instance Methods

setup_readline_history(verbose=true) click to toggle source
# File lib/nydp/readline_history.rb, line 8
def setup_readline_history verbose=true
  histfile = File::expand_path(HISTFILE)
  begin
    if File::exists?(histfile)
      lines = IO::readlines(histfile).collect { |line| line.chomp }
      Readline::HISTORY.push(*lines)
    end
    Kernel::at_exit do
      lines = Readline::HISTORY.to_a.reverse.uniq.reverse
      lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.size > MAXHISTSIZE
      File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
    end
  rescue => e
    puts "Error when configuring permanent history: #{e}" if verbose
  end
end