Sha256: dfbec6b6c6ae225de6d984c77749ba08e98031f2f617349d7454abafc2ae1f0c

Contents?: true

Size: 1.75 KB

Versions: 18

Compression:

Stored size: 1.75 KB

Contents

module Vedeu

  # During the conversion of a Vedeu::Views::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::Views::Char>>]
    # @return [String]
    def self.render(output)
      new(output).render
    end

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

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

      else
        uncompress

      end
    end

    protected

    # @!attribute [r] output
    # @return [Array<Array<Vedeu::Views::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 { |char| out << char.to_s }
      out
    end

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

      @colour = char.colour
      @colour.to_s
    end

    # @param char [Vedeu::Views::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

18 entries across 18 versions & 1 rubygems

Version Path
vedeu-0.6.8 lib/vedeu/output/compressor.rb
vedeu-0.6.7 lib/vedeu/output/compressor.rb
vedeu-0.6.6 lib/vedeu/output/compressor.rb
vedeu-0.6.5 lib/vedeu/output/compressor.rb
vedeu-0.6.4 lib/vedeu/output/compressor.rb
vedeu-0.6.3 lib/vedeu/output/compressor.rb
vedeu-0.6.2 lib/vedeu/output/compressor.rb
vedeu-0.6.1 lib/vedeu/output/compressor.rb
vedeu-0.6.0 lib/vedeu/output/compressor.rb
vedeu-0.5.13 lib/vedeu/output/compressor.rb
vedeu-0.5.12 lib/vedeu/output/compressor.rb
vedeu-0.5.11 lib/vedeu/output/compressor.rb
vedeu-0.5.10 lib/vedeu/output/compressor.rb
vedeu-0.5.9 lib/vedeu/output/compressor.rb
vedeu-0.5.8 lib/vedeu/output/compressor.rb
vedeu-0.5.7 lib/vedeu/output/compressor.rb
vedeu-0.5.6 lib/vedeu/output/compressor.rb
vedeu-0.5.5 lib/vedeu/output/compressor.rb