From 468fbb867733f05bf93974c4ddfb28f3899ef216 Mon Sep 17 00:00:00 2001 From: manga_osyo Date: Tue, 16 Apr 2019 14:20:51 +0900 Subject: [PATCH] Add support cursor keys. --- lib/reline.rb | 10 ++++++---- lib/reline/key_stroke.rb | 4 ++++ lib/reline/line_editor.rb | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/reline.rb b/lib/reline.rb index 5db5f690f0..7a8dd44833 100644 --- a/lib/reline.rb +++ b/lib/reline.rb @@ -108,14 +108,16 @@ def self.inner_readline(prompt = '', add_hist = false, multiline, &confirm_multi @line_editor.completion_proc = @completion_proc @line_editor.retrieve_completion_block = method(:retrieve_completion_block) @line_editor.rerender + config = { key_mapping: { - # TODO - # "a" => "bb", - # "z" => "aa", - # "y" => "ak", + [27, 91, 65] => :ed_prev_history, # ↑ + [27, 91, 66] => :ed_next_history, # ↓ + [27, 91, 67] => :ed_next_char, # → + [27, 91, 68] => :ed_prev_char # ← } } + key_stroke = Reline::KeyStroke.new(config) begin while c = getc diff --git a/lib/reline/key_stroke.rb b/lib/reline/key_stroke.rb index 2fec45b667..ac0a820759 100644 --- a/lib/reline/key_stroke.rb +++ b/lib/reline/key_stroke.rb @@ -4,6 +4,10 @@ class Reline::KeyStroke def start_with?(other) other.size <= size && other == self.take(other.size) end + + def bytes + self + end end } diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 2cd27a1283..50ddfa72cc 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -488,6 +488,8 @@ def input_key(key) else @meta_prefix = true end + elsif Symbol === key and respond_to?(key, true) + process_key(key, key, method(key)) else normal_char(key) end