pry 0.7.3 → 0.8.0

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.
@@ -6,7 +6,7 @@ class Pry
6
6
 
7
7
  # Implements tab completion for Readline in Pry
8
8
  module InputCompleter
9
-
9
+
10
10
  if Readline.respond_to?("basic_word_break_characters=")
11
11
  Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
12
12
  end
@@ -0,0 +1,6 @@
1
+ class Pry
2
+
3
+ # This proc will be instance_eval's against the active Pry instance
4
+ DEFAULT_CUSTOM_COMPLETIONS = proc { commands.commands.keys }
5
+ FILE_COMPLETIONS = proc { commands.commands.keys + Dir.entries('.') }
6
+ end
data/lib/pry/hooks.rb CHANGED
@@ -2,18 +2,16 @@ class Pry
2
2
 
3
3
  # The default hooks - display messages when beginning and ending Pry sessions.
4
4
  DEFAULT_HOOKS = {
5
-
6
- :before_session => proc do |out, target|
7
- out.puts "Beginning Pry session for #{Pry.view_clip(target.eval('self'))}"
8
5
 
6
+ :before_session => proc do |out, target|
9
7
  # ensure we're actually in a method
10
8
  meth_name = target.eval('__method__')
11
9
  file = target.eval('__FILE__')
12
- if ![:__script__, nil, :__binding__, :__binding_impl__].include?(meth_name) && file !~ /(\(.*\))|<.*>/
13
- Pry.run_command "whereami", :output => out, :show_output => true, :context => target, :commands => Pry::Commands
10
+
11
+ # /unknown/ for rbx
12
+ if file !~ /(\(.*\))|<.*>/ && file !~ /__unknown__/ && file != "" && file != "-e"
13
+ Pry.run_command "whereami 5", :output => out, :show_output => true, :context => target, :commands => Pry::Commands
14
14
  end
15
15
  end,
16
-
17
- :after_session => proc { |out, target| out.puts "Ending Pry session for #{Pry.view_clip(target.eval('self'))}" }
18
16
  }
19
17
  end
data/lib/pry/print.rb CHANGED
@@ -1,19 +1,16 @@
1
1
  class Pry
2
-
3
- # The default print object - only show first line of backtrace and
4
- # prepend output with `=>`
5
2
  DEFAULT_PRINT = proc do |output, value|
6
- case value
7
- when Exception
8
- output.puts "#{value.class}: #{value.message}"
9
- output.puts "from #{value.backtrace.first}"
3
+ if Pry.color
4
+ output.puts "=> #{CodeRay.scan(Pry.view(value), :ruby).term}"
10
5
  else
11
- if Pry.color
12
- output.puts "=> #{CodeRay.scan(Pry.view(value), :ruby).term}"
13
- else
14
- output.puts "=> #{Pry.view(value)}"
15
- end
6
+ output.puts "=> #{Pry.view(value)}"
16
7
  end
17
8
  end
9
+
10
+ # Will only show the first line of the backtrace
11
+ DEFAULT_EXCEPTION_HANDLER = proc do |output, exception|
12
+ output.puts "#{exception.class}: #{exception.message}"
13
+ output.puts "from #{exception.backtrace.first}"
14
+ end
18
15
  end
19
-
16
+
data/lib/pry/prompts.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  class Pry
2
2
 
3
-
3
+
4
4
  # The default prompt; includes the target and nesting level
5
5
  DEFAULT_PROMPT = [
6
6
  proc do |target_self, nest_level|
@@ -11,7 +11,7 @@ class Pry
11
11
  "pry(#{Pry.view_clip(target_self)}):#{Pry.view_clip(nest_level)}> "
12
12
  end
13
13
  end,
14
-
14
+
15
15
  proc do |target_self, nest_level|
16
16
  if nest_level == 0
17
17
  "pry(#{Pry.view_clip(target_self)})* "
@@ -23,4 +23,9 @@ class Pry
23
23
 
24
24
  # A simple prompt - doesn't display target or nesting level
25
25
  SIMPLE_PROMPT = [proc { ">> " }, proc { ">* " }]
26
+
27
+ SHELL_PROMPT = [
28
+ proc { |target_self, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} $ " },
29
+ proc { |target_self, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} * " }
30
+ ]
26
31
  end
data/lib/pry/pry_class.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  # @author John Mair (banisterfiend)
2
2
  class Pry
3
3
 
4
- # The RC Files to load.
4
+ # The RC Files to load.
5
5
  RC_FILES = ["~/.pryrc"]
6
-
6
+
7
7
  # class accessors
8
8
  class << self
9
9
 
@@ -49,6 +49,10 @@ class Pry
49
49
  # Pry instances.
50
50
  attr_accessor :print
51
51
 
52
+ # @return [Proc] The Proc to use for printing exceptions by default by all
53
+ # Pry instances.
54
+ attr_accessor :exception_handler
55
+
52
56
  # Get/Set the Hash that defines Pry hooks used by default by all Pry
53
57
  # instances.
54
58
  # @return [Hash] The hooks used by default by all Pry instances.
@@ -57,6 +61,14 @@ class Pry
57
61
  # :after_session => proc { puts "goodbye" }
58
62
  attr_accessor :hooks
59
63
 
64
+
65
+ # Get/Set the Proc that defines extra Readline completions (on top
66
+ # of the ones defined for IRB).
67
+ # @return [Proc] The Proc that defines extra Readline completions (on top
68
+ # @example Add file names to completion list
69
+ # Pry.custom_completions = proc { Dir.entries('.') }
70
+ attr_accessor :custom_completions
71
+
60
72
  # Get the array of Procs to be used for the prompts by default by
61
73
  # all Pry instances.
62
74
  # @return [Array<Proc>] The array of Procs to be used for the
@@ -68,11 +80,15 @@ class Pry
68
80
  attr_accessor :cmd_ret_value
69
81
 
70
82
  # Determines whether colored output is enabled.
71
- # @return [Boolean]
83
+ # @return [Boolean]
72
84
  attr_accessor :color
73
85
 
86
+ # Determines whether paging (of long blocks of text) is enabled.
87
+ # @return [Boolean]
88
+ attr_accessor :pager
89
+
74
90
  # Determines whether the rc file (~/.pryrc) should be loaded.
75
- # @return [Boolean]
91
+ # @return [Boolean]
76
92
  attr_accessor :should_load_rc
77
93
 
78
94
  # Set to true if Pry is invoked from command line using `pry` executable
@@ -93,7 +109,7 @@ class Pry
93
109
  load(file_name) if File.exists?(file_name)
94
110
  end
95
111
  end
96
-
112
+
97
113
  # Start a Pry REPL.
98
114
  # This method also loads the files specified in `Pry::RC_FILES` the
99
115
  # first time it is invoked.
@@ -107,7 +123,7 @@ class Pry
107
123
  load_rc
108
124
  @rc_loaded = true
109
125
  end
110
-
126
+
111
127
  new(options).repl(target)
112
128
  end
113
129
 
@@ -117,11 +133,14 @@ class Pry
117
133
  # @return [String] The string representation of `obj`.
118
134
  def self.view(obj)
119
135
  case obj
120
- when String, Hash, Array, Symbol, nil
136
+ when String, Hash, Array, Symbol, Exception, nil
121
137
  obj.inspect
122
138
  else
123
139
  obj.to_s
124
140
  end
141
+
142
+ rescue NoMethodError
143
+ "unknown"
125
144
  end
126
145
 
127
146
  # A version of `Pry.view` that clips the output to `max_size` chars.
@@ -158,17 +177,18 @@ class Pry
158
177
  def self.run_command(arg_string, options={})
159
178
  name, arg_string = arg_string.split(/\s+/, 2)
160
179
  arg_string = "" if !arg_string
161
-
180
+
162
181
  options = {
163
182
  :context => TOPLEVEL_BINDING,
164
183
  :show_output => false,
165
184
  :output => Pry.output,
166
185
  :commands => Pry.commands
167
186
  }.merge!(options)
168
-
187
+
169
188
  null_output = Object.new.tap { |v| v.instance_eval { def puts(*) end } }
170
-
171
- commands = options[:commands].clone
189
+
190
+ commands = options[:commands]
191
+
172
192
  commands.output = options[:show_output] ? options[:output] : null_output
173
193
  commands.target = Pry.binding_for(options[:context])
174
194
 
@@ -188,8 +208,11 @@ class Pry
188
208
  @commands = Pry::Commands
189
209
  @prompt = DEFAULT_PROMPT
190
210
  @print = DEFAULT_PRINT
211
+ @exception_handler = DEFAULT_EXCEPTION_HANDLER
191
212
  @hooks = DEFAULT_HOOKS
213
+ @custom_completions = DEFAULT_CUSTOM_COMPLETIONS
192
214
  @color = true
215
+ @pager = true
193
216
  @should_load_rc = true
194
217
  @rc_loaded = false
195
218
  @cli = false
@@ -1,8 +1,13 @@
1
+ direc = File.dirname(__FILE__)
2
+
3
+ require "#{direc}/command_processor.rb"
4
+
1
5
  class Pry
2
6
 
3
7
  # The list of configuration options.
4
8
  CONFIG_OPTIONS = [:input, :output, :commands, :print,
5
- :prompt, :hooks]
9
+ :exception_handler, :prompt, :hooks,
10
+ :custom_completions]
6
11
 
7
12
  attr_accessor *CONFIG_OPTIONS
8
13
 
@@ -13,13 +18,11 @@ class Pry
13
18
 
14
19
  # Create a new `Pry` object.
15
20
  # @param [Hash] options The optional configuration parameters.
16
- # @option options [#readline] :input The object to use for input.
17
- # @option options [#puts] :output The object to use for output.
18
- # @option options [Pry::CommandBase] :commands The object to use for
19
- # commands. (see commands.rb)
21
+ # @option options [#readline] :input The object to use for input.
22
+ # @option options [#puts] :output The object to use for output.
23
+ # @option options [Pry::CommandBase] :commands The object to use for commands. (see commands.rb)
20
24
  # @option options [Hash] :hooks The defined hook Procs (see hooks.rb)
21
- # @option options [Array<Proc>] :default_prompt The array of Procs
22
- # to use for the prompts. (see prompts.rb)
25
+ # @option options [Array<Proc>] :default_prompt The array of Procs to use for the prompts. (see prompts.rb)
23
26
  # @option options [Proc] :print The Proc to use for the 'print'
24
27
  # component of the REPL. (see print.rb)
25
28
  def initialize(options={})
@@ -31,6 +34,8 @@ class Pry
31
34
  CONFIG_OPTIONS.each do |key|
32
35
  instance_variable_set("@#{key}", default_options[key])
33
36
  end
37
+
38
+ @command_processor = CommandProcessor.new(self)
34
39
  end
35
40
 
36
41
  # Get nesting data.
@@ -73,8 +78,8 @@ class Pry
73
78
  Pry.active_instance = self
74
79
 
75
80
  # Make sure special locals exist
76
- target.eval("_pry_ = Pry.active_instance")
77
- target.eval("_ = Pry.last_result")
81
+ target.eval("_pry_ = ::Pry.active_instance")
82
+ target.eval("_ = ::Pry.last_result")
78
83
  self.session_target = target
79
84
  end
80
85
 
@@ -87,7 +92,7 @@ class Pry
87
92
 
88
93
  # If break_data is an array, then the last element is the return value
89
94
  break_level, return_value = Array(break_data)
90
-
95
+
91
96
  # keep throwing until we reach the desired nesting level
92
97
  if nesting_level != break_level
93
98
  throw :breakout, break_data
@@ -95,7 +100,7 @@ class Pry
95
100
 
96
101
  return_value
97
102
  end
98
-
103
+
99
104
  # Start a read-eval-print-loop.
100
105
  # If no parameter is given, default to top-level (main).
101
106
  # @param [Object, Binding] target The receiver of the Pry session
@@ -109,7 +114,7 @@ class Pry
109
114
  target_self = target.eval('self')
110
115
 
111
116
  repl_prologue(target)
112
-
117
+
113
118
  # cannot rely on nesting.level as
114
119
  # nesting.level changes with new sessions
115
120
  nesting_level = nesting.size
@@ -125,7 +130,7 @@ class Pry
125
130
 
126
131
  # if one was provided, return the return value
127
132
  return return_value if return_value
128
-
133
+
129
134
  # otherwise return the target_self
130
135
  target_self
131
136
  end
@@ -137,13 +142,17 @@ class Pry
137
142
  # Pry.new.rep(Object.new)
138
143
  def rep(target=TOPLEVEL_BINDING)
139
144
  target = Pry.binding_for(target)
140
- print.call output, re(target)
145
+ result = re(target)
146
+
147
+ show_result(result) if should_print?
141
148
  end
142
149
 
143
150
  # Perform a read-eval
144
151
  # If no parameter is given, default to top-level (main).
145
152
  # @param [Object, Binding] target The receiver of the read-eval-print
146
- # @return [Object] The result of the eval or an `Exception` object in case of error.
153
+ # @return [Object] The result of the eval or an `Exception` object in case of
154
+ # error. In the latter case, you can check whether the exception was raised
155
+ # or is just the result of the expression using #last_result_is_exception?
147
156
  # @example
148
157
  # Pry.new.re(Object.new)
149
158
  def re(target=TOPLEVEL_BINDING)
@@ -151,13 +160,14 @@ class Pry
151
160
 
152
161
  if input == Readline
153
162
  # Readline tab completion
154
- Readline.completion_proc = Pry::InputCompleter.build_completion_proc(target, commands.commands.keys)
163
+ Readline.completion_proc = Pry::InputCompleter.build_completion_proc target, instance_eval(&custom_completions)
155
164
  end
156
165
 
157
-
158
166
  # save the pry instance to active_instance
159
167
  Pry.active_instance = self
160
- target.eval("_pry_ = Pry.active_instance")
168
+ target.eval("_pry_ = ::Pry.active_instance")
169
+
170
+ @last_result_is_exception = false
161
171
 
162
172
  # eval the expression and save to last_result
163
173
  # Do not want __FILE__, __LINE__ here because we need to distinguish
@@ -167,6 +177,7 @@ class Pry
167
177
  rescue SystemExit => e
168
178
  exit
169
179
  rescue Exception => e
180
+ @last_result_is_exception = true
170
181
  set_last_exception(e, target)
171
182
  end
172
183
 
@@ -181,105 +192,95 @@ class Pry
181
192
  # Pry.new.r(Object.new)
182
193
  def r(target=TOPLEVEL_BINDING)
183
194
  target = Pry.binding_for(target)
195
+ @suppress_output = false
184
196
  eval_string = ""
185
197
 
198
+ val = ""
186
199
  loop do
187
- current_prompt = select_prompt(eval_string.empty?, target.eval('self'))
200
+ val = retrieve_line(eval_string, target)
201
+ process_line(val, eval_string, target)
202
+ break if valid_expression?(eval_string)
203
+ end
188
204
 
189
- val = readline(current_prompt)
205
+ @suppress_output = true if eval_string =~ /;\Z/ || null_input?(val)
190
206
 
191
- # exit pry if we receive EOF character
192
- if !val
193
- output.puts
194
- throw(:breakout, nesting.level)
195
- end
196
-
197
- val.chomp!
207
+ eval_string
208
+ end
198
209
 
199
- Pry.cmd_ret_value = process_commands(val, eval_string, target)
210
+ # FIXME should delete this method? it's exposing an implementation detail!
211
+ def show_result(result)
212
+ if last_result_is_exception?
213
+ exception_handler.call output, result
214
+ else
215
+ print.call output, result
216
+ end
217
+ end
200
218
 
201
- if Pry.cmd_ret_value
202
- eval_string << "Pry.cmd_ret_value\n"
203
- else
204
- eval_string << "#{val}\n"
205
- end
219
+ # Returns true if input is "" and a command is not returning a
220
+ # value.
221
+ # @param [String] val The input string.
222
+ # @return [Boolean] Whether the input is null.
223
+ def null_input?(val)
224
+ val.empty? && !Pry.cmd_ret_value
225
+ end
226
+
227
+ # Read a line of input and check for ^d, also determine prompt to use.
228
+ # This method should not need to be invoked directly.
229
+ # @param [String] eval_string The cumulative lines of input.
230
+ # @param [Binding] target The target of the session.
231
+ # @return [String] The line received.
232
+ def retrieve_line(eval_string, target)
233
+ current_prompt = select_prompt(eval_string.empty?, target.eval('self'))
234
+ val = readline(current_prompt)
235
+
236
+ # exit session if we receive EOF character
237
+ if !val
238
+ output.puts
239
+ throw(:breakout, nesting.level)
240
+ end
206
241
 
207
- break eval_string if valid_expression?(eval_string)
242
+ val
243
+ end
244
+
245
+ # Process the line received.
246
+ # This method should not need to be invoked directly.
247
+ # @param [String] val The line to process.
248
+ # @param [String] eval_string The cumulative lines of input.
249
+ # @target [Binding] target The target of the Pry session.
250
+ def process_line(val, eval_string, target)
251
+ val.rstrip!
252
+ Pry.cmd_ret_value = @command_processor.process_commands(val, eval_string, target)
253
+
254
+ if Pry.cmd_ret_value
255
+ eval_string << "Pry.cmd_ret_value\n"
256
+ else
257
+ eval_string << "#{val}\n" if !val.empty?
208
258
  end
209
259
  end
210
260
 
211
261
  # Set the last result of an eval.
262
+ # This method should not need to be invoked directly.
212
263
  # @param [Object] result The result.
213
264
  # @param [Binding] target The binding to set `_` on.
214
265
  def set_last_result(result, target)
215
266
  Pry.last_result = result
216
- target.eval("_ = Pry.last_result")
267
+ target.eval("_ = ::Pry.last_result")
217
268
  end
218
269
 
219
270
  # Set the last exception for a session.
271
+ # This method should not need to be invoked directly.
220
272
  # @param [Exception] ex The exception.
221
273
  # @param [Binding] target The binding to set `_ex_` on.
222
274
  def set_last_exception(ex, target)
223
275
  Pry.last_exception = ex
224
- target.eval("_ex_ = Pry.last_exception")
276
+ target.eval("_ex_ = ::Pry.last_exception")
225
277
  end
226
278
 
227
- # Process Pry commands. Pry commands are not Ruby methods and are evaluated
228
- # prior to Ruby expressions.
229
- # Commands can be modified/configured by the user: see `Pry::Commands`
230
- # This method should not need to be invoked directly - it is called
231
- # by `Pry#r`.
232
- # @param [String] val The current line of input.
233
- # @param [String] eval_string The cumulative lines of input for
234
- # multi-line input.
235
- # @param [Binding] target The receiver of the commands.
236
- def process_commands(val, eval_string, target)
237
- def val.clear() replace("") end
238
- def eval_string.clear() replace("") end
239
-
240
- pattern, cmd_data = commands.commands.find do |name, cmd_data|
241
- /^#{name}(?!\S)(?:\s+(.+))?/ =~ val
242
- end
243
-
244
- # no command was matched, so return to caller
245
- return if !pattern
246
-
247
- args_string = $1
248
- args = args_string ? Shellwords.shellwords(args_string) : []
249
- action = cmd_data[:action]
250
- keep_retval = cmd_data[:keep_retval]
251
-
252
- options = {
253
- :val => val,
254
- :eval_string => eval_string,
255
- :nesting => nesting,
256
- :commands => commands.commands
257
- }
258
-
259
- # set some useful methods to be used by the action blocks
260
- commands.opts = options
261
- commands.target = target
262
- commands.output = output
263
-
264
- case action.arity <=> 0
265
- when -1
266
-
267
- # Use instance_exec() to make the `opts` method, etc available
268
- ret_value = commands.instance_exec(*args, &action)
269
- when 1, 0
270
-
271
- # ensure that we get the right number of parameters
272
- # since 1.8.7 complains about incorrect arity (1.9.2
273
- # doesn't care)
274
- args_with_corrected_arity = args.values_at *0..(action.arity - 1)
275
- ret_value = commands.instance_exec(*args_with_corrected_arity, &action)
276
- end
277
-
278
- # a command was processed so we can now clear the input string
279
- val.clear
280
-
281
- # return value of block only if :keep_retval is true
282
- ret_value if keep_retval
279
+ # @return [Boolean] True if the last result is an exception that was raised,
280
+ # as opposed to simply an instance of Exception (like the result of
281
+ # Exception.new)
282
+ def last_result_is_exception?
283
+ @last_result_is_exception
283
284
  end
284
285
 
285
286
  # Returns the next line of input to be used by the pry instance.
@@ -294,14 +295,28 @@ class Pry
294
295
  # as it has a second parameter.
295
296
  input.readline(current_prompt, true)
296
297
  else
297
- if input.method(:readline).arity == 1
298
- input.readline(current_prompt)
299
- else
300
- input.readline
298
+ begin
299
+ if input.method(:readline).arity == 1
300
+ input.readline(current_prompt)
301
+ else
302
+ input.readline
303
+ end
304
+
305
+ rescue EOFError
306
+ self.input = Readline
307
+ ""
301
308
  end
302
309
  end
303
310
  end
304
311
 
312
+ # Whether the print proc should be invoked.
313
+ # Currently only invoked if the output is not suppressed OR the last result
314
+ # is an exception regardless of suppression.
315
+ # @return [Boolean] Whether the print proc should be invoked.
316
+ def should_print?
317
+ !@suppress_output || last_result_is_exception?
318
+ end
319
+
305
320
  # Returns the appropriate prompt to use.
306
321
  # This method should not need to be invoked directly.
307
322
  # @param [Boolean] first_line Whether this is the first line of input
@@ -317,7 +332,7 @@ class Pry
317
332
  end
318
333
  end
319
334
 
320
- if RUBY_VERSION =~ /1.9/
335
+ if RUBY_VERSION =~ /1.9/ && RUBY_ENGINE == "ruby"
321
336
  require 'ripper'
322
337
 
323
338
  # Determine if a string of code is a valid Ruby expression.
data/lib/pry/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Pry
2
- VERSION = "0.7.3"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/pry.rb CHANGED
@@ -4,7 +4,6 @@
4
4
  direc = File.dirname(__FILE__)
5
5
 
6
6
  $LOAD_PATH << File.expand_path(direc)
7
- $LOAD_PATH << "."
8
7
 
9
8
  require "method_source"
10
9
  require 'shellwords'
@@ -16,18 +15,25 @@ if RUBY_PLATFORM =~ /mswin/ || RUBY_PLATFORM =~ /mingw/
16
15
  begin
17
16
  require 'win32console'
18
17
  rescue LoadError
19
- $stderr.puts "Need to `gem install win32console`"
18
+ $stderr.puts "Need to `gem install win32console`"
20
19
  exit 1
21
20
  end
22
21
  end
23
22
 
24
- require "pry/version"
25
- require "pry/hooks"
26
- require "pry/print"
27
- require "pry/command_base"
28
- require "pry/commands"
29
- require "pry/prompts"
30
- require "pry/completion"
31
- require "pry/core_extensions"
32
- require "pry/pry_class"
33
- require "pry/pry_instance"
23
+ require "#{direc}/pry/version"
24
+ require "#{direc}/pry/hooks"
25
+ require "#{direc}/pry/print"
26
+ require "#{direc}/pry/command_base"
27
+ require "#{direc}/pry/commands"
28
+ require "#{direc}/pry/prompts"
29
+ require "#{direc}/pry/custom_completions"
30
+ require "#{direc}/pry/completion"
31
+ require "#{direc}/pry/core_extensions"
32
+ require "#{direc}/pry/pry_class"
33
+ require "#{direc}/pry/pry_instance"
34
+
35
+
36
+ # TEMPORARY HACK FOR BUG IN JRUBY 1.9 REGEX (which kills CodeRay)
37
+ if RUBY_VERSION =~ /1.9/ && RUBY_ENGINE =~ /jruby/
38
+ Pry.color = false
39
+ end