Sha256: 8cc1dda28591e07b5c229ddd120c600a4149d148a8eddefda448f316a3fa86f2

Contents?: true

Size: 1.08 KB

Versions: 9

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true
require 'pp'
require_relative '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 ''
      when ',', '=>', '[', ']', '{', '}', '..', '...', /\A@\w+\z/
        super(str, width)
      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

9 entries across 9 versions & 2 rubygems

Version Path
irb-1.6.3 lib/irb/color_printer.rb
irb-1.6.2 lib/irb/color_printer.rb
irb-1.6.1 lib/irb/color_printer.rb
irb-1.6.0 lib/irb/color_printer.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/irb-1.4.2/lib/irb/color_printer.rb
irb-1.5.1 lib/irb/color_printer.rb
irb-1.5.0 lib/irb/color_printer.rb
irb-1.4.3 lib/irb/color_printer.rb
irb-1.4.2 lib/irb/color_printer.rb