Sha256: 4083031acdca2e03d0add71d0b3513b6e400daa04d7b146552cc95de0ac51df9

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 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.escape_sequence(@attributes[:background])
      @foreground = Foreground.escape_sequence(@attributes[:foreground])
    end

    # Converts the value into a terminal escape sequence.
    #
    # @param value [String]
    # @return [String]
    def foreground=(value)
      @foreground = Foreground.escape_sequence(value)
    end

    # Converts the value into a terminal escape sequence.
    #
    # @param value [String]
    # @return [String]
    def background=(value)
      @background = Background.escape_sequence(value)
    end

    # Returns both or either of the converted attributes into a single escape
    # sequence.
    #
    # @return [String]
    def to_s
      foreground + background
    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

5 entries across 5 versions & 1 rubygems

Version Path
vedeu-0.3.4 lib/vedeu/presentation/colour.rb
vedeu-0.3.3 lib/vedeu/presentation/colour.rb
vedeu-0.3.2 lib/vedeu/presentation/colour.rb
vedeu-0.3.1 lib/vedeu/presentation/colour.rb
vedeu-0.3.0 lib/vedeu/presentation/colour.rb