Sha256: 68e0526b999556091f10539fa788a0099fd85b73f8ca80cac255fec78db6fe6d

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

module Vedeu

  # During the conversion of a Vedeu::Char object into a string of escape
  # sequences, this class removes multiple occurences of the same escape
  # sequence, resulting in a smaller payload being sent to the renderer.
  #
  class Compressor

    # @param output [Array<Array<Vedeu::Char>>]
    # @return [String]
    def self.render(output)
      new(output).render
    end

    # Returns a new instance of Vedeu::Compressor.
    #
    # @param output [Array<Array<Vedeu::Char>>]
    # @return [Vedeu::Compressor]
    def initialize(output)
      @output = output
      @colour = ''
      @style  = ''
    end

    # @note Takes approximately ~70ms for 2100 chars. (2015-05-24)
    # @return [String]
    def render
      if Vedeu::Configuration.compression?
        compress

      else
        uncompress

      end
    end

    protected

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

    private

    # @return [String]
    def compress
      out = ''
      Array(output).flatten.each do |char|
        out << char.position.to_s
        out << colour_for(char)
        out << style_for(char)
        out << char.value
      end
      out
    end

    # @return [String]
    def uncompress
      out = ''
      Array(output).flatten.each do |char|
        out << char.position.to_s
        out << char.colour.to_s
        out << char.style.to_s
        out << char.value
      end
      out
    end

    # @param char [Vedeu::Char]
    # @return [String]
    def colour_for(char)
      return '' if char.colour == @colour

      @colour = char.colour
      @colour.to_s
    end

    # @param char [Vedeu::Char]
    # @return [String]
    def style_for(char)
      return '' if char.style == @style

      @style = char.style
      @style.to_s
    end

  end # Compressor

end # Vedeu

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
vedeu-0.4.60 lib/vedeu/output/compressor.rb
vedeu-0.4.59 lib/vedeu/output/compressor.rb
vedeu-0.4.58 lib/vedeu/output/compressor.rb
vedeu-0.4.57 lib/vedeu/output/compressor.rb