Sha256: 7c57b1de4169b85efd7dc05861c26cad3b1f8c55400a0a169c5b79c18722bac4

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

# PP subclass for streaming inspect output in color.
class Pry
  class ColorPrinter < ::PP
    OBJ_COLOR = begin
      code = CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
      if code.start_with? "\e"
        code
      else
        "\e[0m\e[0;#{code}m"
      end
    end

    CodeRay::Encoders::Terminal::TOKEN_COLORS[:comment][:self] = "\e[1;34m"

    def self.pp(obj, out = $>, width = 79, newline = "\n")
      q = ColorPrinter.new(out, width, newline)
      q.guard_inspect_key { q.pp obj }
      q.flush
      out << "\n"
    end

    def text(str, width = str.length)
      # Don't recolorize output with color [Issue #751]
      if str.include?("\e[")
        super "#{str}\e[0m", width
      elsif str.start_with?('#<') || str == '=' || str == '>'
        super highlight_object_literal(str), width
      else
        super CodeRay.scan(str, :ruby).term, width
      end
    end

    def pp(obj)
      if String === obj
        # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
        # Strings prettier
        text(obj.inspect)
      else
        super
      end
    rescue => e
      raise if e.is_a? Pry::Pager::StopPaging

      begin
        str = obj.inspect
      rescue Exception 
        # Read the class name off of the singleton class to provide a default
        # inspect.
        singleton = class << obj; self; end
        ancestors = Pry::Method.safe_send(singleton, :ancestors)
        klass  = ancestors.reject { |k| k == singleton }.first
        obj_id = obj.__id__.to_s(16) rescue 0
        str    = "#<#{klass}:0x#{obj_id}>"
      end

      text highlight_object_literal(str)
    end

    private

    def highlight_object_literal(object_literal)
      "#{OBJ_COLOR}#{object_literal}\e[0m"
    end
  end
end

Version data entries

15 entries across 15 versions & 7 rubygems

Version Path
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/pry-0.12.2/lib/pry/color_printer.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/pry-0.12.2/lib/pry/color_printer.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/pry-0.12.2/lib/pry/color_printer.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/pry-0.12.2/lib/pry/color_printer.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/pry-0.12.2/lib/pry/color_printer.rb
chess_engine-0.0.2 vendor/bundle/gems/pry-0.12.2/lib/pry/color_printer.rb
chess_engine-0.0.1 vendor/bundle/gems/pry-0.12.2/lib/pry/color_printer.rb
alimentos-alu0100945645-0.1.0 vendor/bundle/ruby/2.3.0/gems/pry-0.12.2/lib/pry/color_printer.rb
alimentos-alu0100945645-1.0.0 vendor/bundle/ruby/2.3.0/gems/pry-0.12.2/lib/pry/color_printer.rb
pry-0.12.2-java lib/pry/color_printer.rb
pry-0.12.2 lib/pry/color_printer.rb
pry-0.12.1 lib/pry/color_printer.rb
pry-0.12.1-java lib/pry/color_printer.rb
pry-0.12.0 lib/pry/color_printer.rb
pry-0.12.0-java lib/pry/color_printer.rb