pry 0.12.1-java → 0.14.1-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +166 -1
  3. data/LICENSE +1 -1
  4. data/README.md +331 -269
  5. data/bin/pry +5 -0
  6. data/lib/pry.rb +132 -118
  7. data/lib/pry/basic_object.rb +8 -4
  8. data/lib/pry/block_command.rb +22 -0
  9. data/lib/pry/class_command.rb +194 -0
  10. data/lib/pry/cli.rb +43 -54
  11. data/lib/pry/code.rb +40 -28
  12. data/lib/pry/code/code_file.rb +28 -24
  13. data/lib/pry/code/code_range.rb +4 -2
  14. data/lib/pry/code/loc.rb +15 -8
  15. data/lib/pry/code_object.rb +40 -38
  16. data/lib/pry/color_printer.rb +47 -46
  17. data/lib/pry/command.rb +162 -360
  18. data/lib/pry/command_set.rb +76 -73
  19. data/lib/pry/command_state.rb +31 -0
  20. data/lib/pry/commands/amend_line.rb +86 -81
  21. data/lib/pry/commands/bang.rb +18 -14
  22. data/lib/pry/commands/bang_pry.rb +15 -11
  23. data/lib/pry/commands/cat.rb +61 -54
  24. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  25. data/lib/pry/commands/cat/exception_formatter.rb +71 -60
  26. data/lib/pry/commands/cat/file_formatter.rb +55 -49
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  28. data/lib/pry/commands/cd.rb +40 -35
  29. data/lib/pry/commands/change_inspector.rb +29 -22
  30. data/lib/pry/commands/change_prompt.rb +44 -39
  31. data/lib/pry/commands/clear_screen.rb +16 -10
  32. data/lib/pry/commands/code_collector.rb +148 -133
  33. data/lib/pry/commands/disable_pry.rb +23 -19
  34. data/lib/pry/commands/easter_eggs.rb +19 -30
  35. data/lib/pry/commands/edit.rb +184 -161
  36. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  37. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  38. data/lib/pry/commands/exit.rb +39 -35
  39. data/lib/pry/commands/exit_all.rb +24 -20
  40. data/lib/pry/commands/exit_program.rb +20 -16
  41. data/lib/pry/commands/find_method.rb +168 -160
  42. data/lib/pry/commands/fix_indent.rb +16 -12
  43. data/lib/pry/commands/help.rb +140 -133
  44. data/lib/pry/commands/hist.rb +151 -150
  45. data/lib/pry/commands/import_set.rb +20 -16
  46. data/lib/pry/commands/jump_to.rb +25 -21
  47. data/lib/pry/commands/list_inspectors.rb +35 -28
  48. data/lib/pry/commands/ls.rb +124 -102
  49. data/lib/pry/commands/ls/constants.rb +59 -42
  50. data/lib/pry/commands/ls/formatter.rb +50 -46
  51. data/lib/pry/commands/ls/globals.rb +38 -34
  52. data/lib/pry/commands/ls/grep.rb +17 -13
  53. data/lib/pry/commands/ls/instance_vars.rb +29 -27
  54. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  55. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  56. data/lib/pry/commands/ls/local_names.rb +26 -22
  57. data/lib/pry/commands/ls/local_vars.rb +38 -28
  58. data/lib/pry/commands/ls/ls_entity.rb +47 -51
  59. data/lib/pry/commands/ls/methods.rb +44 -43
  60. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  61. data/lib/pry/commands/ls/self_methods.rb +23 -22
  62. data/lib/pry/commands/nesting.rb +21 -17
  63. data/lib/pry/commands/play.rb +93 -82
  64. data/lib/pry/commands/pry_backtrace.rb +22 -17
  65. data/lib/pry/commands/pry_version.rb +15 -11
  66. data/lib/pry/commands/raise_up.rb +27 -22
  67. data/lib/pry/commands/reload_code.rb +60 -48
  68. data/lib/pry/commands/reset.rb +16 -12
  69. data/lib/pry/commands/ri.rb +55 -45
  70. data/lib/pry/commands/save_file.rb +45 -43
  71. data/lib/pry/commands/shell_command.rb +51 -51
  72. data/lib/pry/commands/shell_mode.rb +21 -17
  73. data/lib/pry/commands/show_doc.rb +80 -68
  74. data/lib/pry/commands/show_info.rb +189 -171
  75. data/lib/pry/commands/show_input.rb +16 -11
  76. data/lib/pry/commands/show_source.rb +110 -45
  77. data/lib/pry/commands/stat.rb +35 -31
  78. data/lib/pry/commands/switch_to.rb +21 -15
  79. data/lib/pry/commands/toggle_color.rb +20 -16
  80. data/lib/pry/commands/watch_expression.rb +89 -86
  81. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  82. data/lib/pry/commands/whereami.rb +156 -148
  83. data/lib/pry/commands/wtf.rb +75 -50
  84. data/lib/pry/config.rb +307 -25
  85. data/lib/pry/config/attributable.rb +22 -0
  86. data/lib/pry/config/lazy_value.rb +29 -0
  87. data/lib/pry/config/memoized_value.rb +34 -0
  88. data/lib/pry/config/value.rb +24 -0
  89. data/lib/pry/control_d_handler.rb +28 -0
  90. data/lib/pry/core_extensions.rb +9 -7
  91. data/lib/pry/editor.rb +48 -21
  92. data/lib/pry/env.rb +18 -0
  93. data/lib/pry/exception_handler.rb +43 -0
  94. data/lib/pry/exceptions.rb +13 -16
  95. data/lib/pry/forwardable.rb +5 -1
  96. data/lib/pry/helpers.rb +2 -0
  97. data/lib/pry/helpers/base_helpers.rb +58 -59
  98. data/lib/pry/helpers/command_helpers.rb +50 -61
  99. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  100. data/lib/pry/helpers/options_helpers.rb +14 -7
  101. data/lib/pry/helpers/platform.rb +7 -5
  102. data/lib/pry/helpers/table.rb +33 -26
  103. data/lib/pry/helpers/text.rb +22 -19
  104. data/lib/pry/history.rb +48 -56
  105. data/lib/pry/hooks.rb +17 -8
  106. data/lib/pry/indent.rb +54 -50
  107. data/lib/pry/input_completer.rb +248 -230
  108. data/lib/pry/input_lock.rb +8 -9
  109. data/lib/pry/inspector.rb +36 -24
  110. data/lib/pry/last_exception.rb +45 -45
  111. data/lib/pry/method.rb +141 -85
  112. data/lib/pry/method/disowned.rb +16 -4
  113. data/lib/pry/method/patcher.rb +12 -3
  114. data/lib/pry/method/weird_method_locator.rb +68 -44
  115. data/lib/pry/object_path.rb +33 -25
  116. data/lib/pry/output.rb +121 -35
  117. data/lib/pry/pager.rb +41 -42
  118. data/lib/pry/prompt.rb +108 -46
  119. data/lib/pry/pry_class.rb +61 -103
  120. data/lib/pry/pry_instance.rb +217 -185
  121. data/lib/pry/repl.rb +18 -22
  122. data/lib/pry/repl_file_loader.rb +27 -21
  123. data/lib/pry/ring.rb +11 -6
  124. data/lib/pry/slop.rb +574 -563
  125. data/lib/pry/slop/commands.rb +164 -169
  126. data/lib/pry/slop/option.rb +172 -168
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable.rb +59 -61
  130. data/lib/pry/testable/evalable.rb +21 -12
  131. data/lib/pry/testable/mockable.rb +18 -10
  132. data/lib/pry/testable/pry_tester.rb +71 -56
  133. data/lib/pry/testable/utility.rb +29 -21
  134. data/lib/pry/testable/variables.rb +49 -43
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +27 -0
  137. data/lib/pry/wrapped_module.rb +51 -42
  138. data/lib/pry/wrapped_module/candidate.rb +21 -14
  139. metadata +34 -33
  140. data/lib/pry/commands.rb +0 -6
  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/gem_readme.rb +0 -25
  147. data/lib/pry/commands/gem_search.rb +0 -40
  148. data/lib/pry/commands/gem_stats.rb +0 -83
  149. data/lib/pry/commands/gist.rb +0 -102
  150. data/lib/pry/commands/install_command.rb +0 -54
  151. data/lib/pry/config/behavior.rb +0 -255
  152. data/lib/pry/config/convenience.rb +0 -28
  153. data/lib/pry/config/default.rb +0 -159
  154. data/lib/pry/config/memoization.rb +0 -48
  155. data/lib/pry/plugins.rb +0 -122
  156. data/lib/pry/rubygem.rb +0 -84
  157. data/lib/pry/terminal.rb +0 -91
data/lib/pry/pager.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'pry/terminal'
1
+ # frozen_string_literal: true
2
2
 
3
3
  # A pager is an `IO`-like object that accepts text and either prints it
4
4
  # immediately, prints it one page at a time, or streams it to an external
@@ -8,10 +8,10 @@ class Pry
8
8
  class StopPaging < StandardError
9
9
  end
10
10
 
11
- attr_reader :_pry_
11
+ attr_reader :pry_instance
12
12
 
13
- def initialize(_pry_)
14
- @_pry_ = _pry_
13
+ def initialize(pry_instance)
14
+ @pry_instance = pry_instance
15
15
  end
16
16
 
17
17
  # Send the given text through the best available pager (if
@@ -33,16 +33,18 @@ class Pry
33
33
  def open
34
34
  pager = best_available
35
35
  yield pager
36
- rescue StopPaging
36
+ rescue StopPaging # rubocop:disable Lint/HandleExceptions
37
37
  ensure
38
38
  pager.close if pager
39
39
  end
40
40
 
41
41
  private
42
42
 
43
- def enabled?; !!@enabled; end
43
+ def enabled?
44
+ !!@enabled
45
+ end
44
46
 
45
- def output; @output; end
47
+ attr_reader :output
46
48
 
47
49
  # Return an instance of the "best" available pager class --
48
50
  # `SystemPager` if possible, `SimplePager` if `SystemPager` isn't
@@ -52,12 +54,12 @@ class Pry
52
54
  # you must rescue `Pry::Pager::StopPaging`. These requirements can be
53
55
  # avoided by using `.open` instead.
54
56
  def best_available
55
- if !_pry_.config.pager
56
- NullPager.new(_pry_.output)
57
+ if !pry_instance.config.pager
58
+ NullPager.new(pry_instance.output)
57
59
  elsif !SystemPager.available? || Helpers::Platform.jruby?
58
- SimplePager.new(_pry_.output)
60
+ SimplePager.new(pry_instance.output)
59
61
  else
60
- SystemPager.new(_pry_.output)
62
+ SystemPager.new(pry_instance.output)
61
63
  end
62
64
  end
63
65
 
@@ -81,17 +83,16 @@ class Pry
81
83
  @out.write str
82
84
  end
83
85
 
84
- def close
85
- end
86
+ def close; end
86
87
 
87
88
  private
88
89
 
89
90
  def height
90
- @height ||= Pry::Terminal.height!
91
+ @height ||= @out.height
91
92
  end
92
93
 
93
94
  def width
94
- @width ||= Pry::Terminal.width!
95
+ @width ||= @out.width
95
96
  end
96
97
  end
97
98
 
@@ -108,15 +109,15 @@ class Pry
108
109
  @out.print line
109
110
  @tracker.record line
110
111
 
111
- if @tracker.page?
112
- @out.print "\n"
113
- @out.print "\e[0m"
114
- @out.print "<page break> --- Press enter to continue " \
115
- "( q<enter> to break ) --- <page break>\n"
116
- raise StopPaging if Readline.readline("").chomp == "q"
112
+ next unless @tracker.page?
117
113
 
118
- @tracker.reset
119
- end
114
+ @out.print "\n"
115
+ @out.print "\e[0m"
116
+ @out.print "<page break> --- Press enter to continue " \
117
+ "( q<enter> to break ) --- <page break>\n"
118
+ raise StopPaging if Readline.readline("").chomp == "q"
119
+
120
+ @tracker.reset
120
121
  end
121
122
  end
122
123
  end
@@ -127,13 +128,11 @@ class Pry
127
128
  # buffered content.
128
129
  class SystemPager < NullPager
129
130
  def self.default_pager
130
- pager = ENV["PAGER"] || ""
131
+ pager = Pry::Env['PAGER'] || ''
131
132
 
132
133
  # Default to less, and make sure less is being passed the correct
133
134
  # options
134
- if pager.strip.empty? or pager =~ /^less\b/
135
- pager = "less -R -F -X"
136
- end
135
+ pager = "less -R -F -X" if pager.strip.empty? || pager =~ /^less\b/
137
136
 
138
137
  pager
139
138
  end
@@ -142,17 +141,18 @@ class Pry
142
141
 
143
142
  def self.available?
144
143
  if @system_pager.nil?
145
- @system_pager = begin
146
- pager_executable = default_pager.split(' ').first
147
- if Helpers::Platform.windows? || Helpers::Platform.windows_ansi?
148
- `where /Q #{pager_executable}`
149
- else
150
- `which #{pager_executable}`
144
+ @system_pager =
145
+ begin
146
+ pager_executable = default_pager.split(' ').first
147
+ if Helpers::Platform.windows? || Helpers::Platform.windows_ansi?
148
+ `where /Q #{pager_executable}`
149
+ else
150
+ `which #{pager_executable}`
151
+ end
152
+ $CHILD_STATUS.success?
153
+ rescue StandardError
154
+ false
151
155
  end
152
- $?.success?
153
- rescue
154
- false
155
- end
156
156
  else
157
157
  @system_pager
158
158
  end
@@ -170,11 +170,9 @@ class Pry
170
170
  write_to_pager str
171
171
  else
172
172
  @tracker.record str
173
- @buffer << str
173
+ @buffer += str
174
174
 
175
- if @tracker.page?
176
- write_to_pager @buffer
177
- end
175
+ write_to_pager @buffer if @tracker.page?
178
176
  end
179
177
  rescue Errno::EPIPE
180
178
  raise StopPaging
@@ -214,7 +212,8 @@ class Pry
214
212
  # `false` to `true` until we see a newline.
215
213
  class PageTracker
216
214
  def initialize(rows, cols)
217
- @rows, @cols = rows, cols
215
+ @rows = rows
216
+ @cols = cols
218
217
  reset
219
218
  end
220
219
 
data/lib/pry/prompt.rb CHANGED
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  # Prompt represents the Pry prompt, which can be used with Readline-like
3
5
  # libraries. It defines a few default prompts (default prompt, simple prompt,
4
- # etc) and also provides an API to add custom prompts.
6
+ # etc) and also provides an API for adding and implementing custom prompts.
5
7
  #
6
- # @example
8
+ # @example Registering a new Pry prompt
7
9
  # Pry::Prompt.add(
8
10
  # :ipython,
9
11
  # 'IPython-like prompt', [':', '...:']
10
- # ) do |_context, _nesting, _pry_, sep|
11
- # sep == ':' ? "In [#{_pry_.input_ring.count}]: " : ' ...: '
12
+ # ) do |_context, _nesting, pry_instance, sep|
13
+ # sep == ':' ? "In [#{pry_instance.input_ring.count}]: " : ' ...: '
12
14
  # end
13
15
  #
14
16
  # # Produces:
@@ -17,16 +19,23 @@ class Pry
17
19
  # # ...: end
18
20
  # # => :foo
19
21
  # # In [4]:
22
+ #
23
+ # @example Manually instantiating the Prompt class
24
+ # prompt_procs = [
25
+ # proc { '#{rand(1)}>" },
26
+ # proc { "#{('a'..'z').to_a.sample}*" }
27
+ # ]
28
+ # prompt = Pry::Prompt.new(
29
+ # :random,
30
+ # 'Random number or letter prompt.',
31
+ # prompt_procs
32
+ # )
33
+ # prompt.wait_proc.call(...) #=>
34
+ # prompt.incomplete_proc.call(...)
35
+ #
20
36
  # @since v0.11.0
21
37
  # @api public
22
- module Prompt
23
- # @return [String]
24
- DEFAULT_NAME = 'pry'.freeze
25
-
26
- # @return [Array<Object>] the list of objects that are known to have a
27
- # 1-line #inspect output suitable for prompt
28
- SAFE_CONTEXTS = [String, Numeric, Symbol, nil, true, false].freeze
29
-
38
+ class Prompt
30
39
  # A Hash that holds all prompts. The keys of the Hash are prompt
31
40
  # names, the values are Hash instances of the format {:description, :value}.
32
41
  @prompts = {}
@@ -35,13 +44,13 @@ class Pry
35
44
  # Retrieves a prompt.
36
45
  #
37
46
  # @example
38
- # Prompt[:my_prompt][:value]
47
+ # Prompt[:my_prompt]
39
48
  #
40
- # @param [Symbol] prompt_name The name of the prompt you want to access
49
+ # @param [Symbol] name The name of the prompt you want to access
41
50
  # @return [Hash{Symbol=>Object}]
42
51
  # @since v0.12.0
43
- def [](prompt_name)
44
- @prompts[prompt_name.to_s]
52
+ def [](name)
53
+ @prompts[name.to_s]
45
54
  end
46
55
 
47
56
  # @return [Hash{Symbol=>Hash}] the duplicate of the internal prompts hash
@@ -53,51 +62,104 @@ class Pry
53
62
 
54
63
  # Adds a new prompt to the prompt hash.
55
64
  #
56
- # @param [Symbol] prompt_name
65
+ # @param [Symbol] name
57
66
  # @param [String] description
58
67
  # @param [Array<String>] separators The separators to differentiate
59
68
  # between prompt modes (default mode and class/method definition mode).
60
69
  # The Array *must* have a size of 2.
61
- # @yield [context, nesting, _pry_, sep]
70
+ # @yield [context, nesting, pry_instance, sep]
62
71
  # @yieldparam context [Object] the context where Pry is currently in
63
72
  # @yieldparam nesting [Integer] whether the context is nested
64
- # @yieldparam _pry_ [Pry] the Pry instance
73
+ # @yieldparam pry_instance [Pry] the Pry instance
65
74
  # @yieldparam separator [String] separator string
66
75
  # @return [nil]
67
76
  # @raise [ArgumentError] if the size of `separators` is not 2
77
+ # @raise [ArgumentError] if `prompt_name` is already occupied
68
78
  # @since v0.12.0
69
- def add(prompt_name, description = '', separators = %w[> *])
79
+ def add(name, description = '', separators = %w[> *])
80
+ name = name.to_s
81
+
70
82
  unless separators.size == 2
71
83
  raise ArgumentError, "separators size must be 2, given #{separators.size}"
72
84
  end
73
85
 
74
- @prompts[prompt_name.to_s] = {
75
- description: description,
76
- value: separators.map do |sep|
77
- proc { |context, nesting, _pry_| yield(context, nesting, _pry_, sep) }
86
+ if @prompts.key?(name)
87
+ raise ArgumentError, "the '#{name}' prompt was already added"
88
+ end
89
+
90
+ @prompts[name] = new(
91
+ name,
92
+ description,
93
+ separators.map do |sep|
94
+ proc do |context, nesting, pry_instance|
95
+ yield(context, nesting, pry_instance, sep)
96
+ end
78
97
  end
79
- }
98
+ )
80
99
 
81
100
  nil
82
101
  end
102
+ end
83
103
 
84
- private
104
+ # @return [String]
105
+ attr_reader :name
106
+
107
+ # @return [String]
108
+ attr_reader :description
109
+
110
+ # @return [Array<Proc>] the array of procs that hold
111
+ # `[wait_proc, incomplete_proc]`
112
+ attr_reader :prompt_procs
113
+
114
+ # @param [String] name
115
+ # @param [String] description
116
+ # @param [Array<Proc>] prompt_procs
117
+ def initialize(name, description, prompt_procs)
118
+ @name = name
119
+ @description = description
120
+ @prompt_procs = prompt_procs
121
+ end
85
122
 
86
- def prompt_name(name)
87
- return name unless name.is_a?(Pry::Config::Lazy)
123
+ # @return [Proc] the proc which builds the wait prompt (`>`)
124
+ def wait_proc
125
+ @prompt_procs.first
126
+ end
88
127
 
89
- name.call
128
+ # @return [Proc] the proc which builds the prompt when in the middle of an
129
+ # expression such as open method, etc. (`*`)
130
+ def incomplete_proc
131
+ @prompt_procs.last
132
+ end
133
+
134
+ # @deprecated Use a `Pry::Prompt` instance directly
135
+ def [](key)
136
+ key = key.to_s
137
+ if %w[name description].include?(key)
138
+ Pry::Warning.warn(
139
+ "`Pry::Prompt[:#{@name}][:#{key}]` is deprecated. " \
140
+ "Use `#{self.class}##{key}` instead"
141
+ )
142
+ public_send(key)
143
+ elsif key.to_s == 'value'
144
+ Pry::Warning.warn(
145
+ "`#{self.class}[:#{@name}][:value]` is deprecated. Use " \
146
+ "`#{self.class}#prompt_procs` instead or an instance of " \
147
+ "`#{self.class}` directly"
148
+ )
149
+ @prompt_procs
90
150
  end
91
151
  end
92
152
 
93
- add 'default',
94
- "The default Pry prompt. Includes information about the current expression \n" \
95
- "number, evaluation context, and nesting level, plus a reminder that you're \n" \
96
- 'using Pry.' do |context, nesting, _pry_, sep|
153
+ add(
154
+ :default,
155
+ "The default Pry prompt. Includes information about the current expression \n" \
156
+ "number, evaluation context, and nesting level, plus a reminder that you're \n" \
157
+ 'using Pry.'
158
+ ) do |context, nesting, pry_instance, sep|
97
159
  format(
98
160
  "[%<in_count>s] %<name>s(%<context>s)%<nesting>s%<separator>s ",
99
- in_count: _pry_.input_ring.count,
100
- name: prompt_name(_pry_.config.prompt_name),
161
+ in_count: pry_instance.input_ring.count,
162
+ name: pry_instance.config.prompt_name,
101
163
  context: Pry.view_clip(context),
102
164
  nesting: (nesting > 0 ? ":#{nesting}" : ''),
103
165
  separator: sep
@@ -105,7 +167,7 @@ class Pry
105
167
  end
106
168
 
107
169
  add(
108
- 'simple',
170
+ :simple,
109
171
  "A simple `>>`.",
110
172
  ['>> ', ' | ']
111
173
  ) do |_, _, _, sep|
@@ -113,30 +175,30 @@ class Pry
113
175
  end
114
176
 
115
177
  add(
116
- 'nav',
178
+ :nav,
117
179
  "A prompt that displays the binding stack as a path and includes information \n" \
118
180
  "about #{Helpers::Text.bold('_in_')} and #{Helpers::Text.bold('_out_')}.",
119
181
  %w[> *]
120
- ) do |_context, _nesting, _pry_, sep|
121
- tree = _pry_.binding_stack.map { |b| Pry.view_clip(b.eval('self')) }
182
+ ) do |_context, _nesting, pry_instance, sep|
183
+ tree = pry_instance.binding_stack.map { |b| Pry.view_clip(b.eval('self')) }
122
184
  format(
123
185
  "[%<in_count>s] (%<name>s) %<tree>s: %<stack_size>s%<separator>s ",
124
- in_count: _pry_.input_ring.count,
125
- name: prompt_name(_pry_.config.prompt_name),
186
+ in_count: pry_instance.input_ring.count,
187
+ name: pry_instance.config.prompt_name,
126
188
  tree: tree.join(' / '),
127
- stack_size: _pry_.binding_stack.size - 1,
189
+ stack_size: pry_instance.binding_stack.size - 1,
128
190
  separator: sep
129
191
  )
130
192
  end
131
193
 
132
194
  add(
133
- 'shell',
195
+ :shell,
134
196
  'A prompt that displays `$PWD` as you change it.',
135
197
  %w[$ *]
136
- ) do |context, _nesting, _pry_, sep|
198
+ ) do |context, _nesting, pry_instance, sep|
137
199
  format(
138
200
  "%<name>s %<context>s:%<pwd>s %<separator>s ",
139
- name: prompt_name(_pry_.config.prompt_name),
201
+ name: pry_instance.config.prompt_name,
140
202
  context: Pry.view_clip(context),
141
203
  pwd: Dir.pwd,
142
204
  separator: sep
@@ -144,7 +206,7 @@ class Pry
144
206
  end
145
207
 
146
208
  add(
147
- 'none',
209
+ :none,
148
210
  'Wave goodbye to the Pry prompt.',
149
211
  Array.new(2)
150
212
  ) { '' }
data/lib/pry/pry_class.rb CHANGED
@@ -1,17 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+ require 'pathname'
5
+
1
6
  class Pry
2
- HOME_RC_FILE =
3
- if ENV.key?('PRYRC')
4
- ENV['PRYRC']
5
- elsif File.exist?(File.expand_path('~/.pryrc'))
6
- '~/.pryrc'
7
- elsif ENV.key?('XDG_CONFIG_HOME') && ENV['XDG_CONFIG_HOME'] != ''
8
- # See XDG Base Directory Specification at
9
- # https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
10
- ENV['XDG_CONFIG_HOME'] + '/pry/pryrc'
11
- else
12
- '~/.config/pry/pryrc'
13
- end
14
- LOCAL_RC_FILE = "./.pryrc"
7
+ LOCAL_RC_FILE = "./.pryrc".freeze
8
+
9
+ # @return [Boolean] true if this Ruby supports safe levels and tainting,
10
+ # to guard against using deprecated or unsupported features
11
+ HAS_SAFE_LEVEL = (
12
+ RUBY_ENGINE == 'ruby' &&
13
+ Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7')
14
+ )
15
15
 
16
16
  class << self
17
17
  extend Pry::Forwardable
@@ -23,24 +23,14 @@ class Pry
23
23
  attr_accessor :quiet
24
24
  attr_accessor :last_internal_error
25
25
  attr_accessor :config
26
- attr_writer :history
27
-
28
- def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
29
26
 
30
- extend Pry::Config::Convenience
31
- config_shortcut(*Pry::Config.shortcuts)
32
-
33
- def prompt=(value)
34
- config.prompt = value
35
- end
36
-
37
- def prompt
38
- config.prompt
39
- end
40
-
41
- def history
42
- @history ||= History.new
43
- end
27
+ def_delegators(
28
+ :@config, :input, :input=, :output, :output=, :commands,
29
+ :commands=, :print, :print=, :exception_handler, :exception_handler=,
30
+ :hooks, :hooks=, :color, :color=, :pager, :pager=, :editor, :editor=,
31
+ :memory_size, :memory_size=, :extra_sticky_locals, :extra_sticky_locals=,
32
+ :prompt, :prompt=, :history, :history=
33
+ )
44
34
 
45
35
  #
46
36
  # @example
@@ -71,7 +61,7 @@ class Pry
71
61
  # Returns a value store for an instance of Pry running on the current thread.
72
62
  #
73
63
  def self.current
74
- Thread.current[:__pry__] ||= Pry::Config.from_hash({}, nil)
64
+ Thread.current[:__pry__] ||= {}
75
65
  end
76
66
 
77
67
  # Load the given file in the context of `Pry.toplevel_binding`
@@ -82,8 +72,8 @@ class Pry
82
72
  puts "Error loading #{file}: #{e}\n#{e.backtrace.first}"
83
73
  end
84
74
 
85
- # Load HOME_RC_FILE and LOCAL_RC_FILE if appropriate
86
- # This method can also be used to reload the files if they have changed.
75
+ # Load RC files if appropriate This method can also be used to reload the
76
+ # files if they have changed.
87
77
  def self.load_rc_files
88
78
  rc_files_to_load.each do |file|
89
79
  critical_section do
@@ -95,7 +85,7 @@ class Pry
95
85
  # Load the local RC file (./.pryrc)
96
86
  def self.rc_files_to_load
97
87
  files = []
98
- files << HOME_RC_FILE if Pry.config.should_load_rc
88
+ files << Pry.config.rc_file if Pry.config.should_load_rc
99
89
  files << LOCAL_RC_FILE if Pry.config.should_load_local_rc
100
90
  files.map { |file| real_path_to(file) }.compact.uniq
101
91
  end
@@ -121,19 +111,17 @@ class Pry
121
111
  end
122
112
 
123
113
  def self.load_win32console
124
- begin
125
- require 'win32console'
126
- # The mswin and mingw versions of pry require win32console, so this should
127
- # only fail on jruby (where win32console doesn't work).
128
- # Instead we'll recommend ansicon, which does.
129
- rescue LoadError
130
- warn <<-WARNING if Pry.config.windows_console_warning
114
+ require 'win32console'
115
+ # The mswin and mingw versions of pry require win32console, so this should
116
+ # only fail on jruby (where win32console doesn't work).
117
+ # Instead we'll recommend ansicon, which does.
118
+ rescue LoadError
119
+ warn <<-WARNING if Pry.config.windows_console_warning
131
120
  For a better Pry experience on Windows, please use ansicon:
132
121
  https://github.com/adoxa/ansicon
133
122
  If you use an alternative to ansicon and don't want to see this warning again,
134
123
  you can add "Pry.config.windows_console_warning = false" to your pryrc.
135
- WARNING
136
- end
124
+ WARNING
137
125
  end
138
126
 
139
127
  # Do basic setup for initial session including: loading pryrc, plugins,
@@ -143,7 +131,7 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
143
131
 
144
132
  @initial_session = false
145
133
 
146
- # note these have to be loaded here rather than in pry_instance as
134
+ # note these have to be loaded here rather than in _pry_ as
147
135
  # we only want them loaded once per entire Pry lifetime.
148
136
  load_rc_files
149
137
  end
@@ -152,9 +140,8 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
152
140
  return if @session_finalized
153
141
 
154
142
  @session_finalized = true
155
- load_plugins if Pry.config.should_load_plugins
156
143
  load_requires if Pry.config.should_load_requires
157
- load_history if Pry.config.history.should_load
144
+ load_history if Pry.config.history_load
158
145
  load_traps if Pry.config.should_trap_interrupts
159
146
  load_win32console if Helpers::Platform.windows? && !Helpers::Platform.windows_ansi?
160
147
  end
@@ -167,8 +154,8 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
167
154
  # @example
168
155
  # Pry.start(Object.new, :input => MyInput.new)
169
156
  def self.start(target = nil, options = {})
170
- return if ENV['DISABLE_PRY']
171
- if ENV['FAIL_PRY']
157
+ return if Pry::Env['DISABLE_PRY']
158
+ if Pry::Env['FAIL_PRY']
172
159
  raise 'You have FAIL_PRY set to true, which results in Pry calls failing'
173
160
  end
174
161
 
@@ -176,7 +163,8 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
176
163
 
177
164
  if in_critical_section?
178
165
  output.puts "ERROR: Pry started inside Pry."
179
- output.puts "This can happen if you have a binding.pry inside a #to_s or #inspect function."
166
+ output.puts "This can happen if you have a binding.pry inside a #to_s " \
167
+ "or #inspect function."
180
168
  return
181
169
  end
182
170
 
@@ -206,7 +194,6 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
206
194
  # Execute the file through the REPL loop, non-interactively.
207
195
  # @param [String] file_name File name to load through the REPL.
208
196
  def self.load_file_through_repl(file_name)
209
- require "pry/repl_file_loader"
210
197
  REPLFileLoader.new(file_name).load
211
198
  end
212
199
 
@@ -232,17 +219,23 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
232
219
  def self.view_clip(obj, options = {})
233
220
  max = options.fetch :max_length, 60
234
221
  id = options.fetch :id, false
235
- if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max
222
+ if obj.is_a?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max
236
223
  obj.name.to_s
237
224
  elsif Pry.main == obj
238
- # special-case to support jruby.
239
- # fixed as of https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039
225
+ # Special-case to support jruby. Fixed as of:
226
+ # https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039
240
227
  # we can drop in the future.
241
228
  obj.to_s
242
- elsif Pry.config.prompt_safe_contexts.any? { |v| v === obj } && obj.inspect.length <= max
229
+ # rubocop:disable Style/CaseEquality
230
+ elsif Pry.config.prompt_safe_contexts.any? { |v| v === obj } &&
231
+ obj.inspect.length <= max
232
+ # rubocop:enable Style/CaseEquality
233
+
243
234
  obj.inspect
235
+ elsif id
236
+ format("#<#{obj.class}:0x%<id>x>", id: obj.object_id << 1)
244
237
  else
245
- id == true ? "#<#{obj.class}:0x%x>" % (obj.object_id << 1) : "#<#{obj.class}>"
238
+ "#<#{obj.class}>"
246
239
  end
247
240
  rescue RescuableException
248
241
  "unknown"
@@ -292,16 +285,6 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
292
285
  nil
293
286
  end
294
287
 
295
- def self.default_editor_for_platform
296
- return ENV['VISUAL'] if ENV['VISUAL'] and not ENV['VISUAL'].empty?
297
- return ENV['EDITOR'] if ENV['EDITOR'] and not ENV['EDITOR'].empty?
298
- return 'notepad' if Helpers::Platform.windows?
299
-
300
- %w(editor nano vi).detect do |editor|
301
- system("which #{editor} > /dev/null 2>&1")
302
- end
303
- end
304
-
305
288
  def self.auto_resize!
306
289
  Pry.config.input # by default, load Readline
307
290
 
@@ -311,23 +294,23 @@ you can add "Pry.config.windows_console_warning = false" to your pryrc.
311
294
  end
312
295
 
313
296
  if Readline::VERSION =~ /edit/i
314
- warn <<-EOT
297
+ warn(<<-WARN)
315
298
  Readline version #{Readline::VERSION} detected - will not auto_resize! correctly.
316
299
  For the fix, use GNU Readline instead:
317
- https://github.com/guard/guard/wiki/Add-proper-Readline-support-to-Ruby-on-Mac-OS-X
318
- EOT
300
+ https://github.com/guard/guard/wiki/Add-Readline-support-to-Ruby-on-Mac-OS-X
301
+ WARN
319
302
  return
320
303
  end
321
304
 
322
305
  trap :WINCH do
323
306
  begin
324
- Readline.set_screen_size(*Terminal.size!)
325
- rescue => e
307
+ Readline.set_screen_size(*output.size)
308
+ rescue StandardError => e
326
309
  warn "\nPry.auto_resize!'s Readline.set_screen_size failed: #{e}"
327
310
  end
328
311
  begin
329
312
  Readline.refresh_line
330
- rescue => e
313
+ rescue StandardError => e
331
314
  warn "\nPry.auto_resize!'s Readline.refresh_line failed: #{e}"
332
315
  end
333
316
  end
@@ -338,7 +321,7 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
338
321
  @initial_session = true
339
322
  @session_finalized = nil
340
323
 
341
- self.config = Pry::Config.new Pry::Config::Default.new
324
+ self.config = Pry::Config.new
342
325
  self.cli = false
343
326
  self.current_line = 1
344
327
  self.line_buffer = [""]
@@ -347,9 +330,7 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
347
330
 
348
331
  # Basic initialization.
349
332
  def self.init
350
- @plugin_manager ||= PluginManager.new
351
333
  reset_defaults
352
- locate_plugins
353
334
  end
354
335
 
355
336
  # Return a `Binding` object for `target` or return `target` if it is
@@ -358,15 +339,10 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
358
339
  # @param [Object] target The object to get a `Binding` object for.
359
340
  # @return [Binding] The `Binding` object.
360
341
  def self.binding_for(target)
361
- if Binding === target
362
- target
363
- else
364
- if Pry.main == target
365
- TOPLEVEL_BINDING
366
- else
367
- target.__binding__
368
- end
369
- end
342
+ return target if Binding === target # rubocop:disable Style/CaseEquality
343
+ return TOPLEVEL_BINDING if Pry.main == target
344
+
345
+ target.__binding__
370
346
  end
371
347
 
372
348
  def self.toplevel_binding
@@ -386,8 +362,8 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
386
362
  @toplevel_binding
387
363
  end
388
364
 
389
- def self.toplevel_binding=(binding)
390
- @toplevel_binding = binding
365
+ class << self
366
+ attr_writer :toplevel_binding
391
367
  end
392
368
 
393
369
  def self.in_critical_section?
@@ -402,24 +378,6 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
402
378
  ensure
403
379
  Thread.current[:pry_critical_section] -= 1
404
380
  end
405
-
406
- # Wraps a block in a named block called `Pry::Config::Lazy`. This is used for
407
- # dynamic config values, which are calculated every time
408
- # {Pry::Config::Lazy#call} is called.
409
- #
410
- # @example
411
- # # pryrc
412
- # Pry.config.prompt_name = Pry.lazy { rand(100) }
413
- #
414
- # # Session
415
- # [1] 96(main)>
416
- # [2] 19(main)>
417
- # [3] 80(main)>
418
- #
419
- # @return [#call]
420
- def self.lazy(&block)
421
- Pry::Config::Lazy.new(&block)
422
- end
423
381
  end
424
382
 
425
383
  Pry.init