summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyrylo Silin <silin@kyrylo.org>2019-03-30 01:23:54 +0200
committerKyrylo Silin <silin@kyrylo.org>2019-03-30 01:44:33 +0200
commitd1075419519ea6cea077394c8ddd474b4f4f7c56 (patch)
tree07661a7f07d1ed203390c164e69ccde0b9132469
parentd3f9913d1a57c01856899507494c265176da5f45 (diff)
downloadpry-d1075419519ea6cea077394c8ddd474b4f4f7c56.tar.gz
color_printer: cosmetic refactoring
Changes: * method extractions * indentation fixes * brackets for methods * clearer variable names
-rw-r--r--lib/pry/color_printer.rb72
1 files changed, 32 insertions, 40 deletions
diff --git a/lib/pry/color_printer.rb b/lib/pry/color_printer.rb
index f1e0c01d..b339351a 100644
--- a/lib/pry/color_printer.rb
+++ b/lib/pry/color_printer.rb
@@ -6,52 +6,33 @@ class Pry
class ColorPrinter < ::PP
Pry::SyntaxHighlighter.overwrite_coderay_comment_token!
- def self.pp(obj, out = $DEFAULT_OUTPUT, width = 79, newline = "\n")
- q = ColorPrinter.new(out, width, newline)
- q.guard_inspect_key { q.pp obj }
- q.flush
- out << "\n"
+ def self.pp(obj, output = $DEFAULT_OUTPUT, max_width = 79)
+ queue = ColorPrinter.new(output, max_width, "\n")
+ queue.guard_inspect_key { queue.pp(obj) }
+ queue.flush
+ output << "\n"
end
- 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(SyntaxHighlighter.highlight(str), width)
- end
+ def pp(object)
+ return super unless object.is_a?(String)
+
+ # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
+ # Strings prettier
+ text(object.inspect)
+ rescue StandardError => exception
+ raise if exception.is_a?(Pry::Pager::StopPaging)
+
+ text(highlight_object_literal(inspect_object(object)))
end
- def pp(obj)
- if obj.is_a?(String)
- # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
- # Strings prettier
- text(obj.inspect)
+ def text(str, max_width = str.length)
+ if str.include?("\e[")
+ super("#{str}\e[0m", max_width)
+ elsif str.start_with?('#<') || %w[= >].include?(str)
+ super(highlight_object_literal(str), max_width)
else
- super
+ super(SyntaxHighlighter.highlight(str), max_width)
end
- rescue StandardError => e
- raise if e.is_a? Pry::Pager::StopPaging
-
- begin
- str = obj.inspect
- rescue StandardError
- # 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 = begin
- obj.__id__.to_s(16)
- rescue StandardError
- 0
- end
- str = "#<#{klass}:0x#{obj_id}>"
- end
-
- text highlight_object_literal(str)
end
private
@@ -61,5 +42,16 @@ class Pry
obj_color = code.start_with?("\e") ? code : "\e[0m\e[0;#{code}m"
"#{obj_color}#{object_literal}\e[0m"
end
+
+ def inspect_object(object)
+ object.inspect
+ rescue StandardError
+ # Read the class name off of the singleton class to provide a default
+ # inspect.
+ singleton = class << object; self; end
+ ancestors = Pry::Method.safe_send(singleton, :ancestors)
+ klass = ancestors.find { |k| k != singleton }
+ "#<#{klass}:0x#{object.__id__.to_s(16)}>"
+ end
end
end