class VimGolf::Keylog

Constants

KC_1BYTE

Quick lookup array for single-byte keycodes

KC_MBYTE
NO_SNIFF_DATE_RANGE

Between these dates, assume KE_SNIFF is removed.

Public Class Methods

new(input, time=Time.now.utc) click to toggle source
# File lib/vimgolf/keylog.rb, line 8
def initialize(input, time=Time.now.utc)
  # Force encoding of solution text. Must match string literals.
  # .force_encoding CHANGES THE ORIGINAL STRING!
  @input = input.force_encoding(Encoding::ASCII_8BIT)
  @time = time
end

Public Instance Methods

convert(sep = '')
Alias for: to_s
each() { |code| ... } click to toggle source
# File lib/vimgolf/keylog.rb, line 22
def each
  scanner = StringScanner.new(@input)

  # A Vim keycode is either a single byte, or a 3-byte sequence starting
  # with 0x80.
  while (c = scanner.get_byte)
    n = c.ord
    if n == 0x80
      b2, b3 = scanner.get_byte, scanner.get_byte
      if b2 == "\xfd" && b3 >= "\x38" && @time.between?(*NO_SNIFF_DATE_RANGE)
        # Should we account for KE_SNIFF removal?
        b3 = (b3.ord + 1).chr
      end
      code = KC_MBYTE[b2+b3]
      yield code if code # ignore "nil" keystrokes (like window focus)
    else
      yield KC_1BYTE[n]
    end
  end
end
to_s(sep = '') click to toggle source
# File lib/vimgolf/keylog.rb, line 15
def to_s(sep = '')
  to_a.join(sep)
end
Also aliased as: convert