123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Core::Formatters::ConsoleCodes

Relationships & Source Files
Defined in: rspec-core/lib/rspec/core/formatters/console_codes.rb

Overview

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

Constant Summary

Class Method Summary

Class Method Details

.config_colors_to_methods (mod_func)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/formatters/console_codes.rb', line 34

def config_colors_to_methods
  @config_colors_to_methods ||=
    Configuration.instance_methods.grep(/_color\z/).inject({}) do |hash, method|
      hash[method.to_s.sub(/_color\z/, '').to_sym] = method
      hash
    end
end

.console_code_for(code_or_symbol) ⇒ Fixnum (mod_func)

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

Parameters:

  • code_or_symbol (Symbol, Fixnum)

    Symbol or code to check

Returns:

  • (Fixnum)

    a console code

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/formatters/console_codes.rb', line 47

def console_code_for(code_or_symbol)
  if (config_method = config_colors_to_methods[code_or_symbol])
    console_code_for RSpec.configuration.__send__(config_method)
  elsif VT100_CODE_VALUES.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) ⇒ String (mod_func)

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.

Parameters:

  • text (String)

    the text to wrap

  • code_or_symbol (Symbol, Fixnum)

    the desired control code

Returns:

  • (String)

    the wrapped text

[ GitHub ]

  
# File 'rspec-core/lib/rspec/core/formatters/console_codes.rb', line 66

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