diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 6f209b596a48c9..d755622f32bec7 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -8,6 +8,7 @@ require_relative "inspector" require_relative "input-method" require_relative "output-method" +require_relative "history" module IRB # A class that wraps the current state of the irb session, including the @@ -151,6 +152,27 @@ def initialize(irb, workspace = nil, input_method = nil) @command_aliases = IRB.conf[:COMMAND_ALIASES] end + def save_history=(val) + IRB.conf[:SAVE_HISTORY] = val + if val + (IRB.conf[:MAIN_CONTEXT] || self).init_save_history + end + end + + def save_history + IRB.conf[:SAVE_HISTORY] + end + + # A copy of the default IRB.conf[:HISTORY_FILE] + def history_file + IRB.conf[:HISTORY_FILE] + end + + # Set IRB.conf[:HISTORY_FILE] to the given +hist+. + def history_file=(hist) + IRB.conf[:HISTORY_FILE] = hist + end + # The top-level workspace, see WorkSpace#main def main @workspace.main @@ -554,5 +576,11 @@ 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/extend-command.rb b/lib/irb/extend-command.rb index 8b074262ee6705..7238f1fd1c42de 100644 --- a/lib/irb/extend-command.rb +++ b/lib/irb/extend-command.rb @@ -319,7 +319,6 @@ module ContextExtender [:eval_history=, "ext/history.rb"], [:use_tracer=, "ext/tracer.rb"], [:use_loader=, "ext/use-loader.rb"], - [:save_history=, "ext/save-history.rb"], ] # Installs the default context extensions as irb commands: @@ -327,7 +326,6 @@ module ContextExtender # Context#eval_history=:: +irb/ext/history.rb+ # Context#use_tracer=:: +irb/ext/tracer.rb+ # Context#use_loader=:: +irb/ext/use-loader.rb+ - # Context#save_history=:: +irb/ext/save-history.rb+ def self.install_extend_commands for args in @EXTEND_COMMANDS def_extend_command(*args) diff --git a/lib/irb/ext/save-history.rb b/lib/irb/history.rb similarity index 64% rename from lib/irb/ext/save-history.rb rename to lib/irb/history.rb index b1987ea6465abd..a1b0d5cba076d8 100644 --- a/lib/irb/ext/save-history.rb +++ b/lib/irb/history.rb @@ -1,55 +1,4 @@ -# frozen_string_literal: false -# -# save-history.rb - -# by Keiju ISHITSUKA(keiju@ruby-lang.org) -# - module IRB - module HistorySavingAbility # :nodoc: - end - - class Context - def init_save_history# :nodoc: - unless (class<<@io;self;end).include?(HistorySavingAbility) - @io.extend(HistorySavingAbility) - end - end - - # A copy of the default IRB.conf[:SAVE_HISTORY] - def save_history - IRB.conf[:SAVE_HISTORY] - end - - remove_method(:save_history=) if method_defined?(:save_history=) - # Sets IRB.conf[:SAVE_HISTORY] to the given +val+ and calls - # #init_save_history with this context. - # - # Will store the number of +val+ entries of history in the #history_file - # - # Add the following to your +.irbrc+ to change the number of history - # entries stored to 1000: - # - # IRB.conf[:SAVE_HISTORY] = 1000 - def save_history=(val) - IRB.conf[:SAVE_HISTORY] = val - if val - main_context = IRB.conf[:MAIN_CONTEXT] - main_context = self unless main_context - main_context.init_save_history - end - end - - # A copy of the default IRB.conf[:HISTORY_FILE] - def history_file - IRB.conf[:HISTORY_FILE] - end - - # Set IRB.conf[:HISTORY_FILE] to the given +hist+. - def history_file=(hist) - IRB.conf[:HISTORY_FILE] = hist - end - end - module HistorySavingAbility # :nodoc: def HistorySavingAbility.extended(obj) IRB.conf[:AT_EXIT].push proc{obj.save_history} diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb index fc81d53005b9c1..f0dfa03c6fa9ce 100644 --- a/test/irb/test_history.rb +++ b/test/irb/test_history.rb @@ -1,6 +1,5 @@ # frozen_string_literal: false require 'irb' -require 'irb/ext/save-history' require 'readline' require_relative "helper"