Sha256: 4f8d4b13a372b0016955a6ed09123750ecc38ac79a87ddd1b9cf676b09b3181c

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'vedeu/support/coercions'

module Vedeu

  # Provides a container for terminal escape sequences controlling the
  # foreground and background colours of a character or collection of
  # characters.
  #
  # @api private
  #
  class Colour

    include Vedeu::Coercions

    attr_reader :attributes,
      :background,
      :foreground

    # Returns a new instance of Colour.
    #
    # @param attributes [Hash]
    # @option attributes background [String]
    # @option attributes foreground [String]
    # @return [Colour]
    def initialize(attributes = {})
      @attributes = defaults.merge!(attributes)

      @background = Background.coerce(@attributes[:background])
      @foreground = Foreground.coerce(@attributes[:foreground])
    end

    # Converts the value into a Vedeu::Foreground.
    #
    # @param value [String]
    # @return [String]
    def foreground=(value)
      @foreground = Foreground.coerce(value)
    end

    # Converts the value into a Vedeu::Background.
    #
    # @param value [String]
    # @return [String]
    def background=(value)
      @background = Background.coerce(value)
    end

    # Returns both or either of the converted attributes into a single escape
    # sequence.
    #
    # @return [String]
    def to_s
      foreground.to_s + background.to_s
    end

    private

    # The default values for a new instance of this class.
    #
    # @return [Hash]
    def defaults
      {
        foreground: '',
        background: ''
      }
    end

  end # Colour

end # Vedeu

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vedeu-0.4.2 lib/vedeu/output/colour.rb