pry 0.9.12.2 → 0.14.2

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 (237) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1141 -0
  3. data/LICENSE +2 -2
  4. data/README.md +466 -0
  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 +97 -92
  10. data/lib/pry/code/code_file.rb +114 -0
  11. data/lib/pry/code/code_range.rb +7 -4
  12. data/lib/pry/code/loc.rb +27 -14
  13. data/lib/pry/code.rb +62 -90
  14. data/lib/pry/code_object.rb +83 -39
  15. data/lib/pry/color_printer.rb +66 -0
  16. data/lib/pry/command.rb +202 -371
  17. data/lib/pry/command_set.rb +151 -133
  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 -73
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -63
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +64 -47
  27. data/lib/pry/commands/cd.rb +42 -26
  28. data/lib/pry/commands/change_inspector.rb +34 -0
  29. data/lib/pry/commands/change_prompt.rb +51 -0
  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 +33 -24
  36. data/lib/pry/commands/edit.rb +183 -167
  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 -17
  40. data/lib/pry/commands/find_method.rb +167 -167
  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 +153 -132
  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 +42 -0
  47. data/lib/pry/commands/ls/constants.rb +75 -0
  48. data/lib/pry/commands/ls/formatter.rb +55 -0
  49. data/lib/pry/commands/ls/globals.rb +50 -0
  50. data/lib/pry/commands/ls/grep.rb +23 -0
  51. data/lib/pry/commands/ls/instance_vars.rb +40 -0
  52. data/lib/pry/commands/ls/interrogatable.rb +24 -0
  53. data/lib/pry/commands/ls/jruby_hacks.rb +55 -0
  54. data/lib/pry/commands/ls/local_names.rb +37 -0
  55. data/lib/pry/commands/ls/local_vars.rb +47 -0
  56. data/lib/pry/commands/ls/ls_entity.rb +65 -0
  57. data/lib/pry/commands/ls/methods.rb +55 -0
  58. data/lib/pry/commands/ls/methods_helper.rb +50 -0
  59. data/lib/pry/commands/ls/self_methods.rb +34 -0
  60. data/lib/pry/commands/ls.rb +100 -303
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +93 -49
  63. data/lib/pry/commands/pry_backtrace.rb +22 -18
  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 +57 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -38
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +66 -34
  71. data/lib/pry/commands/shell_mode.rb +22 -20
  72. data/lib/pry/commands/show_doc.rb +80 -65
  73. data/lib/pry/commands/show_info.rb +193 -159
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +113 -33
  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 +21 -13
  79. data/lib/pry/commands/watch_expression/expression.rb +43 -0
  80. data/lib/pry/commands/watch_expression.rb +110 -0
  81. data/lib/pry/commands/whereami.rb +157 -134
  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 +290 -220
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +50 -27
  90. data/lib/pry/editor.rb +130 -102
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +73 -0
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +22 -151
  96. data/lib/pry/helpers/command_helpers.rb +55 -63
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  99. data/lib/pry/helpers/platform.rb +55 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +96 -86
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +101 -70
  104. data/lib/pry/hooks.rb +67 -137
  105. data/lib/pry/indent.rb +79 -73
  106. data/lib/pry/input_completer.rb +283 -0
  107. data/lib/pry/input_lock.rb +129 -0
  108. data/lib/pry/inspector.rb +39 -0
  109. data/lib/pry/last_exception.rb +61 -0
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +51 -42
  112. data/lib/pry/method/weird_method_locator.rb +80 -44
  113. data/lib/pry/method.rb +225 -176
  114. data/lib/pry/object_path.rb +91 -0
  115. data/lib/pry/output.rb +136 -0
  116. data/lib/pry/pager.rb +227 -68
  117. data/lib/pry/prompt.rb +214 -0
  118. data/lib/pry/pry_class.rb +216 -289
  119. data/lib/pry/pry_instance.rb +438 -500
  120. data/lib/pry/repl.rb +256 -0
  121. data/lib/pry/repl_file_loader.rb +34 -35
  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 +20 -0
  137. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +36 -43
  138. data/lib/pry/wrapped_module.rb +102 -103
  139. data/lib/pry.rb +135 -261
  140. metadata +94 -283
  141. data/.document +0 -2
  142. data/.gitignore +0 -16
  143. data/.travis.yml +0 -21
  144. data/.yardopts +0 -1
  145. data/CHANGELOG +0 -534
  146. data/CONTRIBUTORS +0 -55
  147. data/Gemfile +0 -9
  148. data/Guardfile +0 -62
  149. data/README.markdown +0 -400
  150. data/Rakefile +0 -140
  151. data/TODO +0 -117
  152. data/lib/pry/commands/disabled_commands.rb +0 -2
  153. data/lib/pry/commands/gem_cd.rb +0 -26
  154. data/lib/pry/commands/gem_install.rb +0 -29
  155. data/lib/pry/commands/gem_list.rb +0 -33
  156. data/lib/pry/commands/gem_open.rb +0 -29
  157. data/lib/pry/commands/gist.rb +0 -102
  158. data/lib/pry/commands/install_command.rb +0 -51
  159. data/lib/pry/commands/simple_prompt.rb +0 -22
  160. data/lib/pry/commands.rb +0 -6
  161. data/lib/pry/completion.rb +0 -304
  162. data/lib/pry/custom_completions.rb +0 -6
  163. data/lib/pry/history_array.rb +0 -116
  164. data/lib/pry/plugins.rb +0 -103
  165. data/lib/pry/rbx_method.rb +0 -13
  166. data/lib/pry/rbx_path.rb +0 -22
  167. data/lib/pry/rubygem.rb +0 -74
  168. data/lib/pry/terminal.rb +0 -78
  169. data/lib/pry/test/helper.rb +0 -185
  170. data/man/pry.1 +0 -195
  171. data/man/pry.1.html +0 -204
  172. data/man/pry.1.ronn +0 -141
  173. data/pry.gemspec +0 -30
  174. data/spec/Procfile +0 -3
  175. data/spec/cli_spec.rb +0 -78
  176. data/spec/code_object_spec.rb +0 -277
  177. data/spec/code_spec.rb +0 -219
  178. data/spec/command_helpers_spec.rb +0 -29
  179. data/spec/command_integration_spec.rb +0 -644
  180. data/spec/command_set_spec.rb +0 -627
  181. data/spec/command_spec.rb +0 -821
  182. data/spec/commands/amend_line_spec.rb +0 -247
  183. data/spec/commands/bang_spec.rb +0 -19
  184. data/spec/commands/cat_spec.rb +0 -164
  185. data/spec/commands/cd_spec.rb +0 -250
  186. data/spec/commands/disable_pry_spec.rb +0 -25
  187. data/spec/commands/edit_spec.rb +0 -727
  188. data/spec/commands/exit_all_spec.rb +0 -34
  189. data/spec/commands/exit_program_spec.rb +0 -19
  190. data/spec/commands/exit_spec.rb +0 -34
  191. data/spec/commands/find_method_spec.rb +0 -70
  192. data/spec/commands/gem_list_spec.rb +0 -26
  193. data/spec/commands/gist_spec.rb +0 -79
  194. data/spec/commands/help_spec.rb +0 -56
  195. data/spec/commands/hist_spec.rb +0 -181
  196. data/spec/commands/jump_to_spec.rb +0 -15
  197. data/spec/commands/ls_spec.rb +0 -181
  198. data/spec/commands/play_spec.rb +0 -140
  199. data/spec/commands/raise_up_spec.rb +0 -56
  200. data/spec/commands/save_file_spec.rb +0 -177
  201. data/spec/commands/show_doc_spec.rb +0 -510
  202. data/spec/commands/show_input_spec.rb +0 -17
  203. data/spec/commands/show_source_spec.rb +0 -782
  204. data/spec/commands/whereami_spec.rb +0 -203
  205. data/spec/completion_spec.rb +0 -239
  206. data/spec/control_d_handler_spec.rb +0 -58
  207. data/spec/documentation_helper_spec.rb +0 -73
  208. data/spec/editor_spec.rb +0 -79
  209. data/spec/exception_whitelist_spec.rb +0 -21
  210. data/spec/fixtures/candidate_helper1.rb +0 -11
  211. data/spec/fixtures/candidate_helper2.rb +0 -8
  212. data/spec/fixtures/example.erb +0 -5
  213. data/spec/fixtures/example_nesting.rb +0 -33
  214. data/spec/fixtures/show_source_doc_examples.rb +0 -15
  215. data/spec/fixtures/testrc +0 -2
  216. data/spec/fixtures/testrcbad +0 -2
  217. data/spec/fixtures/whereami_helper.rb +0 -6
  218. data/spec/helper.rb +0 -34
  219. data/spec/helpers/bacon.rb +0 -86
  220. data/spec/helpers/mock_pry.rb +0 -43
  221. data/spec/helpers/table_spec.rb +0 -105
  222. data/spec/history_array_spec.rb +0 -67
  223. data/spec/hooks_spec.rb +0 -522
  224. data/spec/indent_spec.rb +0 -301
  225. data/spec/input_stack_spec.rb +0 -90
  226. data/spec/method_spec.rb +0 -482
  227. data/spec/prompt_spec.rb +0 -60
  228. data/spec/pry_defaults_spec.rb +0 -419
  229. data/spec/pry_history_spec.rb +0 -99
  230. data/spec/pry_output_spec.rb +0 -95
  231. data/spec/pry_spec.rb +0 -504
  232. data/spec/run_command_spec.rb +0 -25
  233. data/spec/sticky_locals_spec.rb +0 -157
  234. data/spec/syntax_checking_spec.rb +0 -81
  235. data/spec/wrapped_module_spec.rb +0 -261
  236. data/wiki/Customizing-pry.md +0 -397
  237. data/wiki/Home.md +0 -4
@@ -1,4 +1,7 @@
1
- require "pry/indent"
1
+ # frozen_string_literal: true
2
+
3
+ require 'method_source'
4
+ require 'ostruct'
2
5
 
3
6
  ##
4
7
  # Pry is a powerful alternative to the standard IRB shell for Ruby. It
@@ -17,156 +20,187 @@ require "pry/indent"
17
20
  # This will show a list of available commands and their usage. For more
18
21
  # information about Pry you can refer to the following resources:
19
22
  #
20
- # * http://pry.github.com/
23
+ # * https://pry.github.io
21
24
  # * https://github.com/pry/pry
22
25
  # * the IRC channel, which is #pry on the Freenode network
23
26
  #
24
- class Pry
25
27
 
26
- attr_accessor :input
27
- attr_accessor :output
28
- attr_accessor :commands
29
- attr_accessor :print
30
- attr_accessor :exception_handler
31
- attr_accessor :input_stack
32
- attr_accessor :quiet
33
- alias :quiet? :quiet
34
-
35
- attr_accessor :custom_completions
28
+ # rubocop:disable Metrics/ClassLength
29
+ class Pry
30
+ extend Pry::Forwardable
36
31
 
37
32
  attr_accessor :binding_stack
38
-
33
+ attr_accessor :custom_completions
34
+ attr_accessor :eval_string
35
+ attr_accessor :backtrace
36
+ attr_accessor :suppress_output
39
37
  attr_accessor :last_result
40
38
  attr_accessor :last_file
41
39
  attr_accessor :last_dir
42
40
 
43
41
  attr_reader :last_exception
42
+ attr_reader :exit_value
43
+
44
+ # @since v0.12.0
45
+ attr_reader :input_ring
46
+
47
+ # @since v0.12.0
48
+ attr_reader :output_ring
49
+
50
+ attr_reader :config
51
+
52
+ def_delegators(
53
+ :@config, :input, :input=, :output, :output=, :commands,
54
+ :commands=, :print, :print=, :exception_handler, :exception_handler=,
55
+ :hooks, :hooks=, :color, :color=, :pager, :pager=, :editor, :editor=,
56
+ :memory_size, :memory_size=, :extra_sticky_locals, :extra_sticky_locals=
57
+ )
58
+
59
+ EMPTY_COMPLETIONS = [].freeze
60
+
61
+ # Create a new {Pry} instance.
62
+ # @param [Hash] options
63
+ # @option options [#readline] :input
64
+ # The object to use for input.
65
+ # @option options [#puts] :output
66
+ # The object to use for output.
67
+ # @option options [Pry::CommandBase] :commands
68
+ # The object to use for commands.
69
+ # @option options [Hash] :hooks
70
+ # The defined hook Procs.
71
+ # @option options [Pry::Prompt] :prompt
72
+ # The array of Procs to use for prompts.
73
+ # @option options [Proc] :print
74
+ # The Proc to use for printing return values.
75
+ # @option options [Boolean] :quiet
76
+ # Omit the `whereami` banner when starting.
77
+ # @option options [Array<String>] :backtrace
78
+ # The backtrace of the session's `binding.pry` line, if applicable.
79
+ # @option options [Object] :target
80
+ # The initial context for this session.
81
+ def initialize(options = {})
82
+ @binding_stack = []
83
+ @indent = Pry::Indent.new(self)
84
+ @eval_string = ''.dup
85
+ @backtrace = options.delete(:backtrace) || caller
86
+ target = options.delete(:target)
87
+ @config = self.class.config.merge(options)
88
+ push_prompt(config.prompt)
89
+ @input_ring = Pry::Ring.new(config.memory_size)
90
+ @output_ring = Pry::Ring.new(config.memory_size)
91
+ @custom_completions = config.command_completions
92
+ set_last_result nil
93
+ @input_ring << nil
94
+ push_initial_binding(target)
95
+ exec_hook(:when_started, target, options, self)
96
+ @prompt_warn = false
97
+ end
44
98
 
45
- attr_reader :input_array
46
- attr_reader :output_array
47
-
48
- attr_accessor :backtrace
49
-
50
- attr_accessor :extra_sticky_locals
51
-
52
- attr_accessor :suppress_output
53
-
54
- # This is exposed via Pry::Command#state.
55
- attr_reader :command_state
56
-
57
- # Special treatment for hooks as we want to alert people of the
58
- # changed API
59
- attr_reader :hooks
99
+ # This is the prompt at the top of the prompt stack.
100
+ # @return [Pry::Prompt] the current prompt
101
+ def prompt
102
+ prompt_stack.last
103
+ end
60
104
 
61
- # FIXME:
62
- # This is a hack to alert people of the new API.
63
- # @param [Pry::Hooks] v Only accept `Pry::Hooks` now!
64
- def hooks=(v)
65
- if v.is_a?(Hash)
66
- warn "Hash-based hooks are now deprecated! Use a `Pry::Hooks` object instead! http://rubydoc.info/github/pry/pry/master/Pry/Hooks"
67
- @hooks = Pry::Hooks.from_hash(v)
105
+ # Sets the Pry prompt.
106
+ # @param [Pry::Prompt] new_prompt
107
+ # @return [void]
108
+ def prompt=(new_prompt)
109
+ if prompt_stack.empty?
110
+ push_prompt new_prompt
68
111
  else
69
- @hooks = v
112
+ prompt_stack[-1] = new_prompt
70
113
  end
71
114
  end
72
115
 
73
- # Create a new `Pry` object.
74
- # @param [Hash] options The optional configuration parameters.
75
- # @option options [#readline] :input The object to use for input.
76
- # @option options [#puts] :output The object to use for output.
77
- # @option options [Pry::CommandBase] :commands The object to use for commands.
78
- # @option options [Hash] :hooks The defined hook Procs
79
- # @option options [Array<Proc>] :prompt The array of Procs to use for the prompts.
80
- # @option options [Proc] :print The Proc to use for the 'print'
81
- # @option options [Boolean] :quiet If true, omit the whereami banner when starting.
82
- # component of the REPL. (see print.rb)
83
- def initialize(options={})
84
- refresh(options)
85
-
86
- @binding_stack = []
87
- @indent = Pry::Indent.new
88
- @command_state = {}
89
- end
90
-
91
- # Refresh the Pry instance settings from the Pry class.
92
- # Allows options to be specified to override settings from Pry class.
93
- # @param [Hash] options The options to override Pry class settings
94
- # for this instance.
95
- def refresh(options={})
96
- defaults = {}
97
- attributes = [
98
- :input, :output, :commands, :print, :quiet,
99
- :exception_handler, :hooks, :custom_completions,
100
- :prompt, :memory_size, :extra_sticky_locals
101
- ]
102
-
103
- attributes.each do |attribute|
104
- defaults[attribute] = Pry.send attribute
105
- end
106
-
107
- defaults[:input_stack] = Pry.input_stack.dup
108
-
109
- defaults.merge!(options).each do |key, value|
110
- send("#{key}=", value) if respond_to?("#{key}=")
111
- end
112
-
113
- true
116
+ # Initialize this instance by pushing its initial context into the binding
117
+ # stack. If no target is given, start at the top level.
118
+ def push_initial_binding(target = nil)
119
+ push_binding(target || Pry.toplevel_binding)
114
120
  end
115
121
 
116
122
  # The currently active `Binding`.
117
123
  # @return [Binding] The currently active `Binding` for the session.
118
- def current_context
124
+ def current_binding
119
125
  binding_stack.last
120
126
  end
127
+ alias current_context current_binding # support previous API
128
+
129
+ # Push a binding for the given object onto the stack. If this instance is
130
+ # currently stopped, mark it as usable again.
131
+ def push_binding(object)
132
+ @stopped = false
133
+ binding_stack << Pry.binding_for(object)
134
+ end
121
135
 
122
- # The current prompt.
123
- # This is the prompt at the top of the prompt stack.
124
136
  #
125
- # @example
126
- # self.prompt = Pry::SIMPLE_PROMPT
127
- # self.prompt # => Pry::SIMPLE_PROMPT
137
+ # Generate completions.
128
138
  #
129
- # @return [Array<Proc>] Current prompt.
130
- def prompt
131
- prompt_stack.last
132
- end
139
+ # @param [String] str
140
+ # What the user has typed so far
141
+ #
142
+ # @return [Array<String>]
143
+ # Possible completions
144
+ #
145
+ def complete(str)
146
+ return EMPTY_COMPLETIONS unless config.completer
133
147
 
134
- def prompt=(new_prompt)
135
- if prompt_stack.empty?
136
- push_prompt new_prompt
137
- else
138
- prompt_stack[-1] = new_prompt
148
+ Pry.critical_section do
149
+ completer = config.completer.new(config.input, self)
150
+ completer.call(
151
+ str,
152
+ target: current_binding,
153
+ custom_completions: custom_completions.call.push(*sticky_locals.keys)
154
+ )
139
155
  end
140
156
  end
141
157
 
158
+ #
142
159
  # Injects a local variable into the provided binding.
143
- # @param [String] name The name of the local to inject.
144
- # @param [Object] value The value to set the local to.
145
- # @param [Binding] b The binding to set the local on.
146
- # @return [Object] The value the local was set to.
147
- def inject_local(name, value, b)
148
- Pry.current[:pry_local] = value.is_a?(Proc) ? value.call : value
149
- b.eval("#{name} = ::Pry.current[:pry_local]")
150
- ensure
151
- Pry.current[:pry_local] = nil
160
+ #
161
+ # @param [String] name
162
+ # The name of the local to inject.
163
+ #
164
+ # @param [Object] value
165
+ # The value to set the local to.
166
+ #
167
+ # @param [Binding] binding
168
+ # The binding to set the local on.
169
+ #
170
+ # @return [Object]
171
+ # The value the local was set to.
172
+ #
173
+ def inject_local(name, value, binding)
174
+ value = value.is_a?(Proc) ? value.call : value
175
+ if binding.respond_to?(:local_variable_set)
176
+ binding.local_variable_set name, value
177
+ else # < 2.1
178
+ begin
179
+ Pry.current[:pry_local] = value
180
+ binding.eval "#{name} = ::Pry.current[:pry_local]"
181
+ ensure
182
+ Pry.current[:pry_local] = nil
183
+ end
184
+ end
152
185
  end
153
186
 
187
+ undef :memory_size if method_defined? :memory_size
154
188
  # @return [Integer] The maximum amount of objects remembered by the inp and
155
189
  # out arrays. Defaults to 100.
156
190
  def memory_size
157
- @output_array.max_size
191
+ @output_ring.max_size
158
192
  end
159
193
 
194
+ undef :memory_size= if method_defined? :memory_size=
160
195
  def memory_size=(size)
161
- @input_array = Pry::HistoryArray.new(size)
162
- @output_array = Pry::HistoryArray.new(size)
196
+ @input_ring = Pry::Ring.new(size)
197
+ @output_ring = Pry::Ring.new(size)
163
198
  end
164
199
 
165
- # Inject all the sticky locals into the `target` binding.
166
- # @param [Binding] target
167
- def inject_sticky_locals(target)
200
+ # Inject all the sticky locals into the current binding.
201
+ def inject_sticky_locals!
168
202
  sticky_locals.each_pair do |name, value|
169
- inject_local(name, value, target)
203
+ inject_local(name, value, current_binding)
170
204
  end
171
205
  end
172
206
 
@@ -176,157 +210,85 @@ class Pry
176
210
  # @yield The block that defines the content of the local. The local
177
211
  # will be refreshed at each tick of the repl loop.
178
212
  def add_sticky_local(name, &block)
179
- sticky_locals[name] = block
213
+ config.extra_sticky_locals[name] = block
180
214
  end
181
215
 
182
- # @return [Hash] The currently defined sticky locals.
183
216
  def sticky_locals
184
- @sticky_locals ||= {
185
- :_in_ => proc { @input_array },
186
- :_out_ => proc { @output_array },
187
- :_pry_ => self,
188
- :_ex_ => proc { last_exception },
189
- :_file_ => proc { last_file },
190
- :_dir_ => proc { last_dir },
191
- :_ => proc { last_result },
192
- :__ => proc { @output_array[-2] }
193
- }.merge(extra_sticky_locals)
217
+ {
218
+ _in_: input_ring,
219
+ _out_: output_ring,
220
+ pry_instance: self,
221
+ _ex_: last_exception && last_exception.wrapped_exception,
222
+ _file_: last_file,
223
+ _dir_: last_dir,
224
+ _: proc { last_result },
225
+ __: proc { output_ring[-2] }
226
+ }.merge(config.extra_sticky_locals)
194
227
  end
195
228
 
196
- # Initialize the repl session.
197
- # @param [Binding] target The target binding for the session.
198
- def repl_prologue(target)
199
- exec_hook :before_session, output, target, self
200
- set_last_result(nil, target)
201
-
202
- @input_array << nil # add empty input so _in_ and _out_ match
203
-
204
- binding_stack.push target
229
+ # Reset the current eval string. If the user has entered part of a multiline
230
+ # expression, this discards that input.
231
+ def reset_eval_string
232
+ @eval_string = ''.dup
205
233
  end
206
234
 
207
- # Clean-up after the repl session.
208
- # @param [Binding] target The target binding for the session.
209
- def repl_epilogue(target)
210
- exec_hook :after_session, output, target, self
211
-
212
- binding_stack.pop
213
- Pry.save_history if Pry.config.history.should_save
214
- end
215
-
216
- # Start a read-eval-print-loop.
217
- # If no parameter is given, default to top-level (main).
218
- # @param [Object, Binding] target The receiver of the Pry session
219
- # @return [Object] The target of the Pry session or an explictly given
220
- # return value. If given return value is `nil` or no return value
221
- # is specified then `target` will be returned.
222
- # @example
223
- # Pry.new.repl(Object.new)
224
- def repl(target=TOPLEVEL_BINDING)
225
- target = Pry.binding_for(target)
226
-
227
- repl_prologue(target)
228
-
229
- break_data = nil
235
+ # Pass a line of input to Pry.
236
+ #
237
+ # This is the equivalent of `Binding#eval` but with extra Pry!
238
+ #
239
+ # In particular:
240
+ # 1. Pry commands will be executed immediately if the line matches.
241
+ # 2. Partial lines of input will be queued up until a complete expression has
242
+ # been accepted.
243
+ # 3. Output is written to `#output` in pretty colours, not returned.
244
+ #
245
+ # Once this method has raised an exception or returned false, this instance
246
+ # is no longer usable. {#exit_value} will return the session's breakout
247
+ # value if applicable.
248
+ #
249
+ # @param [String?] line The line of input; `nil` if the user types `<Ctrl-D>`
250
+ # @option options [Boolean] :generated Whether this line was generated automatically.
251
+ # Generated lines are not stored in history.
252
+ # @return [Boolean] Is Pry ready to accept more input?
253
+ # @raise [Exception] If the user uses the `raise-up` command, this method
254
+ # will raise that exception.
255
+ def eval(line, options = {})
256
+ return false if @stopped
257
+
258
+ exit_value = nil
230
259
  exception = catch(:raise_up) do
231
- break_data = catch(:breakout) do
232
- loop do
233
- throw(:breakout) if binding_stack.empty?
234
- rep(binding_stack.last)
235
- end
260
+ exit_value = catch(:breakout) do
261
+ handle_line(line, options)
262
+ # We use 'return !@stopped' here instead of 'return true' so that if
263
+ # handle_line has stopped this pry instance (e.g. by opening pry_instance.repl and
264
+ # then popping all the bindings) we still exit immediately.
265
+ return !@stopped
236
266
  end
237
267
  exception = false
238
268
  end
239
269
 
270
+ @stopped = true
271
+ @exit_value = exit_value
272
+
273
+ # TODO: make this configurable?
240
274
  raise exception if exception
241
275
 
242
- break_data
243
- ensure
244
- repl_epilogue(target)
276
+ false
245
277
  end
246
278
 
247
- # Perform a read-eval-print.
248
- # If no parameter is given, default to top-level (main).
249
- # @param [Object, Binding] target The receiver of the read-eval-print
250
- # @example
251
- # Pry.new.rep(Object.new)
252
- def rep(target=TOPLEVEL_BINDING)
253
- target = Pry.binding_for(target)
254
- result = re(target)
255
-
256
- Pry.critical_section do
257
- show_result(result)
258
- end
279
+ # Potentially deprecated. Use `Pry::REPL.new(pry, :target => target).start`
280
+ # (If nested sessions are going to exist, this method is fine, but a goal is
281
+ # to come up with an alternative to nested sessions altogether.)
282
+ def repl(target = nil)
283
+ Pry::REPL.new(self, target: target).start
259
284
  end
260
285
 
261
- # Perform a read-eval
262
- # If no parameter is given, default to top-level (main).
263
- # @param [Object, Binding] target The receiver of the read-eval-print
264
- # @return [Object] The result of the eval or an `Exception` object in case of
265
- # error. In the latter case, you can check whether the exception was raised
266
- # or is just the result of the expression using #last_result_is_exception?
267
- # @example
268
- # Pry.new.re(Object.new)
269
- def re(target=TOPLEVEL_BINDING)
270
- target = Pry.binding_for(target)
271
-
272
- # It's not actually redundant to inject them continually as we may have
273
- # moved into the scope of a new Binding (e.g the user typed `cd`).
274
- inject_sticky_locals(target)
275
-
276
- code = r(target)
277
-
278
- evaluate_ruby(code, target)
279
- rescue RescuableException => e
280
- self.last_exception = e
281
- e
282
- end
283
-
284
- # Perform a read.
285
- # If no parameter is given, default to top-level (main).
286
- # This is a multi-line read; so the read continues until a valid
287
- # Ruby expression is received.
288
- # Pry commands are also accepted here and operate on the target.
289
- # @param [Object, Binding] target The receiver of the read.
290
- # @param [String] eval_string Optionally Prime `eval_string` with a start value.
291
- # @return [String] The Ruby expression.
292
- # @example
293
- # Pry.new.r(Object.new)
294
- def r(target=TOPLEVEL_BINDING, eval_string="")
295
- target = Pry.binding_for(target)
296
- @suppress_output = false
297
-
298
- loop do
299
- begin
300
- # eval_string will probably be mutated by this method
301
- retrieve_line(eval_string, target)
302
- rescue CommandError, Slop::InvalidOptionError, MethodSource::SourceNotFoundError => e
303
- Pry.last_internal_error = e
304
- output.puts "Error: #{e.message}"
305
- end
306
-
307
- begin
308
- break if Pry::Code.complete_expression?(eval_string)
309
- rescue SyntaxError => e
310
- exception_handler.call(output, e.extend(UserError), self)
311
- eval_string = ""
312
- end
313
- end
314
-
315
- if eval_string =~ /;\Z/ || eval_string.empty? || eval_string =~ /\A *#.*\n\z/
316
- @suppress_output = true
317
- end
318
-
319
- exec_hook :after_read, eval_string, self
320
- eval_string
321
- end
322
-
323
- def evaluate_ruby(code, target = binding_stack.last)
324
- target = Pry.binding_for(target)
325
- inject_sticky_locals(target)
286
+ def evaluate_ruby(code)
287
+ inject_sticky_locals!
326
288
  exec_hook :before_eval, code, self
327
289
 
328
- result = target.eval(code, Pry.eval_path, Pry.current_line)
329
- set_last_result(result, target, code)
290
+ result = current_binding.eval(code, Pry.eval_path, Pry.current_line)
291
+ set_last_result(result, code)
330
292
  ensure
331
293
  update_input_history(code)
332
294
  exec_hook :after_eval, result, self
@@ -337,115 +299,40 @@ class Pry
337
299
  if last_result_is_exception?
338
300
  exception_handler.call(output, result, self)
339
301
  elsif should_print?
340
- print.call(output, result)
341
- else
342
- # nothin'
302
+ print.call(output, result, self)
343
303
  end
344
304
  rescue RescuableException => e
345
305
  # Being uber-paranoid here, given that this exception arose because we couldn't
346
306
  # serialize something in the user's program, let's not assume we can serialize
347
307
  # the exception either.
348
308
  begin
349
- output.puts "(pry) output error: #{e.inspect}"
350
- rescue RescuableException => e
309
+ output.puts "(pry) output error: #{e.inspect}\n#{e.backtrace.join("\n")}"
310
+ rescue RescuableException
351
311
  if last_result_is_exception?
352
312
  output.puts "(pry) output error: failed to show exception"
353
313
  else
354
314
  output.puts "(pry) output error: failed to show result"
355
315
  end
356
316
  end
357
- end
358
-
359
- def should_force_encoding?(eval_string, val)
360
- eval_string.empty? && val.respond_to?(:encoding) && val.encoding != eval_string.encoding
361
- end
362
- private :should_force_encoding?
363
-
364
- # Read and process a line of input -- check for ^D, determine which prompt to
365
- # use, rewrite the indentation if `Pry.config.auto_indent` is enabled, and,
366
- # if the line is a command, process it and alter the eval_string accordingly.
367
- # This method should not need to be invoked directly.
368
- #
369
- # @param [String] eval_string The cumulative lines of input.
370
- # @param [Binding] target The target of the session.
371
- # @return [String] The line received.
372
- def retrieve_line(eval_string, target)
373
- @indent.reset if eval_string.empty?
374
-
375
- current_prompt = select_prompt(eval_string, target)
376
- completion_proc = Pry.config.completer.build_completion_proc(target, self,
377
- instance_eval(&custom_completions))
378
-
379
- safe_completion_proc = proc{ |*a| Pry.critical_section{ completion_proc.call(*a) } }
380
-
381
- indentation = Pry.config.auto_indent ? @indent.current_prefix : ''
382
-
383
- begin
384
- val = readline("#{current_prompt}#{indentation}", safe_completion_proc)
385
-
386
- # Handle <Ctrl+C> like Bash, empty the current input buffer but do not quit.
387
- # This is only for ruby-1.9; other versions of ruby do not let you send Interrupt
388
- # from within Readline.
389
- rescue Interrupt
390
- output.puts ""
391
- eval_string.replace("")
392
- return
393
- end
394
-
395
- # invoke handler if we receive EOF character (^D)
396
- if !val
397
- output.puts ""
398
- Pry.config.control_d_handler.call(eval_string, self)
399
- return
400
- end
401
-
402
- # Change the eval_string into the input encoding (Issue 284)
403
- # TODO: This wouldn't be necessary if the eval_string was constructed from
404
- # input strings only.
405
- if should_force_encoding?(eval_string, val)
406
- eval_string.force_encoding(val.encoding)
407
- end
408
-
409
- if Pry.config.auto_indent && !input.is_a?(StringIO)
410
- original_val = "#{indentation}#{val}"
411
- indented_val = @indent.indent(val)
412
-
413
- if output.tty? && Pry::Helpers::BaseHelpers.use_ansi_codes? && Pry.config.correct_indent
414
- output.print @indent.correct_indentation(current_prompt, indented_val, original_val.length - indented_val.length)
415
- output.flush
416
- end
417
- else
418
- indented_val = val
419
- end
420
-
421
- # Check this before processing the line, because a command might change
422
- # Pry's input.
423
- interactive = !input.is_a?(StringIO)
424
-
425
- begin
426
- if !process_command(val, eval_string, target)
427
- eval_string << "#{indented_val.chomp}\n" unless val.empty?
428
- end
429
- ensure
430
- Pry.history << indented_val if interactive
431
- end
317
+ ensure
318
+ output.flush if output.respond_to?(:flush)
432
319
  end
433
320
 
434
321
  # If the given line is a valid command, process it in the context of the
435
- # current `eval_string` and context.
436
- # This method should not need to be invoked directly.
322
+ # current `eval_string` and binding.
437
323
  # @param [String] val The line to process.
438
- # @param [String] eval_string The cumulative lines of input.
439
- # @param [Binding] target The target of the Pry session.
440
324
  # @return [Boolean] `true` if `val` is a command, `false` otherwise
441
- def process_command(val, eval_string = '', target = binding_stack.last)
325
+ def process_command(val)
326
+ val = val.lstrip if /^\s\S/ !~ val
442
327
  val = val.chomp
443
- result = commands.process_line(val, {
444
- :target => target,
445
- :output => output,
446
- :eval_string => eval_string,
447
- :pry_instance => self
448
- })
328
+ result = commands.process_line(
329
+ val,
330
+ target: current_binding,
331
+ output: output,
332
+ eval_string: @eval_string,
333
+ pry_instance: self,
334
+ hooks: hooks
335
+ )
449
336
 
450
337
  # set a temporary (just so we can inject the value we want into eval_string)
451
338
  Pry.current[:pry_cmd_result] = result
@@ -454,11 +341,11 @@ class Pry
454
341
  # command was matched and invoked then `result.command?` returns true,
455
342
  # otherwise it returns false.
456
343
  if result.command?
457
- if !result.void_command?
344
+ unless result.void_command?
458
345
  # the command that was invoked was non-void (had a return value) and so we make
459
346
  # the value of the current expression equal to the return value
460
347
  # of the command.
461
- eval_string.replace "::Pry.current[:pry_cmd_result].retval\n"
348
+ @eval_string = "::Pry.current[:pry_cmd_result].retval\n"
462
349
  end
463
350
  true
464
351
  else
@@ -466,19 +353,32 @@ class Pry
466
353
  end
467
354
  end
468
355
 
356
+ # Same as process_command, but outputs exceptions to `#output` instead of
357
+ # raising.
358
+ # @param [String] val The line to process.
359
+ # @return [Boolean] `true` if `val` is a command, `false` otherwise
360
+ def process_command_safely(val)
361
+ process_command(val)
362
+ rescue CommandError,
363
+ Pry::Slop::InvalidOptionError,
364
+ MethodSource::SourceNotFoundError => e
365
+ Pry.last_internal_error = e
366
+ output.puts "Error: #{e.message}"
367
+ true
368
+ end
369
+
469
370
  # Run the specified command.
470
371
  # @param [String] val The command (and its params) to execute.
471
- # @param [String] eval_string The current input buffer.
472
- # @param [Binding] target The binding to use..
473
372
  # @return [Pry::Command::VOID_VALUE]
474
373
  # @example
475
374
  # pry_instance.run_command("ls -m")
476
- def run_command(val, eval_string = "", target = binding_stack.last)
477
- commands.process_line(val,
478
- :eval_string => eval_string,
479
- :target => target,
480
- :pry_instance => self,
481
- :output => output
375
+ def run_command(val)
376
+ commands.process_line(
377
+ val,
378
+ eval_string: @eval_string,
379
+ target: current_binding,
380
+ pry_instance: self,
381
+ output: output
482
382
  )
483
383
  Pry::Command::VOID_VALUE
484
384
  end
@@ -496,8 +396,8 @@ class Pry
496
396
  hooks.exec_hook(name, *args, &block).tap do
497
397
  hooks.errors[e_before..-1].each do |e|
498
398
  output.puts "#{name} hook failed: #{e.class}: #{e.message}"
499
- output.puts "#{e.backtrace.first}"
500
- output.puts "(see _pry_.hooks.errors to debug)"
399
+ output.puts e.backtrace.first.to_s
400
+ output.puts "(see pry_instance.hooks.errors to debug)"
501
401
  end
502
402
  end
503
403
  end
@@ -505,48 +405,33 @@ class Pry
505
405
  # Set the last result of an eval.
506
406
  # This method should not need to be invoked directly.
507
407
  # @param [Object] result The result.
508
- # @param [Binding] target The binding to set `_` on.
509
408
  # @param [String] code The code that was run.
510
- def set_last_result(result, target, code="")
409
+ def set_last_result(result, code = "")
511
410
  @last_result_is_exception = false
512
- @output_array << result
411
+ @output_ring << result
513
412
 
514
413
  self.last_result = result unless code =~ /\A\s*\z/
515
414
  end
516
415
 
517
416
  # Set the last exception for a session.
518
- # @param [Exception] ex
519
- def last_exception=(ex)
520
- class << ex
521
- attr_accessor :file, :line, :bt_index
522
- def bt_source_location_for(index)
523
- backtrace[index] =~ /(.*):(\d+)/
524
- [$1, $2.to_i]
525
- end
526
-
527
- def inc_bt_index
528
- @bt_index = (@bt_index + 1) % backtrace.size
529
- end
530
- end
531
-
532
- ex.bt_index = 0
533
- ex.file, ex.line = ex.bt_source_location_for(0)
534
-
417
+ # @param [Exception] exception The last exception.
418
+ def last_exception=(exception)
535
419
  @last_result_is_exception = true
536
- @output_array << ex
537
- @last_exception = ex
420
+ last_exception = Pry::LastException.new(exception)
421
+ @output_ring << last_exception
422
+ @last_exception = last_exception
538
423
  end
539
424
 
540
425
  # Update Pry's internal state after evalling code.
541
426
  # This method should not need to be invoked directly.
542
427
  # @param [String] code The code we just eval'd
543
428
  def update_input_history(code)
544
- # Always push to the @input_array as the @output_array is always pushed to.
545
- @input_array << code
546
- if code
547
- Pry.line_buffer.push(*code.each_line)
548
- Pry.current_line += code.each_line.count
549
- end
429
+ # Always push to the @input_ring as the @output_ring is always pushed to.
430
+ @input_ring << code
431
+ return unless code
432
+
433
+ Pry.line_buffer.push(*code.each_line)
434
+ Pry.current_line += code.lines.count
550
435
  end
551
436
 
552
437
  # @return [Boolean] True if the last result is an exception that was raised,
@@ -556,84 +441,6 @@ class Pry
556
441
  @last_result_is_exception
557
442
  end
558
443
 
559
- # Manage switching of input objects on encountering EOFErrors
560
- def handle_read_errors
561
- should_retry = true
562
- exception_count = 0
563
- begin
564
- yield
565
- rescue EOFError
566
- if input_stack.empty?
567
- self.input = Pry.config.input
568
- if !should_retry
569
- output.puts "Error: Pry ran out of things to read from! Attempting to break out of REPL."
570
- throw(:breakout)
571
- end
572
- should_retry = false
573
- else
574
- self.input = input_stack.pop
575
- end
576
-
577
- retry
578
-
579
- # Interrupts are handled in r() because they need to tweak eval_string
580
- # TODO: Refactor this baby.
581
- rescue Interrupt
582
- raise
583
-
584
- # If we get a random error when trying to read a line we don't want to automatically
585
- # retry, as the user will see a lot of error messages scroll past and be unable to do
586
- # anything about it.
587
- rescue RescuableException => e
588
- puts "Error: #{e.message}"
589
- output.puts e.backtrace
590
- exception_count += 1
591
- if exception_count < 5
592
- retry
593
- end
594
- puts "FATAL: Pry failed to get user input using `#{input}`."
595
- puts "To fix this you may be able to pass input and output file descriptors to pry directly. e.g."
596
- puts " Pry.config.input = STDIN"
597
- puts " Pry.config.output = STDOUT"
598
- puts " binding.pry"
599
- throw(:breakout)
600
- end
601
- end
602
- private :handle_read_errors
603
-
604
- # Returns the next line of input to be used by the pry instance.
605
- # This method should not need to be invoked directly.
606
- # @param [String] current_prompt The prompt to use for input.
607
- # @return [String] The next line of input.
608
- def readline(current_prompt="> ", completion_proc=nil)
609
- handle_read_errors do
610
-
611
- if defined? Coolline and input.is_a? Coolline
612
- input.completion_proc = proc do |cool|
613
- completions = completion_proc.call cool.completed_word
614
- completions.compact
615
- end
616
- elsif input.respond_to? :completion_proc=
617
- input.completion_proc = completion_proc
618
- end
619
-
620
- if input == Readline
621
- if !$stdout.tty? && $stdin.tty? && !Pry::Helpers::BaseHelpers.windows?
622
- Readline.output = File.open('/dev/tty', 'w')
623
- end
624
- input.readline(current_prompt, false) # false since we'll add it manually
625
- elsif defined? Coolline and input.is_a? Coolline
626
- input.readline(current_prompt)
627
- else
628
- if input.method(:readline).arity == 1
629
- input.readline(current_prompt)
630
- else
631
- input.readline
632
- end
633
- end
634
- end
635
- end
636
-
637
444
  # Whether the print proc should be invoked.
638
445
  # Currently only invoked if the output is not suppressed.
639
446
  # @return [Boolean] Whether the print proc should be invoked.
@@ -642,34 +449,45 @@ class Pry
642
449
  end
643
450
 
644
451
  # Returns the appropriate prompt to use.
645
- # This method should not need to be invoked directly.
646
- # @param [String] eval_string The current input buffer.
647
- # @param [Binding] target The target Binding of the Pry session.
648
452
  # @return [String] The prompt.
649
- def select_prompt(eval_string, target)
650
- target_self = target.eval('self')
651
-
652
- open_token = @indent.open_delimiters.any? ? @indent.open_delimiters.last :
653
- @indent.stack.last
453
+ def select_prompt
454
+ object = current_binding.eval('self')
455
+ open_token = @indent.open_delimiters.last || @indent.stack.last
654
456
 
655
457
  c = OpenStruct.new(
656
- :object => target_self,
657
- :nesting_level => binding_stack.size - 1,
658
- :open_token => open_token,
659
- :session_line => Pry.history.session_line_count + 1,
660
- :history_line => Pry.history.history_line_count + 1,
661
- :expr_number => input_array.count,
662
- :_pry_ => self,
663
- :binding_stack => binding_stack,
664
- :input_array => input_array,
665
- :eval_string => eval_string,
666
- :cont => !eval_string.empty?)
458
+ object: object,
459
+ nesting_level: binding_stack.size - 1,
460
+ open_token: open_token,
461
+ session_line: Pry.history.session_line_count + 1,
462
+ history_line: Pry.history.history_line_count + 1,
463
+ expr_number: input_ring.count,
464
+ pry_instance: self,
465
+ binding_stack: binding_stack,
466
+ input_ring: input_ring,
467
+ eval_string: @eval_string,
468
+ cont: !@eval_string.empty?
469
+ )
667
470
 
668
471
  Pry.critical_section do
472
+ # If input buffer is empty, then use normal prompt. Otherwise use the wait
473
+ # prompt (indicating multi-line expression).
474
+ if prompt.is_a?(Pry::Prompt)
475
+ prompt_proc = eval_string.empty? ? prompt.wait_proc : prompt.incomplete_proc
476
+ return prompt_proc.call(c.object, c.nesting_level, c.pry_instance)
477
+ end
478
+
479
+ unless @prompt_warn
480
+ @prompt_warn = true
481
+ Kernel.warn(
482
+ "warning: setting prompt with help of " \
483
+ "`Pry.config.prompt = [proc {}, proc {}]` is deprecated. " \
484
+ "Use Pry::Prompt API instead"
485
+ )
486
+ end
487
+
669
488
  # If input buffer is empty then use normal prompt
670
489
  if eval_string.empty?
671
490
  generate_prompt(Array(prompt).first, c)
672
-
673
491
  # Otherwise use the wait prompt (indicating multi-line expression)
674
492
  else
675
493
  generate_prompt(Array(prompt).last, c)
@@ -677,48 +495,50 @@ class Pry
677
495
  end
678
496
  end
679
497
 
680
- def generate_prompt(prompt_proc, conf)
681
- if prompt_proc.arity == 1
682
- prompt_proc.call(conf)
683
- else
684
- prompt_proc.call(conf.object, conf.nesting_level, conf._pry_)
685
- end
686
- end
687
- private :generate_prompt
688
-
689
- # the array that the prompt stack is stored in
690
- def prompt_stack
691
- @prompt_stack ||= Array.new
692
- end
693
- private :prompt_stack
694
-
695
498
  # Pushes the current prompt onto a stack that it can be restored from later.
696
499
  # Use this if you wish to temporarily change the prompt.
697
- # @param [Array<Proc>] new_prompt
698
- # @return [Array<Proc>] new_prompt
500
+ #
699
501
  # @example
700
- # new_prompt = [ proc { '>' }, proc { '>>' } ]
701
- # push_prompt(new_prompt) # => new_prompt
502
+ # push_prompt(Pry::Prompt[:my_prompt])
503
+ #
504
+ # @param [Pry::Prompt] new_prompt
505
+ # @return [Pry::Prompt] new_prompt
702
506
  def push_prompt(new_prompt)
703
507
  prompt_stack.push new_prompt
704
508
  end
705
509
 
706
- # Pops the current prompt off of the prompt stack.
707
- # If the prompt you are popping is the last prompt, it will not be popped.
708
- # Use this to restore the previous prompt.
709
- # @return [Array<Proc>] Prompt being popped.
510
+ # Pops the current prompt off of the prompt stack. If the prompt you are
511
+ # popping is the last prompt, it will not be popped. Use this to restore the
512
+ # previous prompt.
513
+ #
710
514
  # @example
711
- # prompt1 = [ proc { '>' }, proc { '>>' } ]
712
- # prompt2 = [ proc { '$' }, proc { '>' } ]
713
- # pry = Pry.new :prompt => prompt1
714
- # pry.push_prompt(prompt2)
515
+ # pry = Pry.new(prompt: Pry::Prompt[:my_prompt1])
516
+ # pry.push_prompt(Pry::Prompt[:my_prompt2])
715
517
  # pry.pop_prompt # => prompt2
716
518
  # pry.pop_prompt # => prompt1
717
519
  # pry.pop_prompt # => prompt1
520
+ #
521
+ # @return [Pry::Prompt] the prompt being popped
718
522
  def pop_prompt
719
523
  prompt_stack.size > 1 ? prompt_stack.pop : prompt
720
524
  end
721
525
 
526
+ undef :pager if method_defined? :pager
527
+ # Returns the currently configured pager
528
+ # @example
529
+ # pry_instance.pager.page text
530
+ def pager
531
+ Pry::Pager.new(self)
532
+ end
533
+
534
+ undef :output if method_defined? :output
535
+ # Returns an output device
536
+ # @example
537
+ # pry_instance.output.puts "ohai!"
538
+ def output
539
+ Pry::Output.new(self)
540
+ end
541
+
722
542
  # Raise an exception out of Pry.
723
543
  #
724
544
  # See Kernel#raise for documentation of parameters.
@@ -740,7 +560,7 @@ class Pry
740
560
  raise ArgumentError, "wrong number of arguments"
741
561
  elsif !args.first.respond_to?(:exception)
742
562
  raise TypeError, "exception class/object expected"
743
- elsif args.length === 1
563
+ elsif args.size == 1
744
564
  args.first.exception
745
565
  else
746
566
  args.first.exception(args[1])
@@ -748,7 +568,7 @@ class Pry
748
568
 
749
569
  raise TypeError, "exception object expected" unless exception.is_a? Exception
750
570
 
751
- exception.set_backtrace(args.length === 3 ? args[2] : caller(1))
571
+ exception.set_backtrace(args.size == 3 ? args[2] : caller(1))
752
572
 
753
573
  if force || binding_stack.one?
754
574
  binding_stack.clear
@@ -758,6 +578,124 @@ class Pry
758
578
  raise exception
759
579
  end
760
580
  end
761
- def raise_up(*args); raise_up_common(false, *args); end
762
- def raise_up!(*args); raise_up_common(true, *args); end
581
+
582
+ def raise_up(*args)
583
+ raise_up_common(false, *args)
584
+ end
585
+
586
+ def raise_up!(*args)
587
+ raise_up_common(true, *args)
588
+ end
589
+
590
+ # Convenience accessor for the `quiet` config key.
591
+ # @return [Boolean]
592
+ def quiet?
593
+ config.quiet
594
+ end
595
+
596
+ private
597
+
598
+ def handle_line(line, options)
599
+ if line.nil?
600
+ config.control_d_handler.call(self)
601
+ return
602
+ end
603
+
604
+ ensure_correct_encoding!(line)
605
+ Pry.history << line unless options[:generated]
606
+
607
+ @suppress_output = false
608
+ inject_sticky_locals!
609
+ begin
610
+ unless process_command_safely(line)
611
+ @eval_string += "#{line.chomp}\n" if !line.empty? || !@eval_string.empty?
612
+ end
613
+ rescue RescuableException => e
614
+ self.last_exception = e
615
+ result = e
616
+
617
+ Pry.critical_section do
618
+ show_result(result)
619
+ end
620
+ return
621
+ end
622
+
623
+ # This hook is supposed to be executed after each line of ruby code
624
+ # has been read (regardless of whether eval_string is yet a complete expression)
625
+ exec_hook :after_read, eval_string, self
626
+
627
+ begin
628
+ complete_expr = Pry::Code.complete_expression?(@eval_string)
629
+ rescue SyntaxError => e
630
+ output.puts e.message.gsub(/^.*syntax error, */, "SyntaxError: ")
631
+ reset_eval_string
632
+ end
633
+
634
+ if complete_expr
635
+ if @eval_string =~ /;\Z/ || @eval_string.empty? || @eval_string =~ /\A *#.*\n\z/
636
+ @suppress_output = true
637
+ end
638
+
639
+ # A bug in jruby makes java.lang.Exception not rescued by
640
+ # `rescue Pry::RescuableException` clause.
641
+ #
642
+ # * https://github.com/pry/pry/issues/854
643
+ # * https://jira.codehaus.org/browse/JRUBY-7100
644
+ #
645
+ # Until that gets fixed upstream, treat java.lang.Exception
646
+ # as an additional exception to be rescued explicitly.
647
+ #
648
+ # This workaround has a side effect: java exceptions specified
649
+ # in `Pry.config.unrescued_exceptions` are ignored.
650
+ jruby_exceptions = []
651
+ jruby_exceptions << Java::JavaLang::Exception if Helpers::Platform.jruby?
652
+
653
+ begin
654
+ # Reset eval string, in case we're evaluating Ruby that does something
655
+ # like open a nested REPL on this instance.
656
+ eval_string = @eval_string
657
+ reset_eval_string
658
+
659
+ result = evaluate_ruby(eval_string)
660
+ rescue RescuableException, *jruby_exceptions => e
661
+ # Eliminate following warning:
662
+ # warning: singleton on non-persistent Java type X
663
+ # (http://wiki.jruby.org/Persistence)
664
+ if Helpers::Platform.jruby? && e.class.respond_to?('__persistent__')
665
+ e.class.__persistent__ = true
666
+ end
667
+ self.last_exception = e
668
+ result = e
669
+ end
670
+
671
+ Pry.critical_section do
672
+ show_result(result)
673
+ end
674
+ end
675
+
676
+ throw(:breakout) if current_binding.nil?
677
+ end
678
+
679
+ # Force `eval_string` into the encoding of `val`. [Issue #284]
680
+ def ensure_correct_encoding!(val)
681
+ if @eval_string.empty? &&
682
+ val.respond_to?(:encoding) &&
683
+ val.encoding != @eval_string.encoding
684
+ @eval_string.force_encoding(val.encoding)
685
+ end
686
+ end
687
+
688
+ def generate_prompt(prompt_proc, conf)
689
+ if prompt_proc.arity == 1
690
+ prompt_proc.call(conf)
691
+ else
692
+ prompt_proc.call(conf.object, conf.nesting_level, conf.pry_instance)
693
+ end
694
+ end
695
+
696
+ # the array that the prompt stack is stored in
697
+ def prompt_stack
698
+ @prompt_stack ||= []
699
+ end
763
700
  end
701
+ # rubocop:enable Metrics/ClassLength