Sha256: 99e409db822da17bfe423ecd547143d4f0d926a29ca2dc65a17896401a57a8c6

Contents?: true

Size: 1.92 KB

Versions: 6511

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require 'pp'
require 'English'

class Pry
  # PP subclass for streaming inspect output in color.
  class ColorPrinter < ::PP
    Pry::SyntaxHighlighter.overwrite_coderay_comment_token!

    def self.default(_output, value, pry_instance)
      pry_instance.pager.open do |pager|
        pager.print pry_instance.config.output_prefix
        pp(value, pager, pry_instance.output.width - 1)
      end
    end

    def self.pp(obj, output = $DEFAULT_OUTPUT, max_width = 79)
      queue = ColorPrinter.new(output, max_width, "\n")
      queue.guard_inspect_key { queue.pp(obj) }
      queue.flush
      output << "\n"
    end

    def pp(object)
      return super unless object.is_a?(String)

      # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
      # Strings prettier
      text(object.inspect)
    rescue StandardError => exception
      raise if exception.is_a?(Pry::Pager::StopPaging)

      text(highlight_object_literal(inspect_object(object)))
    end

    def text(str, max_width = str.length)
      if str.include?("\e[")
        super("#{str}\e[0m", max_width)
      elsif str.start_with?('#<') || %w[= >].include?(str)
        super(highlight_object_literal(str), max_width)
      else
        super(SyntaxHighlighter.highlight(str), max_width)
      end
    end

    private

    def highlight_object_literal(object_literal)
      code = Pry::SyntaxHighlighter.keyword_token_color
      obj_color = code.start_with?("\e") ? code : "\e[0m\e[0;#{code}m"
      "#{obj_color}#{object_literal}\e[0m"
    end

    def inspect_object(object)
      object.inspect
    rescue StandardError
      # Read the class name off of the singleton class to provide a default
      # inspect.
      singleton = class << object; self; end
      ancestors = Pry::Method.safe_send(singleton, :ancestors)
      klass = ancestors.find { |k| k != singleton }
      "#<#{klass}:0x#{object.__id__.to_s(16)}>"
    end
  end
end

Version data entries

6,511 entries across 6,507 versions & 30 rubygems

Version Path
ory-client-1.15.13 vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/color_printer.rb
affixapi-1.1.102 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
mux_ruby-3.20.0 vendor/bundle/ruby/3.2.0/gems/pry-0.13.1/lib/pry/color_printer.rb
cybrid_api_bank_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_organization_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_id_ruby-0.123.12 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_bank_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_organization_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_id_ruby-0.123.11 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_id_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_bank_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_organization_ruby-0.123.10 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_organization_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_id_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_bank_ruby-0.123.7 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
ory-client-1.15.12 vendor/bundle/ruby/3.1.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_bank_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_id_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_organization_ruby-0.123.4 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb
cybrid_api_bank_ruby-0.123.3 vendor/bundle/ruby/3.3.0/gems/pry-0.14.2/lib/pry/color_printer.rb