diff --git a/lib/irb/context.rb b/lib/irb/context.rb index d755622f32bec7..bf5b4c5708cf15 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -154,8 +154,12 @@ def initialize(irb, workspace = nil, input_method = nil) def save_history=(val) IRB.conf[:SAVE_HISTORY] = val + if val - (IRB.conf[:MAIN_CONTEXT] || self).init_save_history + context = (IRB.conf[:MAIN_CONTEXT] || self) + if context.io.support_history_saving? && !context.io.singleton_class.include?(HistorySavingAbility) + context.io.extend(HistorySavingAbility) + end end end @@ -576,11 +580,5 @@ def transform_args?(command) command = command_aliases.fetch(command.to_sym, command) ExtendCommandBundle.load_command(command)&.respond_to?(:transform_args) end - - def init_save_history# :nodoc: - unless (class<<@io;self;end).include?(HistorySavingAbility) - @io.extend(HistorySavingAbility) - end - end end end diff --git a/lib/irb/history.rb b/lib/irb/history.rb index a1b0d5cba076d8..e18ff516b24e8c 100644 --- a/lib/irb/history.rb +++ b/lib/irb/history.rb @@ -7,7 +7,6 @@ def HistorySavingAbility.extended(obj) end def load_history - return unless self.class.const_defined?(:HISTORY) history = self.class::HISTORY if history_file = IRB.conf[:HISTORY_FILE] history_file = File.expand_path(history_file) @@ -31,7 +30,6 @@ def load_history end def save_history - return unless self.class.const_defined?(:HISTORY) history = self.class::HISTORY if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) != 0 if history_file = IRB.conf[:HISTORY_FILE] diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index 3e7fac2ace3235..643e89f27e0b2a 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -47,6 +47,10 @@ def readable_after_eof? false end + def support_history_saving? + false + end + # For debug message def inspect 'Abstract InputMethod' @@ -230,6 +234,10 @@ def readable_after_eof? true end + def support_history_saving? + true + end + # Returns the current line number for #io. # # #line counts the number of times #gets is called. @@ -256,6 +264,7 @@ def inspect end class RelineInputMethod < InputMethod + HISTORY = Reline::HISTORY # Creates a new input method object using Reline def initialize IRB.__send__(:set_encoding, Reline.encoding_system_needs.name, override: false) @@ -458,6 +467,10 @@ def inspect str += " and #{inputrc_path}" if File.exist?(inputrc_path) str end + + def support_history_saving? + true + end end class ReidlineInputMethod < RelineInputMethod