Sha256: 986baf2bff8cccd771619cb523fc0c39cfbf08290e094d4fecba591b862bd219

Contents?: true

Size: 995 Bytes

Versions: 18

Compression:

Stored size: 995 Bytes

Contents

# frozen_string_literal: true
require 'pp'
require 'irb/color'

module IRB
  class ColorPrinter < ::PP
    class << self
      def pp(obj, out = $>, width = screen_width)
        q = ColorPrinter.new(out, width)
        q.guard_inspect_key {q.pp obj}
        q.flush
        out << "\n"
      end

      private

      def screen_width
        Reline.get_screen_size.last
      rescue Errno::EINVAL # in `winsize': Invalid argument - <STDIN>
        79
      end
    end

    def pp(obj)
      if obj.is_a?(String)
        # Avoid calling Ruby 2.4+ String#pretty_print that splits a string by "\n"
        text(obj.inspect)
      else
        super
      end
    end

    def text(str, width = nil)
      unless str.is_a?(String)
        str = str.inspect
      end
      width ||= str.length

      case str
      when /\A#</, '=', '>'
        super(Color.colorize(str, [:GREEN]), width)
      else
        super(Color.colorize_code(str, ignore_error: true), width)
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/irb-1.4.1/lib/irb/color_printer.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/irb-1.4.1/lib/irb/color_printer.rb
irb-1.4.1 lib/irb/color_printer.rb
irb-1.4.0 lib/irb/color_printer.rb
irb-1.3.8.pre.11 lib/irb/color_printer.rb
irb-1.3.8.pre.10 lib/irb/color_printer.rb
irb-1.3.8.pre.9 lib/irb/color_printer.rb
irb-1.3.8.pre.8 lib/irb/color_printer.rb
irb-1.3.8.pre.7 lib/irb/color_printer.rb
irb-1.3.8.pre.6 lib/irb/color_printer.rb
irb-1.3.8.pre.5 lib/irb/color_printer.rb
irb-1.3.8.pre.4 lib/irb/color_printer.rb
irb-1.3.8.pre.3 lib/irb/color_printer.rb
irb-1.3.8.pre.2 lib/irb/color_printer.rb
irb-1.3.8.pre.1 lib/irb/color_printer.rb
irb-1.3.7 lib/irb/color_printer.rb
irb-1.3.6 lib/irb/color_printer.rb
irb-1.3.5 lib/irb/color_printer.rb