Sha256: 80253460c822e0e944a77a4a2b0b52c3e3a4520c782f643cb82d6e37e24ad109

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

module Vedeu
  class UndefinedInterface < StandardError; end

  class Compositor
    class << self
      def arrange(output = [], interface = 'dummy')
        return if output.nil? || output.empty?

        if output.is_a?(Array)
          new(output, interface).arrange
        elsif output.is_a?(Hash)
          output.map { |i, o| new(o, i).arrange }
        end
      end
    end

    def initialize(output = [], interface = 'dummy')
      @output    = output || []
      @interface = interface
    end

    def arrange
      Renderer.write(composition)
    end

    private

    def composition
      container = []
      streams   = []

      output.map do |line|
        line.each_with_index do |stream, index|
          streams << clear(index)
          streams << Directive.enact(stream)
        end

        container << streams.join
        streams = []
      end

      container
    end

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

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

    def width
      geometry.width
    end

    def geometry
      target_interface.geometry
    end

    def output
      return @output.split if @output.is_a?(String)
      @output
    end

    def target_interface
      raise UndefinedInterface if interface.nil?
      interface
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.0.14 lib/vedeu/output/compositor.rb
vedeu-0.0.13 lib/vedeu/output/compositor.rb
vedeu-0.0.12 lib/vedeu/output/compositor.rb