pry 0.10.1 → 0.14.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +412 -11
  3. data/LICENSE +1 -1
  4. data/README.md +352 -306
  5. data/bin/pry +4 -7
  6. data/lib/pry/basic_object.rb +10 -0
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +83 -96
  10. data/lib/pry/code/code_file.rb +37 -26
  11. data/lib/pry/code/code_range.rb +7 -5
  12. data/lib/pry/code/loc.rb +26 -13
  13. data/lib/pry/code.rb +50 -31
  14. data/lib/pry/code_object.rb +53 -28
  15. data/lib/pry/color_printer.rb +46 -35
  16. data/lib/pry/command.rb +197 -369
  17. data/lib/pry/command_set.rb +89 -114
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -82
  20. data/lib/pry/commands/bang.rb +18 -14
  21. data/lib/pry/commands/bang_pry.rb +15 -11
  22. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  23. data/lib/pry/commands/cat/exception_formatter.rb +85 -72
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -46
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +62 -54
  27. data/lib/pry/commands/cd.rb +40 -35
  28. data/lib/pry/commands/change_inspector.rb +29 -22
  29. data/lib/pry/commands/change_prompt.rb +48 -23
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -131
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +23 -34
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  36. data/lib/pry/commands/edit.rb +185 -157
  37. data/lib/pry/commands/exit.rb +40 -35
  38. data/lib/pry/commands/exit_all.rb +24 -20
  39. data/lib/pry/commands/exit_program.rb +20 -16
  40. data/lib/pry/commands/find_method.rb +168 -162
  41. data/lib/pry/commands/fix_indent.rb +16 -12
  42. data/lib/pry/commands/help.rb +140 -133
  43. data/lib/pry/commands/hist.rb +151 -149
  44. data/lib/pry/commands/import_set.rb +20 -15
  45. data/lib/pry/commands/jump_to.rb +25 -21
  46. data/lib/pry/commands/list_inspectors.rb +35 -28
  47. data/lib/pry/commands/ls/constants.rb +59 -31
  48. data/lib/pry/commands/ls/formatter.rb +42 -36
  49. data/lib/pry/commands/ls/globals.rb +38 -36
  50. data/lib/pry/commands/ls/grep.rb +17 -15
  51. data/lib/pry/commands/ls/instance_vars.rb +29 -28
  52. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  53. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  54. data/lib/pry/commands/ls/local_names.rb +26 -24
  55. data/lib/pry/commands/ls/local_vars.rb +38 -30
  56. data/lib/pry/commands/ls/ls_entity.rb +47 -52
  57. data/lib/pry/commands/ls/methods.rb +49 -51
  58. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  59. data/lib/pry/commands/ls/self_methods.rb +23 -21
  60. data/lib/pry/commands/ls.rb +124 -103
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +92 -82
  63. data/lib/pry/commands/pry_backtrace.rb +22 -17
  64. data/lib/pry/commands/pry_version.rb +15 -11
  65. data/lib/pry/commands/raise_up.rb +33 -27
  66. data/lib/pry/commands/reload_code.rb +60 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -42
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +56 -29
  71. data/lib/pry/commands/shell_mode.rb +22 -18
  72. data/lib/pry/commands/show_doc.rb +80 -70
  73. data/lib/pry/commands/show_info.rb +194 -155
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +110 -42
  76. data/lib/pry/commands/stat.rb +35 -31
  77. data/lib/pry/commands/switch_to.rb +21 -15
  78. data/lib/pry/commands/toggle_color.rb +20 -16
  79. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  80. data/lib/pry/commands/watch_expression.rb +89 -84
  81. data/lib/pry/commands/whereami.rb +156 -141
  82. data/lib/pry/commands/wtf.rb +78 -40
  83. data/lib/pry/config/attributable.rb +22 -0
  84. data/lib/pry/config/lazy_value.rb +29 -0
  85. data/lib/pry/config/memoized_value.rb +34 -0
  86. data/lib/pry/config/value.rb +24 -0
  87. data/lib/pry/config.rb +313 -20
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +22 -9
  90. data/lib/pry/editor.rb +53 -33
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +13 -17
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +20 -62
  96. data/lib/pry/helpers/command_helpers.rb +52 -62
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -12
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  99. data/lib/pry/helpers/platform.rb +60 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +96 -85
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +81 -55
  104. data/lib/pry/hooks.rb +60 -110
  105. data/lib/pry/indent.rb +72 -66
  106. data/lib/pry/input_completer.rb +199 -158
  107. data/lib/pry/input_lock.rb +7 -10
  108. data/lib/pry/inspector.rb +36 -24
  109. data/lib/pry/last_exception.rb +45 -45
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/method/patcher.rb +14 -8
  112. data/lib/pry/method/weird_method_locator.rb +79 -43
  113. data/lib/pry/method.rb +177 -124
  114. data/lib/pry/object_path.rb +37 -28
  115. data/lib/pry/output.rb +102 -16
  116. data/lib/pry/pager.rb +187 -174
  117. data/lib/pry/prompt.rb +213 -25
  118. data/lib/pry/pry_class.rb +106 -98
  119. data/lib/pry/pry_instance.rb +261 -224
  120. data/lib/pry/repl.rb +82 -27
  121. data/lib/pry/repl_file_loader.rb +27 -22
  122. data/lib/pry/ring.rb +89 -0
  123. data/lib/pry/slop/LICENSE +20 -0
  124. data/lib/pry/slop/commands.rb +190 -0
  125. data/lib/pry/slop/option.rb +210 -0
  126. data/lib/pry/slop.rb +672 -0
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable/evalable.rb +24 -0
  130. data/lib/pry/testable/mockable.rb +22 -0
  131. data/lib/pry/testable/pry_tester.rb +88 -0
  132. data/lib/pry/testable/utility.rb +34 -0
  133. data/lib/pry/testable/variables.rb +52 -0
  134. data/lib/pry/testable.rb +68 -0
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +27 -0
  137. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +29 -27
  138. data/lib/pry/wrapped_module.rb +67 -57
  139. data/lib/pry.rb +133 -149
  140. metadata +52 -63
  141. data/lib/pry/commands/disabled_commands.rb +0 -2
  142. data/lib/pry/commands/gem_cd.rb +0 -26
  143. data/lib/pry/commands/gem_install.rb +0 -32
  144. data/lib/pry/commands/gem_list.rb +0 -33
  145. data/lib/pry/commands/gem_open.rb +0 -29
  146. data/lib/pry/commands/gist.rb +0 -101
  147. data/lib/pry/commands/install_command.rb +0 -53
  148. data/lib/pry/commands/list_prompts.rb +0 -35
  149. data/lib/pry/commands/simple_prompt.rb +0 -22
  150. data/lib/pry/commands.rb +0 -6
  151. data/lib/pry/config/behavior.rb +0 -139
  152. data/lib/pry/config/convenience.rb +0 -25
  153. data/lib/pry/config/default.rb +0 -161
  154. data/lib/pry/history_array.rb +0 -121
  155. data/lib/pry/plugins.rb +0 -103
  156. data/lib/pry/rbx_path.rb +0 -22
  157. data/lib/pry/rubygem.rb +0 -82
  158. data/lib/pry/terminal.rb +0 -79
  159. data/lib/pry/test/helper.rb +0 -170
data/lib/pry/code/loc.rb CHANGED
@@ -1,10 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  class Code
3
-
4
- # Represents a line of code. A line of code is a tuple, which consists of a
5
- # line and a line number. A `LOC` object's state (namely, the line
6
- # parameter) can be changed via instance methods. `Pry::Code` heavily uses
7
- # this class.
5
+ # Represents a line of code (which may, in fact, contain multiple lines if
6
+ # the entirety was eval'd as a single unit following the `edit` command).
7
+ #
8
+ # A line of code is a tuple, which consists of a line and a line number. A
9
+ # `LOC` object's state (namely, the line parameter) can be changed via
10
+ # instance methods. `Pry::Code` heavily uses this class.
8
11
  #
9
12
  # @api private
10
13
  # @example
@@ -18,7 +21,6 @@ class Pry
18
21
  # loc.indent(3)
19
22
  # loc.line #=> " def example\n :example\nend"
20
23
  class LOC
21
-
22
24
  # @return [Array<String, Integer>]
23
25
  attr_reader :tuple
24
26
 
@@ -52,7 +54,7 @@ class Pry
52
54
  # @param [Symbol] code_type
53
55
  # @return [void]
54
56
  def colorize(code_type)
55
- tuple[0] = CodeRay.scan(line, code_type).term
57
+ tuple[0] = SyntaxHighlighter.highlight(line, code_type)
56
58
  end
57
59
 
58
60
  # Prepends the line number `lineno` to the `line`.
@@ -61,8 +63,14 @@ class Pry
61
63
  # @return [void]
62
64
  def add_line_number(max_width = 0, color = false)
63
65
  padded = lineno.to_s.rjust(max_width)
64
- colorized_lineno = color ? Pry::Helpers::BaseHelpers.colorize_code(padded) : padded
65
- tuple[0] = "#{ colorized_lineno }: #{ line }"
66
+ colorized_lineno =
67
+ if color
68
+ Pry::Helpers::BaseHelpers.colorize_code(padded)
69
+ else
70
+ padded
71
+ end
72
+ properly_padded_line = handle_multiline_entries_from_edit_command(line, max_width)
73
+ tuple[0] = "#{colorized_lineno}: #{properly_padded_line}"
66
74
  end
67
75
 
68
76
  # Prepends a marker "=>" or an empty marker to the +line+.
@@ -73,9 +81,9 @@ class Pry
73
81
  def add_marker(marker_lineno)
74
82
  tuple[0] =
75
83
  if lineno == marker_lineno
76
- " => #{ line }"
84
+ " => #{line}"
77
85
  else
78
- " #{ line }"
86
+ " #{line}"
79
87
  end
80
88
  end
81
89
 
@@ -84,9 +92,14 @@ class Pry
84
92
  # @param [Integer] distance
85
93
  # @return [void]
86
94
  def indent(distance)
87
- tuple[0] = "#{ ' ' * distance }#{ line }"
95
+ tuple[0] = "#{' ' * distance}#{line}"
88
96
  end
89
- end
90
97
 
98
+ def handle_multiline_entries_from_edit_command(line, max_width)
99
+ line.split("\n").map.with_index do |inner_line, i|
100
+ i.zero? ? inner_line : "#{' ' * (max_width + 2)}#{inner_line}"
101
+ end.join("\n")
102
+ end
103
+ end
91
104
  end
92
105
  end
data/lib/pry/code.rb CHANGED
@@ -1,6 +1,6 @@
1
- require 'pry/code/loc'
2
- require 'pry/code/code_range'
3
- require 'pry/code/code_file'
1
+ # frozen_string_literal: true
2
+
3
+ require 'method_source'
4
4
 
5
5
  class Pry
6
6
  class << self
@@ -66,7 +66,7 @@ class Pry
66
66
  # @param [Integer, nil] start_line The line number to start on, or nil to
67
67
  # use the method's original line numbers.
68
68
  # @return [Code]
69
- def from_module(mod, candidate_rank = 0, start_line=nil)
69
+ def from_module(mod, candidate_rank = 0, start_line = nil)
70
70
  candidate = Pry::WrappedModule(mod).candidate(candidate_rank)
71
71
  start_line ||= candidate.line
72
72
  new(candidate.source, start_line, :ruby)
@@ -85,26 +85,23 @@ class Pry
85
85
  # @param [Integer?] start_line
86
86
  # @param [Symbol?] code_type
87
87
  def initialize(lines = [], start_line = 1, code_type = :ruby)
88
- if lines.is_a? String
89
- lines = lines.lines
88
+ lines = lines.lines if lines.is_a? String
89
+ @lines = lines.each_with_index.map do |line, lineno|
90
+ LOC.new(line, lineno + start_line.to_i)
90
91
  end
91
- @lines = lines.each_with_index.map { |line, lineno|
92
- LOC.new(line, lineno + start_line.to_i) }
93
92
  @code_type = code_type
93
+
94
+ @with_marker = @with_indentation = @with_line_numbers = nil
94
95
  end
95
96
 
96
97
  # Append the given line. +lineno+ is one more than the last existing
97
98
  # line, unless specified otherwise.
98
99
  #
99
100
  # @param [String] line
100
- # @param [Integer?] lineno
101
- # @return [String] The inserted line.
102
- def push(line, lineno = nil)
103
- if lineno.nil?
104
- lineno = @lines.last.lineno + 1
105
- end
106
- @lines.push(LOC.new(line, lineno))
107
- line
101
+ # @return [void]
102
+ def push(line)
103
+ line_number = @lines.any? ? @lines.last.lineno + 1 : 1
104
+ @lines.push(LOC.new(line, line_number))
108
105
  end
109
106
  alias << push
110
107
 
@@ -118,6 +115,16 @@ class Pry
118
115
  end
119
116
  end
120
117
 
118
+ # Filter the lines using the given block.
119
+ #
120
+ # @yield [LOC]
121
+ # @return [Code]
122
+ def reject(&block)
123
+ alter do
124
+ @lines = @lines.reject(&block)
125
+ end
126
+ end
127
+
121
128
  # Remove all lines that aren't in the given range, expressed either as a
122
129
  # `Range` object or a first and last line number (inclusive). Negative
123
130
  # indices count from the end of the array of lines.
@@ -199,6 +206,7 @@ class Pry
199
206
  # @return [Code]
200
207
  def grep(pattern)
201
208
  return self unless pattern
209
+
202
210
  pattern = Regexp.new(pattern)
203
211
 
204
212
  select do |loc|
@@ -240,30 +248,25 @@ class Pry
240
248
  end
241
249
  end
242
250
 
243
- # @return [String]
244
- def inspect
245
- Object.instance_method(:to_s).bind(self).call
246
- end
247
-
248
251
  # @return [Integer] the number of digits in the last line.
249
252
  def max_lineno_width
250
- @lines.length > 0 ? @lines.last.lineno.to_s.length : 0
253
+ !@lines.empty? ? @lines.last.lineno.to_s.length : 0
251
254
  end
252
255
 
253
256
  # @return [String] a formatted representation (based on the configuration of
254
257
  # the object).
255
258
  def to_s
256
- print_to_output("", false)
259
+ print_to_output(''.dup, false)
257
260
  end
258
261
 
259
262
  # @return [String] a (possibly highlighted) copy of the source code.
260
263
  def highlighted
261
- print_to_output("", true)
264
+ print_to_output(''.dup, true)
262
265
  end
263
266
 
264
267
  # Writes a formatted representation (based on the configuration of the
265
268
  # object) to the given output, which must respond to `#<<`.
266
- def print_to_output(output, color=false)
269
+ def print_to_output(output, color = false)
267
270
  @lines.each do |loc|
268
271
  loc = loc.dup
269
272
  loc.colorize(@code_type) if color
@@ -289,15 +292,14 @@ class Pry
289
292
  # @param [Integer] line_number (1-based)
290
293
  # @return [String] the code.
291
294
  def expression_at(line_number, consume = 0)
292
- self.class.expression_at(raw, line_number, :consume => consume)
295
+ self.class.expression_at(raw, line_number, consume: consume)
293
296
  end
294
297
 
295
298
  # Get the (approximate) Module.nesting at the give line number.
296
299
  #
297
300
  # @param [Integer] line_number line number starting from 1
298
- # @param [Module] top_module the module in which this code exists
299
301
  # @return [Array<Module>] a list of open modules.
300
- def nesting_at(line_number, top_module = Object)
302
+ def nesting_at(line_number)
301
303
  Pry::Indent.nesting_at(raw, line_number)
302
304
  end
303
305
 
@@ -330,10 +332,27 @@ class Pry
330
332
  end
331
333
 
332
334
  # Forward any missing methods to the output of `#to_s`.
333
- def method_missing(name, *args, &block)
334
- to_s.send(name, *args, &block)
335
+ def method_missing(method_name, *args, &block)
336
+ if (string = to_s).respond_to?(method_name)
337
+ string.__send__(method_name, *args, &block)
338
+ else
339
+ super
340
+ end
341
+ end
342
+ undef =~ if method_defined?(:=~)
343
+
344
+ # Check whether String responds to missing methods.
345
+ def respond_to_missing?(method_name, include_private = false)
346
+ ''.respond_to?(method_name, include_private) || super
347
+ end
348
+
349
+ if RUBY_VERSION.start_with?('1.9')
350
+ # @todo This is needed for Ruby 1.9 support where `lines` return an
351
+ # Enumerator. Newer Rubies return an Array
352
+ def lines
353
+ super.to_a
354
+ end
335
355
  end
336
- undef =~
337
356
 
338
357
  protected
339
358
 
@@ -1,5 +1,6 @@
1
- class Pry
1
+ # frozen_string_literal: true
2
2
 
3
+ class Pry
3
4
  # This class is responsible for taking a string (identifying a
4
5
  # command/class/method/etc) and returning the relevant type of object.
5
6
  # For example, if the user looks up "show-source" then a
@@ -37,13 +38,36 @@ class Pry
37
38
  def command?
38
39
  is_a?(Module) && self <= Pry::Command
39
40
  end
41
+
42
+ # @return [Boolean] `true` if this module was defined by means of the C API,
43
+ # `false` if it's a Ruby module.
44
+ # @note If a module defined by C was extended with a lot of methods written
45
+ # in Ruby, this method would fail.
46
+ def c_module?
47
+ return unless is_a?(WrappedModule)
48
+
49
+ method_locations = wrapped.methods(false).map do |m|
50
+ wrapped.method(m).source_location
51
+ end
52
+
53
+ method_locations.concat(
54
+ wrapped.instance_methods(false).map do |m|
55
+ wrapped.instance_method(m).source_location
56
+ end
57
+ )
58
+
59
+ c_methods = method_locations.grep(nil).count
60
+ ruby_methods = method_locations.count - c_methods
61
+
62
+ c_methods > ruby_methods
63
+ end
40
64
  end
41
65
 
42
66
  include Pry::Helpers::CommandHelpers
43
67
 
44
68
  class << self
45
- def lookup(str, _pry_, options={})
46
- co = new(str, _pry_, options)
69
+ def lookup(str, pry_instance, options = {})
70
+ co = new(str, pry_instance, options)
47
71
 
48
72
  co.default_lookup || co.method_or_class_lookup ||
49
73
  co.command_lookup || co.empty_lookup
@@ -52,24 +76,25 @@ class Pry
52
76
 
53
77
  attr_accessor :str
54
78
  attr_accessor :target
55
- attr_accessor :_pry_
79
+ attr_accessor :pry_instance
56
80
  attr_accessor :super_level
57
81
 
58
- def initialize(str, _pry_, options={})
82
+ def initialize(str, pry_instance, options = {})
59
83
  options = {
60
- :super => 0,
84
+ super: 0
61
85
  }.merge!(options)
62
86
 
63
87
  @str = str
64
- @_pry_ = _pry_
65
- @target = _pry_.current_context
88
+ @pry_instance = pry_instance
89
+ @target = pry_instance.current_context
66
90
  @super_level = options[:super]
67
91
  end
68
92
 
93
+ # TODO: just make it so find_command_by_match_or_listing doesn't raise?
69
94
  def command_lookup
70
- # TODO: just make it so find_command_by_match_or_listing doesn't
71
- # raise?
72
- _pry_.commands.find_command_by_match_or_listing(str) rescue nil
95
+ pry_instance.commands.find_command_by_match_or_listing(str)
96
+ rescue StandardError
97
+ nil
73
98
  end
74
99
 
75
100
  # when no paramter is given (i.e CodeObject.lookup(nil)), then we
@@ -91,8 +116,8 @@ class Pry
91
116
 
92
117
  # lookup variables and constants and `self` that are not modules
93
118
  def default_lookup
94
-
95
- # we skip instance methods as we want those to fall through to method_or_class_lookup()
119
+ # we skip instance methods as we want those to fall through to
120
+ # method_or_class_lookup()
96
121
  if safe_to_evaluate?(str) && !looks_like_an_instance_method?(str)
97
122
  obj = target.eval(str)
98
123
 
@@ -102,22 +127,22 @@ class Pry
102
127
  Pry::Method(obj)
103
128
  elsif !obj.is_a?(Module)
104
129
  Pry::WrappedModule(obj.class)
105
- else
106
- nil
107
130
  end
108
131
  end
109
-
110
132
  rescue Pry::RescuableException
111
133
  nil
112
134
  end
113
135
 
114
136
  def method_or_class_lookup
115
- obj = case str
116
- when /\S+\(\)\z/
117
- Pry::Method.from_str(str.sub(/\(\)\z/, ''),target) || Pry::WrappedModule.from_str(str, target)
118
- else
119
- Pry::WrappedModule.from_str(str,target) || Pry::Method.from_str(str, target)
120
- end
137
+ obj =
138
+ case str
139
+ when /\S+\(\)\z/
140
+ Pry::Method.from_str(str.sub(/\(\)\z/, ''), target) ||
141
+ Pry::WrappedModule.from_str(str, target)
142
+ else
143
+ Pry::WrappedModule.from_str(str, target) ||
144
+ Pry::Method.from_str(str, target)
145
+ end
121
146
 
122
147
  lookup_super(obj, super_level)
123
148
  end
@@ -147,6 +172,8 @@ class Pry
147
172
  # @return [Boolean]
148
173
  def safe_to_evaluate?(str)
149
174
  return true if str.strip == "self"
175
+ return false if str =~ /%/
176
+
150
177
  kind = target.eval("defined?(#{str})")
151
178
  kind =~ /variable|constant/
152
179
  end
@@ -159,14 +186,12 @@ class Pry
159
186
  # @param [Object] obj
160
187
  # @param [Fixnum] super_level How far up the super chain to ascend.
161
188
  def lookup_super(obj, super_level)
162
- return nil if !obj
189
+ return unless obj
163
190
 
164
191
  sup = obj.super(super_level)
165
- if !sup
166
- raise Pry::CommandError, "No superclass found for #{obj.wrapped}"
167
- else
168
- sup
169
- end
192
+ raise Pry::CommandError, "No superclass found for #{obj.wrapped}" unless sup
193
+
194
+ sup
170
195
  end
171
196
  end
172
197
  end
@@ -1,55 +1,66 @@
1
- # PP subclass for streaming inspect output in color.
1
+ # frozen_string_literal: true
2
+
3
+ require 'pp'
4
+ require 'English'
5
+
2
6
  class Pry
7
+ # PP subclass for streaming inspect output in color.
3
8
  class ColorPrinter < ::PP
4
- OBJ_COLOR = begin
5
- code = CodeRay::Encoders::Terminal::TOKEN_COLORS[:keyword]
6
- if code.start_with? "\e"
7
- code
8
- else
9
- "\e[0m\e[0;#{code}m"
9
+ Pry::SyntaxHighlighter.overwrite_coderay_comment_token!
10
+
11
+ def self.default(_output, value, pry_instance)
12
+ pry_instance.pager.open do |pager|
13
+ pager.print pry_instance.config.output_prefix
14
+ pp(value, pager, pry_instance.output.width - 1)
10
15
  end
11
16
  end
12
17
 
13
- CodeRay::Encoders::Terminal::TOKEN_COLORS[:comment][:self] = "\e[1;34m"
18
+ def self.pp(obj, output = $DEFAULT_OUTPUT, max_width = 79)
19
+ queue = ColorPrinter.new(output, max_width, "\n")
20
+ queue.guard_inspect_key { queue.pp(obj) }
21
+ queue.flush
22
+ output << "\n"
23
+ end
14
24
 
15
- def self.pp(obj, out = $>, width = 79)
16
- q = ColorPrinter.new(out, width)
17
- q.guard_inspect_key { q.pp obj }
18
- q.flush
19
- out << "\n"
25
+ def pp(object)
26
+ return super unless object.is_a?(String)
27
+
28
+ # Avoid calling Ruby 2.4+ String#pretty_print that prints multiline
29
+ # Strings prettier
30
+ text(object.inspect)
31
+ rescue StandardError => exception
32
+ raise if exception.is_a?(Pry::Pager::StopPaging)
33
+
34
+ text(highlight_object_literal(inspect_object(object)))
20
35
  end
21
36
 
22
- def text(str, width = str.length)
23
- # Don't recolorize output with color [Issue #751]
37
+ def text(str, max_width = str.length)
24
38
  if str.include?("\e[")
25
- super "#{str}\e[0m", width
26
- elsif str.start_with?('#<') || str == '=' || str == '>'
27
- super highlight_object_literal(str), width
39
+ super("#{str}\e[0m", max_width)
40
+ elsif str.start_with?('#<') || %w[= >].include?(str)
41
+ super(highlight_object_literal(str), max_width)
28
42
  else
29
- super CodeRay.scan(str, :ruby).term, width
43
+ super(SyntaxHighlighter.highlight(str), max_width)
30
44
  end
31
45
  end
32
46
 
33
- def pp(obj)
34
- super
35
- rescue => e
36
- raise if e.is_a? Pry::Pager::StopPaging
47
+ private
37
48
 
49
+ def highlight_object_literal(object_literal)
50
+ code = Pry::SyntaxHighlighter.keyword_token_color
51
+ obj_color = code.start_with?("\e") ? code : "\e[0m\e[0;#{code}m"
52
+ "#{obj_color}#{object_literal}\e[0m"
53
+ end
54
+
55
+ def inspect_object(object)
56
+ object.inspect
57
+ rescue StandardError
38
58
  # Read the class name off of the singleton class to provide a default
39
59
  # inspect.
40
- singleton = class << obj; self; end
60
+ singleton = class << object; self; end
41
61
  ancestors = Pry::Method.safe_send(singleton, :ancestors)
42
- klass = ancestors.reject { |k| k == singleton }.first
43
- obj_id = obj.__id__.to_s(16) rescue 0
44
- str = "#<#{klass}:0x#{obj_id}>"
45
-
46
- text highlight_object_literal(str)
47
- end
48
-
49
- private
50
-
51
- def highlight_object_literal(object_literal)
52
- "#{OBJ_COLOR}#{object_literal}\e[0m"
62
+ klass = ancestors.find { |k| k != singleton }
63
+ "#<#{klass}:0x#{object.__id__.to_s(16)}>"
53
64
  end
54
65
  end
55
66
  end