Skip to content

Commit

Permalink
Support oneshot key bindings config for key_trap of dialog callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Sep 5, 2021
1 parent ebc3e0f commit 5f1141b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
12 changes: 11 additions & 1 deletion lib/reline/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def initialize
@additional_key_bindings[:emacs] = {}
@additional_key_bindings[:vi_insert] = {}
@additional_key_bindings[:vi_command] = {}
@oneshot_key_bindings = {}
@skip_section = nil
@if_stack = nil
@editing_mode_label = :emacs
Expand All @@ -75,6 +76,7 @@ def reset
@additional_key_bindings.keys.each do |key|
@additional_key_bindings[key].clear
end
@oneshot_key_bindings.clear
reset_default_key_bindings
end

Expand Down Expand Up @@ -149,7 +151,15 @@ def read(file = nil)

def key_bindings
# override @key_actors[@editing_mode_label].default_key_bindings with @additional_key_bindings[@editing_mode_label]
@key_actors[@editing_mode_label].default_key_bindings.merge(@additional_key_bindings[@editing_mode_label])
@key_actors[@editing_mode_label].default_key_bindings.merge(@additional_key_bindings[@editing_mode_label]).merge(@oneshot_key_bindings)
end

def add_oneshot_key_binding(keystroke, target)
@oneshot_key_bindings[keystroke] = target
end

def reset_oneshot_key_bindings
@oneshot_key_bindings.clear
end

def add_default_key_binding_by_keymap(keymap, keystroke, target)
Expand Down
19 changes: 14 additions & 5 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,9 @@ class Dialog
attr_reader :name, :contents, :width
attr_accessor :scroll_top, :column, :vertical_offset, :lines_backup, :trap_key

def initialize(name, proc_scope)
def initialize(name, config, proc_scope)
@name = name
@config = config
@proc_scope = proc_scope
@width = nil
@scroll_top = 0
Expand All @@ -575,13 +576,21 @@ def contents=(contents)
def call(key)
@proc_scope.set_dialog(self)
@proc_scope.set_key(key)
@proc_scope.call
dialog_render_info = @proc_scope.call
if @trap_key
if @trap_key.is_a?(Array)
@config.add_oneshot_key_binding(@trap_key, @name)
elsif @trap_key.is_a?(Integer) or @trap_key.is_a?(Reline::Key)
@config.add_oneshot_key_binding([@trap_key], @name)
end
end
dialog_render_info
end
end

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

DIALOG_HEIGHT = 20
Expand Down Expand Up @@ -1497,9 +1506,9 @@ def wrap_method_call(method_symbol, method_obj, key, with_operator = false)

def input_key(key)
@last_key = key
@config.reset_oneshot_key_bindings
@dialogs.each do |dialog|
# The dialog will intercept the key if trap_key is set.
if dialog.trap_key and dialog.trap_key.match?(key)
if key.char.instance_of?(Symbol) and key.char == dialog.name
return
end
end
Expand Down

0 comments on commit 5f1141b

Please sign in to comment.