Sha256: 7e173e4b3a75f35ec66f4f49be0db1656561e2ee6ca9ab29d61cf7ed24c325cd

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'vedeu/models/line'
require 'vedeu/models/stream'

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

    def initialize(interface)
      @interface = interface
    end

    def render
      out = [interface.clear]
      processed_lines.each_with_index do |line, index|
        if index + 1 <= height
          out << interface.origin(index)
          out << line.to_s
        end
      end
      out.join
    end

    private

    attr_reader :interface

    def processed_lines
      return [] unless lines.any? { |line| line.streams.any? }

      lines.map do |line|
        line_length = 0
        streams     = line.streams.inject([]) do |processed, stream|
          next if stream.text.empty?

          if (line_length += stream.text.size) >= width
            remainder = width - line_length

            processed << Stream.new(text:   truncate(stream.text, remainder),
                                    style:  stream.style,
                                    colour: stream.colour)

          else
            processed << stream

          end
        end

        Line.new(streams: streams,
                 style:   line.style,
                 colour:  line.colour)
      end
    end

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

    def lines
      interface.lines
    end

    def height
      interface.height
    end

    def width
      interface.width
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.1.8 lib/vedeu/output/render.rb