1# 2# history.rb - 3# $Release Version: 0.9.6$ 4# $Revision: 38515 $ 5# by Keiju ISHITSUKA(keiju@ruby-lang.org) 6# 7# -- 8# 9# 10# 11 12module IRB # :nodoc: 13 14 class Context 15 16 NOPRINTING_IVARS.push "@eval_history_values" 17 18 # See #set_last_value 19 alias _set_last_value set_last_value 20 21 def set_last_value(value) 22 _set_last_value(value) 23 24# @workspace.evaluate self, "_ = IRB.CurrentContext.last_value" 25 if @eval_history #and !@eval_history_values.equal?(llv) 26 @eval_history_values.push @line_no, @last_value 27 @workspace.evaluate self, "__ = IRB.CurrentContext.instance_eval{@eval_history_values}" 28 end 29 30 @last_value 31 end 32 33 # The command result history limit. 34 attr_reader :eval_history 35 # Sets command result history limit. 36 # 37 # +no+ is an Integer or +nil+. 38 # 39 # Returns +no+ of history items if greater than 0. 40 # 41 # If +no+ is 0, the number of history items is unlimited. 42 # 43 # If +no+ is +nil+, execution result history isn't used (default). 44 def eval_history=(no) 45 if no 46 if defined?(@eval_history) && @eval_history 47 @eval_history_values.size(no) 48 else 49 @eval_history_values = History.new(no) 50 IRB.conf[:__TMP__EHV__] = @eval_history_values 51 @workspace.evaluate(self, "__ = IRB.conf[:__TMP__EHV__]") 52 IRB.conf.delete(:__TMP_EHV__) 53 end 54 else 55 @eval_history_values = nil 56 end 57 @eval_history = no 58 end 59 end 60 61 class History # :nodoc: 62 @RCS_ID='-$Id: history.rb 38515 2012-12-21 05:45:50Z zzak $-' 63 64 def initialize(size = 16) 65 @size = size 66 @contents = [] 67 end 68 69 def size(size) 70 if size != 0 && size < @size 71 @contents = @contents[@size - size .. @size] 72 end 73 @size = size 74 end 75 76 def [](idx) 77 begin 78 if idx >= 0 79 @contents.find{|no, val| no == idx}[1] 80 else 81 @contents[idx][1] 82 end 83 rescue NameError 84 nil 85 end 86 end 87 88 def push(no, val) 89 @contents.push [no, val] 90 @contents.shift if @size != 0 && @contents.size > @size 91 end 92 93 alias real_inspect inspect 94 95 def inspect 96 if @contents.empty? 97 return real_inspect 98 end 99 100 unless (last = @contents.pop)[1].equal?(self) 101 @contents.push last 102 last = nil 103 end 104 str = @contents.collect{|no, val| 105 if val.equal?(self) 106 "#{no} ...self-history..." 107 else 108 "#{no} #{val.inspect}" 109 end 110 }.join("\n") 111 if str == "" 112 str = "Empty." 113 end 114 @contents.push last if last 115 str 116 end 117 end 118end 119 120 121