Class: RawLine::KeycodeParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rawline/keycode_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(keymap) ⇒ KeycodeParser

Returns a new instance of KeycodeParser.



3
4
5
6
# File 'lib/rawline/keycode_parser.rb', line 3

def initialize(keymap)
  @keymap = keymap
  @escape_code = keymap[:escape]
end

Instance Method Details

#parse_bytes_into_sequences(bytes) ⇒ Object

Parses a collection of bytes into key code sequences. All multi-byte sequences (e.g. [27, 91, 67]) will be left alone where-as all other byte sequences will be converted to UTF-8.

E.g.

parse_bytes_into_sequences [97, 98, [27, 91, 67], 99, 198, 146]
# => ["a", "b", [27, 91, 67], "c", "ƒ"]


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rawline/keycode_parser.rb', line 16

def parse_bytes_into_sequences(bytes)
  key_codes = parse_bytes(bytes)

  byte_buffer = []
  sequences = key_codes.each_with_object([]) do |val, arr|
    if val.is_a?(Array)
      arr.push *convert_bytes_to_utf8(byte_buffer)
      arr << val
      byte_buffer = []
    else
      byte_buffer << val
    end
  end

  # don't forget about remaining bytes in the buffer
  if byte_buffer.any?
    sequences.push *convert_bytes_to_utf8(byte_buffer)
  end

  sequences
end