Sha256: 349f888215c85be66769991b7e1ff45b43a1ab28843b2412ce41d2ad37aceee8

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

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

    def initialize(interface)
      @interface = interface
    end

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

    private

    attr_reader :interface

    # The client application may have created a line that us too long for the
    # interface. This code tries to truncate streams whilst preserving styles
    # and colours.
    def processed_lines
      return [] unless lines.any? { |line| line.streams.any? }

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

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

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

            else
              processed << stream

            end
          end

          Line.new({
            colour:  line.colour.attributes,
            streams: processed,
            style:   line.style,
          })

        else
          line

        end
      end
    end

    def exceeds_width?(line)
      content = line.streams.map(&:text).join
      content.size > width
    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

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.1.15 lib/vedeu/output/render.rb
vedeu-0.1.14 lib/vedeu/output/render.rb