Skip to content

Commit

Permalink
Add support for overwriting dialog proc with the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Dec 20, 2021
1 parent 7e5dbe4 commit 16aa20c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
14 changes: 6 additions & 8 deletions lib/reline.rb
Expand Up @@ -60,7 +60,7 @@ class Core

def initialize
self.output = STDOUT
@dialog_proc_list = []
@dialog_proc_list = {}
yield self
@completion_quote_character = nil
@bracketed_paste_finished = false
Expand Down Expand Up @@ -155,16 +155,15 @@ def dig_perfect_match_proc=(p)
@dig_perfect_match_proc = p
end

DialogProc = Struct.new(:dialog_proc, :context)
def add_dialog_proc(name_sym, p, context = nil)
raise ArgumentError unless p.respond_to?(:call) or p.nil?
raise ArgumentError unless name_sym.instance_of?(Symbol)
@dialog_proc_list << [name_sym, p, context]
@dialog_proc_list[name_sym] = DialogProc.new(p, context)
end

def dialog_proc(name_sym)
dialog = @dialog_proc_list.find { |d| d[0] == name_sym }
dialog.nil? ? nil : dialog[1]
#@dialog_proc_list[name_sym]
@dialog_proc_list[name_sym]
end

def input=(val)
Expand Down Expand Up @@ -307,9 +306,8 @@ def readline(prompt = '', add_hist = false)
line_editor.auto_indent_proc = auto_indent_proc
line_editor.dig_perfect_match_proc = dig_perfect_match_proc
line_editor.pre_input_hook = pre_input_hook
@dialog_proc_list.each do |d|
name_sym, dialog_proc, context = d
line_editor.add_dialog_proc(name_sym, dialog_proc, context)
@dialog_proc_list.each_pair do |name_sym, d|
line_editor.add_dialog_proc(name_sym, d.dialog_proc, d.context)
end

unless config.test_mode
Expand Down
8 changes: 6 additions & 2 deletions lib/reline/line_editor.rb
Expand Up @@ -634,8 +634,12 @@ def call(key)
end

def add_dialog_proc(name, p, context = nil)
return if @dialogs.any? { |d| d.name == name }
@dialogs << Dialog.new(name, @config, DialogProcScope.new(self, @config, p, context))
dialog = Dialog.new(name, @config, DialogProcScope.new(self, @config, p, context))
if index = @dialogs.find_index { |d| d.name == name }
@dialogs[index] = dialog
else
@dialogs << dialog
end
end

DIALOG_DEFAULT_HEIGHT = 20
Expand Down
9 changes: 6 additions & 3 deletions test/reline/test_reline.rb
Expand Up @@ -313,11 +313,13 @@ def test_emacs_editing_mode
def test_add_dialog_proc
p = proc {}
Reline.add_dialog_proc(:test_proc, p)
assert_equal(p, Reline.dialog_proc(:test_proc))
d = Reline.dialog_proc(:test_proc)
assert_equal(p, d.dialog_proc)

l = lambda {}
Reline.add_dialog_proc(:test_lambda, l)
assert_equal(l, Reline.dialog_proc(:test_lambda))
d = Reline.dialog_proc(:test_lambda)
assert_equal(l, d.dialog_proc)

assert_equal(nil, Reline.dialog_proc(:test_nothing))

Expand All @@ -327,7 +329,8 @@ def test_add_dialog_proc

dummy = DummyCallbackObject.new
Reline.add_dialog_proc(:dummy, dummy)
assert_equal(dummy, Reline.dialog_proc(:dummy))
d = Reline.dialog_proc(:dummy)
assert_equal(dummy, d.dialog_proc)
end

def test_readmultiline
Expand Down

0 comments on commit 16aa20c

Please sign in to comment.