From 3ef678c7192ffd44d4982427ac9535ac2218bebd Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Tue, 7 Feb 2012 20:20:28 -0800 Subject: [PATCH] add history to irbrc, use the simple prompt --- irbrc | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) mode change 100644 => 100755 irbrc diff --git a/irbrc b/irbrc old mode 100644 new mode 100755 index f752ac7..421b313 --- a/irbrc +++ b/irbrc @@ -1,5 +1,48 @@ +require 'rubygems' +require 'irb/completion' + +# Use awesome print for awesome output begin - require 'ap' + require 'awesome_print' + + unless IRB.version.include?('DietRB') + IRB::Irb.class_eval do + def output_value + ap @context.last_value + end + end + else # MacRuby + IRB.formatter = Class.new(IRB::Formatter) do + def inspect_object(object) + object.ai + end + end.new + end rescue LoadError => e puts "!! Failed to load awesome print." end + +# Use the simple prompt if possible. +IRB.conf[:PROMPT_MODE] = :SIMPLE if IRB.conf[:PROMPT_MODE] == :DEFAULT + +# Setup permanent history. +HISTFILE = "~/.irb_history" +MAXHISTSIZE = 10000 +begin + histfile = File::expand_path(HISTFILE) + if File::exists?(histfile) + lines = IO::readlines(histfile).collect { |line| line.chomp } + puts "Read #{lines.length} saved history commands from '#{histfile}'." if $VERBOSE + Readline::HISTORY.push(*lines) + else + puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE + end + Kernel::at_exit do + lines = Readline::HISTORY.to_a.reverse.uniq.reverse + lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.length > MAXHISTSIZE + puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE + 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