[前][次][番号順一覧][スレッド一覧]

ruby-changes:67568

From: aycabta <ko1@a...>
Date: Fri, 3 Sep 2021 04:27:54 +0900 (JST)
Subject: [ruby-changes:67568] 38ae3b8e36 (master): [ruby/reline] Add key accessor for dialog callbacks

https://git.ruby-lang.org/ruby.git/commit/?id=38ae3b8e36

From 38ae3b8e36a6bdb39a8fa743789525ad764e064d Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Fri, 3 Sep 2021 01:14:11 +0900
Subject: [ruby/reline] Add key accessor for dialog callbacks

https://github.com/ruby/reline/commit/c949e44a14
---
 lib/reline/line_editor.rb | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb
index 4ed79ab..50aeac9 100644
--- a/lib/reline/line_editor.rb
+++ b/lib/reline/line_editor.rb
@@ -251,6 +251,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L251
     @in_pasting = false
     @auto_indent_proc = nil
     @dialogs = []
+    @last_key = nil
     reset_line
   end
 
@@ -512,6 +513,14 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L513
       @cursor_pos.y = row
     end
 
+    def set_key(key)
+      @key = key
+    end
+
+    def key
+      @key
+    end
+
     def cursor_pos
       @cursor_pos
     end
@@ -539,7 +548,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L548
 
   class Dialog
     attr_reader :name, :contents, :width
-    attr_accessor :scroll_top, :column, :vertical_offset, :lines_backup
+    attr_accessor :scroll_top, :column, :vertical_offset, :lines_backup, :trap_key
 
     def initialize(name, proc_scope)
       @name = name
@@ -563,8 +572,9 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L572
       end
     end
 
-    def call
+    def call(key)
       @proc_scope.set_dialog(self)
+      @proc_scope.set_key(key)
       @proc_scope.call
     end
   end
@@ -588,10 +598,11 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L598
   private def render_each_dialog(dialog, cursor_column)
     if @in_pasting
       dialog.contents = nil
+      dialog.trap_key = nil
       return
     end
     dialog.set_cursor_pos(cursor_column, @first_line_started_from + @started_from)
-    dialog_render_info = dialog.call
+    dialog_render_info = dialog.call(@last_key)
     if dialog_render_info.nil? or dialog_render_info.contents.nil? or dialog_render_info.contents.empty?
       dialog.lines_backup = {
         lines: modify_lines(whole_lines),
@@ -602,6 +613,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L613
       }
       clear_each_dialog(dialog)
       dialog.contents = nil
+      dialog.trap_key = nil
       return
     end
     old_dialog = dialog.clone
@@ -778,6 +790,7 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L790
   end
 
   private def clear_each_dialog(dialog)
+    dialog.trap_key = nil
     return unless dialog.contents
     prompt, prompt_width, prompt_list = check_multiline_prompt(dialog.lines_backup[:lines], prompt)
     visual_lines = []
@@ -1464,6 +1477,11 @@ class Reline::LineEditor https://github.com/ruby/ruby/blob/trunk/lib/reline/line_editor.rb#L1477
   end
 
   def input_key(key)
+    @last_key = key
+    @dialogs.each do |dialog|
+      # The dialog will intercept the key if trap_key is set.
+      return if dialog.trap_key and key.combined_char == dialog.trap_key
+    end
     @just_cursor_moving = nil
     if key.char.nil?
       if @first_char
-- 
cgit v1.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]