ruby-changes:67615
From: aycabta <ko1@a...>
Date: Mon, 6 Sep 2021 05:23:14 +0900 (JST)
Subject: [ruby-changes:67615] 544cd3fb91 (master): [ruby/reline] Support oneshot key bindings config for key_trap of dialog callbacks
https://git.ruby-lang.org/ruby.git/commit/?id=544cd3fb91 From 544cd3fb910ff41cdc87c70edf1d00e4f6d5b435 Mon Sep 17 00:00:00 2001 From: aycabta <aycabta@g...> Date: Sun, 5 Sep 2021 23:33:59 +0900 Subject: [ruby/reline] Support oneshot key bindings config for key_trap of dialog callbacks https://github.com/ruby/reline/commit/5f1141b693 --- lib/reline/config.rb | 12 +++++++++++- lib/reline/line_editor.rb | 19 ++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/reline/config.rb b/lib/reline/config.rb index d859aeb..ee73143 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -50,6 +50,7 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L50 @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 @@ -75,6 +76,7 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L76 @additional_key_bindings.keys.each do |key| @additional_key_bindings[key].clear end + @oneshot_key_bindings.clear reset_default_key_bindings end @@ -149,7 +151,15 @@ class Reline::Config https://github.com/ruby/ruby/blob/trunk/lib/reline/config.rb#L151 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) diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 21db97f..c2ef197 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -550,8 +550,9 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L550 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 @@ -575,13 +576,21 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L576 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 @@ -1497,9 +1506,9 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L1506 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 -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/