Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop treating history-saving logic as extension #613

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
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 @@ -551,5 +573,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: false
require 'irb'
require 'irb/ext/save-history'
require 'readline'

require_relative "helper"
Expand Down