Class: IRB::ColorPrinter

Inherits:
PP show all
Defined in:
lib/irb/color_printer.rb

Constant Summary collapse

METHOD_RESPOND_TO =
Object.instance_method(:respond_to?)
METHOD_INSPECT =
Object.instance_method(:inspect)

Constants inherited from PP

PP::VERSION

Constants inherited from PrettyPrint

PrettyPrint::VERSION

Instance Attribute Summary

Attributes inherited from PrettyPrint

#genspace, #group_queue, #indent, #maxwidth, #newline, #output

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PP

mcall, singleline_pp, width_for

Methods included from PP::PPMethods

#check_inspect_key, #comma_breakable, #guard_inspect_key, #object_address_group, #object_group, #pop_inspect_key, #pp_hash, #pp_object, #push_inspect_key, #seplist

Methods inherited from PrettyPrint

#break_outmost_groups, #breakable, #current_group, #fill_breakable, #flush, format, #group, #group_sub, #initialize, #nest, singleline_format

Constructor Details

This class inherits a constructor from PrettyPrint

Class Method Details

.pp(obj, out = $>, width = screen_width) ⇒ Object



11
12
13
14
15
16
# File 'lib/irb/color_printer.rb', line 11

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

Instance Method Details

#pp(obj) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/irb/color_printer.rb', line 27

def pp(obj)
  if String === obj
    # Avoid calling Ruby 2.4+ String#pretty_print that splits a string by "\n"
    text(obj.inspect)
  elsif !METHOD_RESPOND_TO.bind(obj).call(:inspect)
    text(METHOD_INSPECT.bind(obj).call)
  else
    super
  end
end

#text(str, width = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/irb/color_printer.rb', line 38

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