Sha256: 86c9ff933a29997414afbf3bfd0dd6c10fe2ffd90f4916109c03664498bae6bd

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 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.
  #
  # @api private
  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

    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

    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

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.4.52 lib/vedeu/output/compressor.rb