Skip to content

Commit

Permalink
Improve history-saving's ability check
Browse files Browse the repository at this point in the history
Instead of checking the existence of `input_method_class::HISTORY`, we should
make every input method class declare if it supports history saving or not.

Since the default value is `false`, it shouldn't break any custom input method
that inherits from `IRB::InputMethod`.
  • Loading branch information
st0012 committed Jul 14, 2023
1 parent 9e2bc35 commit 3d4b66c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 5 additions & 7 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
2 changes: 0 additions & 2 deletions lib/irb/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def readable_after_eof?
false
end

def support_history_saving?
false
end

# For debug message
def inspect
'Abstract InputMethod'
Expand Down Expand Up @@ -229,6 +233,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.
Expand Down Expand Up @@ -458,6 +466,10 @@ def inspect
str += " and #{inputrc_path}" if File.exist?(inputrc_path)
str
end

def support_history_saving?
true
end
end

class ReidlineInputMethod < RelineInputMethod
Expand Down

0 comments on commit 3d4b66c

Please sign in to comment.