2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2002-07-09 07:17:17 -04:00
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# history.rb -
|
2009-07-07 07:36:20 -04:00
|
|
|
# $Release Version: 0.9.6$
|
2002-07-09 07:17:17 -04:00
|
|
|
# $Revision$
|
2005-04-13 11:27:09 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2002-07-09 07:17:17 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2002-07-09 07:17:17 -04:00
|
|
|
#
|
|
|
|
|
2012-12-13 00:22:30 -05:00
|
|
|
module IRB # :nodoc:
|
2002-07-09 07:17:17 -04:00
|
|
|
|
|
|
|
class Context
|
|
|
|
|
|
|
|
NOPRINTING_IVARS.push "@eval_history_values"
|
|
|
|
|
2012-12-21 00:45:50 -05:00
|
|
|
# See #set_last_value
|
2002-07-09 07:17:17 -04:00
|
|
|
alias _set_last_value set_last_value
|
|
|
|
|
|
|
|
def set_last_value(value)
|
|
|
|
_set_last_value(value)
|
|
|
|
|
2019-06-13 03:09:02 -04:00
|
|
|
if defined?(@eval_history) && @eval_history
|
2014-08-08 21:36:49 -04:00
|
|
|
@eval_history_values.push @line_no, @last_value
|
|
|
|
@workspace.evaluate self, "__ = IRB.CurrentContext.instance_eval{@eval_history_values}"
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@last_value
|
|
|
|
end
|
|
|
|
|
2019-06-13 03:09:02 -04:00
|
|
|
remove_method :eval_history= if method_defined?(:eval_history=)
|
2019-10-26 04:04:24 -04:00
|
|
|
# The command result history limit. This method is not available until
|
|
|
|
# #eval_history= was called with non-nil value (directly or via
|
|
|
|
# setting <code>IRB.conf[:EVAL_HISTORY]</code> in <code>.irbrc</code>).
|
2002-07-09 07:17:17 -04:00
|
|
|
attr_reader :eval_history
|
2019-10-26 04:04:24 -04:00
|
|
|
# Sets command result history limit. Default value is set from
|
|
|
|
# <code>IRB.conf[:EVAL_HISTORY]</code>.
|
2012-12-13 00:22:30 -05:00
|
|
|
#
|
|
|
|
# +no+ is an Integer or +nil+.
|
|
|
|
#
|
|
|
|
# Returns +no+ of history items if greater than 0.
|
|
|
|
#
|
|
|
|
# If +no+ is 0, the number of history items is unlimited.
|
|
|
|
#
|
|
|
|
# If +no+ is +nil+, execution result history isn't used (default).
|
2019-10-26 04:04:24 -04:00
|
|
|
#
|
|
|
|
# History values are available via <code>__</code> variable, see
|
|
|
|
# IRB::History.
|
2002-07-09 07:17:17 -04:00
|
|
|
def eval_history=(no)
|
|
|
|
if no
|
2014-08-08 21:36:49 -04:00
|
|
|
if defined?(@eval_history) && @eval_history
|
|
|
|
@eval_history_values.size(no)
|
|
|
|
else
|
|
|
|
@eval_history_values = History.new(no)
|
|
|
|
IRB.conf[:__TMP__EHV__] = @eval_history_values
|
|
|
|
@workspace.evaluate(self, "__ = IRB.conf[:__TMP__EHV__]")
|
|
|
|
IRB.conf.delete(:__TMP_EHV__)
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@eval_history_values = nil
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
@eval_history = no
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-26 04:04:24 -04:00
|
|
|
# Represents history of results of previously evaluated commands.
|
|
|
|
#
|
|
|
|
# Available via <code>__</code> variable, only if <code>IRB.conf[:EVAL_HISTORY]</code>
|
|
|
|
# or <code>IRB::CurrentContext().eval_history</code> is non-nil integer value
|
|
|
|
# (by default it is +nil+).
|
|
|
|
#
|
|
|
|
# Example (in `irb`):
|
|
|
|
#
|
|
|
|
# # Initialize history
|
|
|
|
# IRB::CurrentContext().eval_history = 10
|
|
|
|
# # => 10
|
|
|
|
#
|
|
|
|
# # Perform some commands...
|
|
|
|
# 1 + 2
|
|
|
|
# # => 3
|
|
|
|
# puts 'x'
|
|
|
|
# # x
|
|
|
|
# # => nil
|
|
|
|
# raise RuntimeError
|
|
|
|
# # ...error raised
|
|
|
|
#
|
|
|
|
# # Inspect history (format is "<item number> <evaluated value>":
|
|
|
|
# __
|
|
|
|
# # => 1 10
|
|
|
|
# # 2 3
|
|
|
|
# # 3 nil
|
|
|
|
#
|
|
|
|
# __[1]
|
|
|
|
# # => 10
|
|
|
|
#
|
|
|
|
class History
|
|
|
|
|
|
|
|
def initialize(size = 16) # :nodoc:
|
2002-07-09 07:17:17 -04:00
|
|
|
@size = size
|
|
|
|
@contents = []
|
|
|
|
end
|
|
|
|
|
2019-10-26 04:04:24 -04:00
|
|
|
def size(size) # :nodoc:
|
2009-03-05 22:56:38 -05:00
|
|
|
if size != 0 && size < @size
|
2014-08-08 21:36:49 -04:00
|
|
|
@contents = @contents[@size - size .. @size]
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
@size = size
|
|
|
|
end
|
|
|
|
|
2019-10-26 04:04:24 -04:00
|
|
|
# Get one item of the content (both positive and negative indexes work).
|
2002-07-09 07:17:17 -04:00
|
|
|
def [](idx)
|
|
|
|
begin
|
2014-08-08 21:36:49 -04:00
|
|
|
if idx >= 0
|
|
|
|
@contents.find{|no, val| no == idx}[1]
|
|
|
|
else
|
|
|
|
@contents[idx][1]
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
rescue NameError
|
2014-08-08 21:36:49 -04:00
|
|
|
nil
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-26 04:04:24 -04:00
|
|
|
def push(no, val) # :nodoc:
|
2002-07-09 07:17:17 -04:00
|
|
|
@contents.push [no, val]
|
|
|
|
@contents.shift if @size != 0 && @contents.size > @size
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
alias real_inspect inspect
|
|
|
|
|
2019-10-26 04:04:24 -04:00
|
|
|
def inspect # :nodoc:
|
2002-07-09 07:17:17 -04:00
|
|
|
if @contents.empty?
|
2014-08-08 21:36:49 -04:00
|
|
|
return real_inspect
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
unless (last = @contents.pop)[1].equal?(self)
|
2014-08-08 21:36:49 -04:00
|
|
|
@contents.push last
|
|
|
|
last = nil
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
str = @contents.collect{|no, val|
|
2014-08-08 21:36:49 -04:00
|
|
|
if val.equal?(self)
|
|
|
|
"#{no} ...self-history..."
|
|
|
|
else
|
|
|
|
"#{no} #{val.inspect}"
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
}.join("\n")
|
|
|
|
if str == ""
|
2014-08-08 21:36:49 -04:00
|
|
|
str = "Empty."
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
@contents.push last if last
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|