Sha256: c751eff9f3a3f1b5e74416965b5b3bedef1ca1736956c25c2f21072de917a632

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 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 [Vedeu::Compressor]
    def initialize(output)
      @output = output
      @colour = ''
      @style  = ''
    end

    # @return [String]
    def render
      Array(output).flatten.map do |char|
        if char.is_a?(Vedeu::Char)
          out = ''
          out << char.position.to_s
          out << colour_for(char)
          out << style_for(char)
          out << char.value
          out

        else
          char.to_s

        end
      end.join
    end

    private

    attr_reader :output

    # @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

5 entries across 5 versions & 1 rubygems

Version Path
vedeu-0.4.18 lib/vedeu/output/compressor.rb
vedeu-0.4.17 lib/vedeu/output/compressor.rb
vedeu-0.4.16 lib/vedeu/output/compressor.rb
vedeu-0.4.15 lib/vedeu/output/compressor.rb
vedeu-0.4.14 lib/vedeu/output/compressor.rb