Class: Pry::ColorPrinter

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

Constant Summary collapse

OBJ_COLOR =
begin
  code = CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
  if code.start_with? "\e"
    code
  else
    "\e[0m\e[0;#{code}m"
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.pp(obj, out = $>, width = 79, newline = "\n") ⇒ Object



15
16
17
18
19
20
# File 'lib/pry/color_printer.rb', line 15

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

Instance Method Details

#highlight_object_literal(object_literal) ⇒ Object (private)



60
61
62
# File 'lib/pry/color_printer.rb', line 60

def highlight_object_literal(object_literal)
  "#{OBJ_COLOR}#{object_literal}\e[0m"
end

#pp(obj) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pry/color_printer.rb', line 33

def pp(obj)
  if String === obj
    # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
    # Strings prettier
    text(obj.inspect)
  else
    super
  end
rescue => e
  raise if e.is_a? Pry::Pager::StopPaging
  begin
    str = obj.inspect
  rescue Exception 
    # Read the class name off of the singleton class to provide a default
    # inspect.
    singleton = class << obj; self; end
    ancestors = Pry::Method.safe_send(singleton, :ancestors)
    klass  = ancestors.reject { |k| k == singleton }.first
    obj_id = obj.__id__.to_s(16) rescue 0
    str    = "#<#{klass}:0x#{obj_id}>"
  end

  text highlight_object_literal(str)
end

#text(str, width = str.length) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/pry/color_printer.rb', line 22

def text(str, width = str.length)
  # Don't recolorize output with color [Issue #751]
  if str.include?("\e[")
    super "#{str}\e[0m", width
  elsif str.start_with?('#<') || str == '=' || str == '>'
    super highlight_object_literal(str), width
  else
    super CodeRay.scan(str, :ruby).term, width
  end
end