Sha256: 55d50a2421d7b444fc2bfbd0a92b8eeb86b49b659d12ed9fc9fa0e3c009823de

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

require 'vedeu/output/clear_interface'

module Vedeu
  class RenderInterface
    def self.call(interface)
      new(interface).render
    end

    def initialize(interface)
      @interface = interface
    end

    def render
      out = [ClearInterface.call(interface)]
      processed_lines.each_with_index do |line, index|
        out << interface.origin(index)
        out << line.to_s
      end
      out.join
    end

    private

    attr_reader :interface

    def processed_lines
      lines.each do |line|
        if line.streams.any?
          processed_streams = []
          line_length       = 0
          line.streams.each do |stream|
            next if stream.text.empty?

            if (line_length += stream.text.size) >= width
              remainder = width - line_length
              stream.text = truncate(stream.text, (remainder - 3))
            end

            processed_streams << stream
          end

          line.streams = processed_streams
        end
      end
    end

    def truncate(text, value)
      text.chomp.slice(0...value)
    end

    def lines
      interface.lines
    end

    def width
      interface.width
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.0.42 lib/vedeu/output/render_interface.rb