Sha256: df25746e151a119e275bc8ba498a93a4cbad8cef8bbe3e6286a135f0e0600cca

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Vedeu
  class Compositor
    class << self
      def arrange(output = {})
        output.map do |interface, stream|
          new({ interface: interface, stream: stream }).arrange
        end
      end
    end

    def initialize(attributes = {})
      @attributes = attributes || {}
      @interface  = attributes[:interface]
      @stream     = attributes[:stream]
    end

    def arrange
      interface.enqueue(composition)
    end

    private

    attr_reader :attributes, :stream

    def composition
      container = []

      pad_stream

      stream.each_with_index do |lines, index|
        line = [interface.colour.set, clear(index)]

        lines.each do |data|
          line << Directive.enact(interface, data)
        end

        line << interface.colour.reset << interface.cursor

        container << line.join
      end

      container
    end

    def pad_stream
      if stream.size < height
        remaining = height - stream.size
        remaining.times { |i| stream << [{ text: '' }] }
      end
    end

    def clear(index)
      [origin(index), (' ' * width), origin(index)].join
    end

    def origin(index)
      interface.geometry.origin(index)
    end

    def height
      interface.geometry.height
    end

    def width
      interface.geometry.width
    end

    def interface
      @_interface ||= InterfaceRepository.find(@interface)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.0.25 lib/vedeu/output/compositor.rb
vedeu-0.0.24 lib/vedeu/output/compositor.rb
vedeu-0.0.23 lib/vedeu/output/compositor.rb