Module: Nydp::ReadlineHistory

Included in:
ReadlineReader
Defined in:
lib/nydp/readline_history.rb

Constant Summary collapse

HISTFILE =
"~/.nydp_history"
MAXHISTSIZE =
100

Instance Method Summary collapse

Instance Method Details

#setup_readline_history(verbose = true) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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