Skip to content

Commit

Permalink
[ruby/irb] Stop treating history-saving logic as extension
Browse files Browse the repository at this point in the history
(ruby/irb#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.
  • Loading branch information
st0012 authored and matzbot committed Jul 3, 2023
1 parent 4430b73 commit af9eeb1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 54 deletions.
28 changes: 28 additions & 0 deletions lib/irb/context.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 <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
Expand Down Expand Up @@ -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
2 changes: 0 additions & 2 deletions lib/irb/extend-command.rb
Expand Up @@ -319,15 +319,13 @@ 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:
#
# 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)
Expand Down
51 changes: 0 additions & 51 deletions lib/irb/ext/save-history.rb → 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 <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}
Expand Down
1 change: 0 additions & 1 deletion 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"
Expand Down

0 comments on commit af9eeb1

Please sign in to comment.