Sha256: 39d9eee2f62ff7c9db89f986aa19fb365f897eaed2db7ca3e5fac33ccb8244cb

Contents?: true

Size: 1.89 KB

Versions: 13

Compression:

Stored size: 1.89 KB

Contents

module Vedeu

  # Converts the style value or value collection into a terminal escape
  # sequence. Unrecognised values are discarded- an empty string is returned.
  #
  # Vedeu has a range of symbol styles which are compatible with most terminals
  # which are ANSI compatible. Like colours, they can be defined in either
  # interfaces, for specific lines or within streams.
  # Styles are applied as encountered.
  #
  # @see Vedeu::Esc
  #
  class Style

    include Vedeu::Common

    # @!attribute [rw] value
    # @return [String|Symbol]
    attr_accessor :value

    # Produces new objects of the correct class from the value, ignores objects
    # that have already been coerced.
    #
    # @param value [Object|NilClass]
    # @return [Object]
    def self.coerce(value)
      if value.nil?
        new

      elsif value.is_a?(self)
        value

      else
        new(value)

      end
    end

    # Return a new instance of Vedeu::Style.
    #
    # @param value [Array<String|Symbol>|String|Symbol]
    #   The style value or a collection of values.
    # @return [Vedeu::Style]
    def initialize(value = nil)
      @value = value
    end

    # Return an attributes hash for this class.
    #
    # @return [Hash<Symbol => Array<String|Symbol>|String|Symbol>]
    def attributes
      {
        style: value,
      }
    end

    # An object is equal when its values are the same.
    #
    # @param other [Vedeu::Views::Char]
    # @return [Boolean]
    def eql?(other)
      self.class == other.class && value == other.value
    end
    alias_method :==, :eql?

    # Return the terminal escape sequences after converting the style or styles.
    #
    # @return [String]
    def to_s
      return '' unless present?(value)

      @sequences ||= Array(value).flatten.map { |v| Vedeu::Esc.string(v) }.join
    end
    alias_method :escape_sequences, :to_s
    alias_method :to_str, :to_s

  end # Style

end # Vedeu

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
vedeu-0.6.3 lib/vedeu/output/style.rb
vedeu-0.6.2 lib/vedeu/output/style.rb
vedeu-0.6.1 lib/vedeu/output/style.rb
vedeu-0.6.0 lib/vedeu/output/style.rb
vedeu-0.5.13 lib/vedeu/output/style.rb
vedeu-0.5.12 lib/vedeu/output/style.rb
vedeu-0.5.11 lib/vedeu/output/style.rb
vedeu-0.5.10 lib/vedeu/output/style.rb
vedeu-0.5.9 lib/vedeu/output/style.rb
vedeu-0.5.8 lib/vedeu/output/style.rb
vedeu-0.5.7 lib/vedeu/output/style.rb
vedeu-0.5.6 lib/vedeu/output/style.rb
vedeu-0.5.5 lib/vedeu/output/style.rb