Sha256: d75b7af204b6bfbb94de281070f2009a46346cba87b6aa3115e458d23c2b5e6c

Contents?: true

Size: 1.86 KB

Versions: 92

Compression:

Stored size: 1.86 KB

Contents

module RSpec
  module Core
    class Source
      # @private
      # Provides terminal syntax highlighting of code snippets
      # when coderay is available.
      class SyntaxHighlighter
        def initialize(configuration)
          @configuration = configuration
        end

        def highlight(lines)
          implementation.highlight_syntax(lines)
        end

      private

        if RSpec::Support::OS.windows?
          # :nocov:
          def implementation
            WindowsImplementation
          end
          # :nocov:
        else
          def implementation
            return color_enabled_implementation if @configuration.color_enabled?
            NoSyntaxHighlightingImplementation
          end
        end

        def color_enabled_implementation
          @color_enabled_implementation ||= begin
            require 'coderay'
            CodeRayImplementation
          rescue LoadError
            NoSyntaxHighlightingImplementation
          end
        end

        # @private
        module CodeRayImplementation
          RESET_CODE = "\e[0m"

          def self.highlight_syntax(lines)
            highlighted = begin
              CodeRay.encode(lines.join("\n"), :ruby, :terminal)
            rescue Support::AllExceptionsExceptOnesWeMustNotRescue
              return lines
            end

            highlighted.split("\n").map do |line|
              line.sub(/\S/) { |char| char.insert(0, RESET_CODE) }
            end
          end
        end

        # @private
        module NoSyntaxHighlightingImplementation
          def self.highlight_syntax(lines)
            lines
          end
        end

        # @private
        # Not sure why, but our code above (and/or coderay itself) does not work
        # on Windows, so we disable the feature on Windows.
        WindowsImplementation = NoSyntaxHighlightingImplementation
      end
    end
  end
end

Version data entries

92 entries across 83 versions & 19 rubygems

Version Path
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.6.2 vendor/bundle/ruby/2.6.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.6.1 vendor/bundle/ruby/2.6.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.6.0 vendor/bundle/ruby/2.6.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.6.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.6.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/syntax_highlighter.rb