module RSpec::Core::Formatters::ConsoleCodes

ConsoleCodes provides helpers for formatting console output with ANSI codes, e.g. color's and bold.

Constants

VT100_CODES

@private

VT100_CODE_VALUES

@private

Public Instance Methods

configuration_color(code) click to toggle source

@private

# File lib/rspec/core/formatters/console_codes.rb, line 58
def configuration_color(code)
  RSpec.configuration.__send__(:"#{code}_color")
end
console_code_for(code_or_symbol) click to toggle source

Fetches the correct code for the supplied symbol, or checks that a code is valid. Defaults to white (37).

@param code_or_symbol [Symbol, Fixnum] Symbol or code to check @return [Fixnum] a console code

# File lib/rspec/core/formatters/console_codes.rb, line 30
def console_code_for(code_or_symbol)
  if RSpec.configuration.respond_to?(:"#{code_or_symbol}_color")
    console_code_for configuration_color(code_or_symbol)
  elsif VT100_CODE_VALUES.has_key?(code_or_symbol)
    code_or_symbol
  else
    VT100_CODES.fetch(code_or_symbol) do
      console_code_for(:white)
    end
  end
end
wrap(text, code_or_symbol) click to toggle source

Wraps a piece of text in ANSI codes with the supplied code. Will only apply the control code if `RSpec.configuration.color_enabled?` returns true.

@param text [String] the text to wrap @param code_or_symbol [Symbol, Fixnum] the desired control code @return [String] the wrapped text

# File lib/rspec/core/formatters/console_codes.rb, line 49
def wrap(text, code_or_symbol)
  if RSpec.configuration.color_enabled?
    "\e[#{console_code_for(code_or_symbol)}m#{text}\e[0m"
  else
    text
  end
end