Sha256: 88a68d475698ef93b92252268f0cfa0f0aba109d9e8a701c2262b9a2b9855bf5

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Fusuma
  module Plugin
    module Detectors
      # Detect KeypressEvent from KeypressBuffer
      class KeypressDetector < Detector
        BUFFER_TYPE = 'keypress'

        # @param buffers [Array<Buffer>]
        # @return [Event] if event is detected
        # @return [NilClass] if event is NOT detected
        def detect(buffers)
          buffer = buffers.find { |b| b.type == BUFFER_TYPE }

          return if buffer.empty?

          records = buffer.events.map(&:record)

          index = create_index(records: records)

          index_record = Events::Records::IndexRecord.new(
            index: index,
            position: :surfix
          )
          create_event(record: index_record)
        end

        # @param records [Array<Events::Records::KeypressRecord>]
        # @return [Config::Index]
        def create_index(records:)
          code = records.map(&:code).join('+')
          Config::Index.new(
            [
              Config::Index::Key.new('keypress', skippable: true),
              Config::Index::Key.new(code, skippable: true)
            ]
          )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fusuma-plugin-keypress-0.1.0 lib/fusuma/plugin/detectors/keypress_detector.rb