Skip to content

Commit 2c2bb1f

Browse files
committed
Combine MAPPINGS(single byte input to symbol) with key_bindings(escape sequence to symbol)
1 parent 07facbb commit 2c2bb1f

File tree

10 files changed

+135
-192
lines changed

10 files changed

+135
-192
lines changed

lib/reline.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ module Reline
1818

1919
class ConfigEncodingConversionError < StandardError; end
2020

21-
Key = Struct.new(:char, :combined_char, :with_meta) do
21+
# EOF key: { char: nil, method_symbol: nil }
22+
# Other key: { char: String, method_symbol: Symbol }
23+
Key = Struct.new(:char, :method_symbol, :unused_boolean) do
2224
# For dialog_proc `key.match?(dialog.name)`
2325
def match?(sym)
24-
combined_char.is_a?(Symbol) && combined_char == sym
26+
method_symbol && method_symbol == sym
2527
end
2628
end
2729
CursorPos = Struct.new(:x, :y)
@@ -341,7 +343,7 @@ def readline(prompt = '', add_hist = false)
341343
read_io(config.keyseq_timeout) { |inputs|
342344
line_editor.set_pasting_state(io_gate.in_pasting?)
343345
inputs.each do |key|
344-
if key.char == :bracketed_paste_start
346+
if key.method_symbol == :bracketed_paste_start
345347
text = io_gate.read_bracketed_paste
346348
line_editor.insert_pasted_text(text)
347349
line_editor.scroll_into_view

lib/reline/key_actor/base.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
class Reline::KeyActor::Base
2-
def initialize(mapping = [])
3-
@mapping = mapping
2+
def initialize(mappings = nil)
43
@matching_bytes = {}
54
@key_bindings = {}
5+
add_mappings(mappings) if mappings
66
end
77

8-
def get_method(key)
9-
@mapping[key]
8+
def add_mappings(mappings)
9+
add([27], :ed_ignore)
10+
128.times do |key|
11+
func = mappings[key]
12+
meta_func = mappings[key | 0b10000000]
13+
add([key], func) unless func == :ed_unassigned
14+
add([27, key], meta_func) unless meta_func == :ed_unassigned
15+
end
1016
end
1117

1218
def add(key, func)

lib/reline/key_stroke.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ def initialize(config, encoding)
2020
def match_status(input)
2121
matching = key_mapping.matching?(input)
2222
matched = key_mapping.get(input)
23-
24-
# FIXME: Workaround for single byte. remove this after MAPPING is merged into KeyActor.
25-
matched ||= input.size == 1 && input[0] < 0x80
26-
matching ||= input == [ESC_BYTE]
27-
2823
if matching && matched
2924
MATCHING_MATCHED
3025
elsif matching
@@ -55,16 +50,14 @@ def expand(input)
5550
return [[], []] unless matched_bytes
5651

5752
func = key_mapping.get(matched_bytes)
53+
s = matched_bytes.pack('c*').force_encoding(@encoding)
5854
if func.is_a?(Array)
59-
keys = func.map { |c| Reline::Key.new(c, c, false) }
55+
keys = func.map { |c| Reline::Key.new(c.chr(@encoding), :ed_insert, false) }
6056
elsif func
61-
keys = [Reline::Key.new(func, func, false)]
62-
elsif matched_bytes.size == 2 && matched_bytes[0] == ESC_BYTE
63-
keys = [Reline::Key.new(matched_bytes[1], matched_bytes[1] | 0b10000000, true)]
57+
keys = [Reline::Key.new(s, func, false)]
6458
else
65-
s = matched_bytes.pack('c*').force_encoding(@encoding)
6659
if s.valid_encoding? && s.size == 1
67-
keys = [Reline::Key.new(s.ord, s.ord, false)]
60+
keys = [Reline::Key.new(s, :ed_insert, false)]
6861
else
6962
keys = []
7063
end

0 commit comments

Comments
 (0)