Sha256: ef6799c8c0d555e5afb2cfea1c9bf43df5483f539e6dc502a469f1eb62bccd2b

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

module Vedeu

  # Converts a grid of {Vedeu::Char} objects into a stream of escape sequences
  # and content suitable for a terminal, and writes them to a file in the /tmp
  # directory.
  #
  class FileRenderer

    # @param output [Array<Array<Vedeu::Char>>]
    # @param options [Hash]
    # @return [String]
    def self.render(output, options = {})
      new(output, options).render
    end

    # Returns a new instance of Vedeu::FileRenderer.
    #
    # @param output [Array<Array<Vedeu::Char>>]
    # @param options [Hash]
    # @return [Vedeu::FileRenderer]
    def initialize(output, options = {})
      @output  = output
      @options = options
    end

    # @return [String]
    def render
      File.open("/tmp/#{filename}", 'w') { |f| f.write(parsed) }

      parsed
    end

    private

    # @!attribute [r] output
    # @return [Array<Array<Vedeu::Char>>]
    attr_reader :output

    # @return [String]
    def filename
      if timestamp?
        "out_#{timestamp}"

      else
        'out'

      end
    end

    # @return [String]
    def parsed
      @parsed ||= Vedeu::Compressor.new(output).render
    end

    # @return [Float]
    def timestamp
      Time.now.to_f
    end

    # @return [Boolean]
    def timestamp?
      return true if options[:timestamp]

      false
    end

    # @return [Hash]
    def options
      defaults.merge!(@options)
    end

    # @return [Hash]
    def defaults
      {
        timestamp: false,
      }
    end

  end # FileRenderer

end # Vedeu

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.4.17 lib/vedeu/output/renderers/file_renderer.rb
vedeu-0.4.16 lib/vedeu/output/renderers/file_renderer.rb