Irb itches eliminated
irb history. A more detailed explanation at:
http://www.chwhat.com/articles/2005/10/08/
1 2 require 'tempfile' 3 HISTFILE = "~/.irb.hist" 4 MAXHISTSIZE = 300 5 6 #borrowed from http://rubygarden.org/ruby?Irb/TipsAndTricks 7 begin 8 if defined? Readline::HISTORY 9 histfile = File::expand_path( HISTFILE ) 10 if File::exists?( histfile ) 11 lines = IO::readlines( histfile ).collect {|line| line.chomp} 12 puts "Read %d saved history commands from %s." % 13 [ lines.nitems, histfile ] if $DEBUG || $VERBOSE 14 Readline::HISTORY.push( *lines ) 15 else 16 puts "History file '%s' was empty or non-existant." % 17 histfile if $DEBUG || $VERBOSE 18 end 19 20 Kernel::at_exit { 21 lines = Readline::HISTORY.to_a.reverse.uniq.reverse 22 lines = lines[ -MAXHISTSIZE, MAXHISTSIZE ] if lines.nitems > MAXHISTSIZE 23 $stderr.puts "Saving %d history lines to %s." % 24 [ lines.length, histfile ] if $VERBOSE || $DEBUG 25 File::open( histfile, File::WRONLY|File::CREAT|File::TRUNC ) {|ofh| 26 lines.each {|line| ofh.puts line } 27 } 28 } 29 end 30 end 31 32 ## My stuff 33 class Array 34 def multislice(range,splitter=',',offset=nil) 35 result = [] 36 for r in range.split(splitter) 37 if r =~ /-/ 38 min,max = r.split('-') 39 slice_min = min.to_i - 1 40 slice_min += offset if offset 41 result.push(*self.slice(slice_min, max.to_i - min.to_i + 1)) 42 else 43 index = r.to_i - 1 44 index += offset if offset 45 result.push(self[index]) 46 end 47 end 48 return result 49 end 50 end 51 52 $original_history_size = Readline::HISTORY.size 53 def history_list(first_num,second_num=Readline::HISTORY.size - 1) 54 Readline::HISTORY.to_a[(first_num + $original_history_size - 1) .. (second_num + $original_history_size - 1) ] 55 end 56 def history_slice(nums) 57 Readline::HISTORY.to_a.multislice(nums,',',$original_history_size) 58 end 59 def history_list_or_slice(*args) 60 if args[0].class == String 61 history_slice(*args) 62 else 63 history_list(*args) 64 end 65 end 66 def print_history(*args) 67 puts history_list_or_slice(*args).join("\n") 68 end 69 def eval_history(*args) 70 eval %[ #{history_list_or_slice(*args).join("\n")} ] 71 end 72 def edit_history(*args) 73 history_string = history_list_or_slice(*args).join("\n") 74 edit(history_string) 75 end 76 def edit(string=nil,editor=ENV['EDITOR']) 77 editor ||= raise "editor must be given or defined by EDITOR environment variable" 78 tempfile = Tempfile.new('edit') 79 File.open(tempfile.path,'w') {|f| f.write(string) } if string 80 system("#{editor} #{tempfile.path}") 81 File.open(tempfile.path) {|f| f.read } 82 end