Sha256: fbd29d04fdecd9cc8c1b14444bb115c672c8ff0da9f12095b0577d194504af29

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

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

    attr_reader :attributes

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

    # Converts the `:foreground` attribute into a terminal escape sequence.
    #
    # @return [String]
    def foreground
      @foreground ||= Foreground.escape_sequence(attributes[:foreground])
    end

    # Converts the `:background` attribute into a terminal escape sequence.
    #
    # @return [String]
    def background
      @background ||= Background.escape_sequence(attributes[:background])
    end

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

    private

    # @api private
    # @return [Hash]
    def defaults
      {
        foreground: '',
        background: ''
      }
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.2.2 lib/vedeu/models/colour.rb
vedeu-0.1.19 lib/vedeu/models/colour.rb