aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStan Lo <stan001212@gmail.com>2023-07-03 14:48:19 +0100
committergit <svn-admin@ruby-lang.org>2023-07-03 13:48:23 +0000
commitaf9eeb19d8b73a951776ea91901618d6e038d030 (patch)
tree6636051b3dcff3c71a8ee84dd5df2ce2a6805b48
parent4430b73cee4aaa4f203e14368d93b3297505c63e (diff)
downloadruby-af9eeb19d8b73a951776ea91901618d6e038d030.tar.gz
[ruby/irb] Stop treating history-saving logic as extension
(https://github.com/ruby/irb/pull/613) Since `IRB.conf[:SAVE_HISTORY]` is assigned with 1000 by default, history-saving is a feature enabled by default. So it should not be treated as an extension, which adds unnecessary complexity to the code.
-rw-r--r--lib/irb/context.rb28
-rw-r--r--lib/irb/extend-command.rb2
-rw-r--r--lib/irb/history.rb (renamed from lib/irb/ext/save-history.rb)51
-rw-r--r--test/irb/test_history.rb1
4 files changed, 28 insertions, 54 deletions
diff --git a/lib/irb/context.rb b/lib/irb/context.rb
index 6f209b596a..d755622f32 100644
--- a/lib/irb/context.rb
+++ b/lib/irb/context.rb
@@ -8,6 +8,7 @@ require_relative "workspace"
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 @@ module IRB
@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 <code>IRB.conf[:HISTORY_FILE]</code>
+ def history_file
+ IRB.conf[:HISTORY_FILE]
+ end
+
+ # Set <code>IRB.conf[:HISTORY_FILE]</code> 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 @@ module IRB
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 8b074262ee..7238f1fd1c 100644
--- a/lib/irb/extend-command.rb
+++ b/lib/irb/extend-command.rb
@@ -319,7 +319,6 @@ module IRB # :nodoc:
[: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 IRB # :nodoc:
# 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
index b1987ea646..a1b0d5cba0 100644
--- a/lib/irb/ext/save-history.rb
+++ b/lib/irb/history.rb
@@ -1,56 +1,5 @@
-# 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 <code>IRB.conf[:SAVE_HISTORY]</code>
- def save_history
- IRB.conf[:SAVE_HISTORY]
- end
-
- remove_method(:save_history=) if method_defined?(:save_history=)
- # Sets <code>IRB.conf[:SAVE_HISTORY]</code> 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 <code>IRB.conf[:HISTORY_FILE]</code>
- def history_file
- IRB.conf[:HISTORY_FILE]
- end
-
- # Set <code>IRB.conf[:HISTORY_FILE]</code> 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}
obj.load_history
diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb
index fc81d53005..f0dfa03c6f 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"