Sha256: ec014e16da985967bf520cc31741084f7c36b56d0497658f6dd76d1fbcfb3359

Contents?: true

Size: 1.5 KB

Versions: 3

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

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.4.1 lib/vedeu/output/colour.rb
vedeu-0.4.0 lib/vedeu/output/colour.rb
vedeu-0.3.5 lib/vedeu/presentation/colour.rb