Sha256: b340fc4c29cf74dee1a3bef2a26de7846bdaa388e4e6dc4395665be30b61db37

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 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   = []

      container << colour.set

      output.map do |lines|
        if lines.size < height
          remaining = height - lines.size
          remaining.times { |i| lines << "" }
        end

        lines.each_with_index do |stream, index|
          streams << clear(index)
          streams << Directive.enact(interface, stream)
        end

        container << streams.join
        streams = []
      end

      container << colour.reset

      container
    end

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

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

    def height
      geometry.height
    end

    def width
      geometry.width
    end

    def geometry
      interface.geometry
    end

    def colour
      interface.colour
    end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.0.16 lib/vedeu/output/compositor.rb