Sha256: 70d139a64c94c32b63911e6a6c1f92671ca092e960e0d850d83bae128c3a93f7

Contents?: true

Size: 1.93 KB

Versions: 34

Compression:

Stored size: 1.93 KB

Contents

module RSpec
  module Core
    module Formatters
      # ConsoleCodes provides helpers for formatting console output
      # with ANSI codes, e.g. color's and bold.
      module ConsoleCodes
        # @private
        VT100_CODES =
          {
            :black   => 30,
            :red     => 31,
            :green   => 32,
            :yellow  => 33,
            :blue    => 34,
            :magenta => 35,
            :cyan    => 36,
            :white   => 37,
            :bold    => 1,
          }
        # @private
        VT100_CODE_VALUES = VT100_CODES.invert

        module_function

        # 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
        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.key?(code_or_symbol)
            code_or_symbol
          else
            VT100_CODES.fetch(code_or_symbol) do
              console_code_for(:white)
            end
          end
        end

        # 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
        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

        # @private
        def configuration_color(code)
          RSpec.configuration.__send__(:"#{code}_color")
        end
      end
    end
  end
end

Version data entries

34 entries across 31 versions & 8 rubygems

Version Path
opal-rspec-0.8.0 rspec-core/upstream/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.8.0.alpha3 rspec-core/upstream/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.8.0.alpha2 rspec-core/upstream/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.8.0.alpha1 rspec-core/upstream/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.7.1 rspec-core/upstream/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.7.0 rspec-core/upstream/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.6.2 rspec-core/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.7.0.rc.2 rspec-core/upstream/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.6.1 rspec-core/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.6.0 rspec-core/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.6.0.beta1 rspec-core/lib/rspec/core/formatters/console_codes.rb
opal-connect-rspec-0.5.0 rspec-core/lib/rspec/core/formatters/console_codes.rb
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/rspec-core-3.1.7/lib/rspec/core/formatters/console_codes.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/rspec-core-3.1.1/lib/rspec/core/formatters/console_codes.rb
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/rspec-core-3.1.1/lib/rspec/core/formatters/console_codes.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/rspec-core-3.1.1/lib/rspec/core/formatters/console_codes.rb
opal-rspec-0.5.0 rspec-core/lib/rspec/core/formatters/console_codes.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/rspec-core-3.1.7/lib/rspec/core/formatters/console_codes.rb
logstash-input-beats-2.0.2 vendor/jruby/1.9/gems/logstash-codec-json-2.0.3/vendor/gems/rspec-core-3.1.7/lib/rspec/core/formatters/console_codes.rb
logstash-codec-json-2.0.3 vendor/gems/rspec-core-3.1.7/lib/rspec/core/formatters/console_codes.rb