2019-04-27 01:53:09 -04:00
|
|
|
class Reline::KeyStroke
|
|
|
|
using Module.new {
|
|
|
|
refine Array do
|
|
|
|
def start_with?(other)
|
|
|
|
other.size <= size && other == self.take(other.size)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bytes
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
def initialize(config)
|
|
|
|
@config = config
|
|
|
|
end
|
|
|
|
|
2019-05-24 10:38:40 -04:00
|
|
|
# Keystrokes of GNU Readline will timeout it with the specification of
|
|
|
|
# "keyseq-timeout" when waiting for the 2nd character after the 1st one.
|
|
|
|
# If the 2nd character comes after 1st ESC without timeout it has a
|
|
|
|
# meta-property of meta-key to discriminate modified key with meta-key
|
|
|
|
# from multibyte characters that come with 8th bit on.
|
|
|
|
#
|
|
|
|
# GNU Readline will wait for the 2nd character with "keyseq-timeout"
|
|
|
|
# milli-seconds but wait forever after 3rd characters.
|
|
|
|
def read_io(keyseq_timeout, &block)
|
|
|
|
buffer = []
|
|
|
|
loop do
|
|
|
|
c = Reline::IOGate.getc
|
|
|
|
buffer << c
|
|
|
|
result = match_status(buffer)
|
|
|
|
case result
|
|
|
|
when :matched
|
2019-05-24 19:24:41 -04:00
|
|
|
block.(expand(buffer).map{ |c| Reline::Key.new(c, c, false) })
|
2019-05-24 10:38:40 -04:00
|
|
|
break
|
|
|
|
when :matching
|
|
|
|
if buffer.size == 1
|
|
|
|
begin
|
|
|
|
succ_c = nil
|
|
|
|
Timeout.timeout(keyseq_timeout / 1000.0) {
|
|
|
|
succ_c = Reline::IOGate.getc
|
|
|
|
}
|
|
|
|
rescue Timeout::Error # cancel matching only when first byte
|
|
|
|
block.([Reline::Key.new(c, c, false)])
|
|
|
|
break
|
|
|
|
else
|
|
|
|
if match_status(buffer.dup.push(succ_c)) == :unmatched
|
|
|
|
if c == "\e".ord
|
|
|
|
block.([Reline::Key.new(succ_c, succ_c | 0b10000000, true)])
|
|
|
|
else
|
|
|
|
block.([Reline::Key.new(c, c, false), Reline::Key.new(succ_c, succ_c, false)])
|
|
|
|
end
|
|
|
|
break
|
|
|
|
else
|
|
|
|
Reline::IOGate.ungetc(succ_c)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when :unmatched
|
|
|
|
if buffer.size == 1 and c == "\e".ord
|
2019-05-24 19:08:54 -04:00
|
|
|
read_escaped_key(keyseq_timeout, buffer, block)
|
2019-05-24 10:38:40 -04:00
|
|
|
else
|
|
|
|
block.(buffer.map{ |c| Reline::Key.new(c, c, false) })
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-24 19:08:54 -04:00
|
|
|
def read_escaped_key(keyseq_timeout, buffer, block)
|
2019-05-24 10:38:40 -04:00
|
|
|
begin
|
|
|
|
escaped_c = nil
|
|
|
|
Timeout.timeout(keyseq_timeout / 1000.0) {
|
|
|
|
escaped_c = Reline::IOGate.getc
|
|
|
|
}
|
|
|
|
rescue Timeout::Error # independent ESC
|
|
|
|
block.([Reline::Key.new(c, c, false)])
|
|
|
|
else
|
|
|
|
if escaped_c.nil?
|
|
|
|
block.([Reline::Key.new(c, c, false)])
|
|
|
|
elsif escaped_c >= 128 # maybe, first byte of multi byte
|
|
|
|
block.([Reline::Key.new(c, c, false), Reline::Key.new(escaped_c, escaped_c, false)])
|
|
|
|
elsif escaped_c == "\e".ord # escape twice
|
|
|
|
block.([Reline::Key.new(c, c, false), Reline::Key.new(c, c, false)])
|
|
|
|
else
|
|
|
|
block.([Reline::Key.new(escaped_c, escaped_c | 0b10000000, true)])
|
|
|
|
end
|
2019-05-12 13:26:31 -04:00
|
|
|
end
|
2019-04-27 01:53:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def match_status(input)
|
|
|
|
key_mapping.keys.select { |lhs|
|
|
|
|
lhs.start_with? input
|
|
|
|
}.tap { |it|
|
|
|
|
return :matched if it.size == 1 && (it.max_by(&:size)&.size&.== input.size)
|
|
|
|
return :matching if it.size == 1 && (it.max_by(&:size)&.size&.!= input.size)
|
|
|
|
return :matched if it.max_by(&:size)&.size&.< input.size
|
|
|
|
return :matching if it.size > 1
|
|
|
|
}
|
|
|
|
key_mapping.keys.select { |lhs|
|
|
|
|
input.start_with? lhs
|
|
|
|
}.tap { |it|
|
|
|
|
return it.size > 0 ? :matched : :unmatched
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-05-24 10:38:40 -04:00
|
|
|
private
|
|
|
|
|
2019-04-27 01:53:09 -04:00
|
|
|
def expand(input)
|
|
|
|
lhs = key_mapping.keys.select { |lhs| input.start_with? lhs }.sort_by(&:size).reverse.first
|
|
|
|
return input unless lhs
|
|
|
|
rhs = key_mapping[lhs]
|
|
|
|
|
|
|
|
case rhs
|
|
|
|
when String
|
|
|
|
rhs_bytes = rhs.bytes
|
|
|
|
expand(expand(rhs_bytes) + expand(input.drop(lhs.size)))
|
|
|
|
when Symbol
|
|
|
|
[rhs] + expand(input.drop(lhs.size))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def key_mapping
|
|
|
|
@config[:key_mapping].transform_keys(&:bytes)
|
|
|
|
end
|
|
|
|
end
|