Class: IRB::ColorPrinter

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out, width, colorize: true) ⇒ ColorPrinter

Returns a new instance of ColorPrinter.

[View source]

24
25
26
27
28
# File 'lib/irb/color_printer.rb', line 24

def initialize(out, width, colorize: true)
  @colorize = colorize

  super(out, width)
end

Class Method Details

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

[View source]

8
9
10
11
12
13
# File 'lib/irb/color_printer.rb', line 8

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

Instance Method Details

#pp(obj) ⇒ Object

[View source]

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

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

#text(str, width = nil) ⇒ Object

[View source]

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

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(@colorize ? Color.colorize(str, [:GREEN]) : str, width)
  else
    super(@colorize ? Color.colorize_code(str, ignore_error: true) : str, width)
  end
end