pry 0.9.8.2 → 0.9.8.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/CHANGELOG +8 -0
  2. data/README.markdown +20 -13
  3. data/Rakefile +1 -1
  4. data/lib/pry.rb +1 -2
  5. data/lib/pry/command.rb +88 -2
  6. data/lib/pry/command_set.rb +15 -85
  7. data/lib/pry/commands.rb +12 -8
  8. data/lib/pry/default_commands/cd.rb +58 -0
  9. data/lib/pry/default_commands/commands.rb +62 -0
  10. data/lib/pry/default_commands/context.rb +48 -165
  11. data/lib/pry/default_commands/editing.rb +385 -0
  12. data/lib/pry/default_commands/help.rb +127 -0
  13. data/lib/pry/default_commands/hist.rb +116 -0
  14. data/lib/pry/default_commands/{shell.rb → input_and_output.rb} +137 -15
  15. data/lib/pry/default_commands/introspection.rb +79 -232
  16. data/lib/pry/default_commands/ls.rb +4 -2
  17. data/lib/pry/default_commands/{basic.rb → misc.rb} +1 -14
  18. data/lib/pry/default_commands/navigating_pry.rb +114 -0
  19. data/lib/pry/helpers/base_helpers.rb +15 -3
  20. data/lib/pry/helpers/command_helpers.rb +16 -0
  21. data/lib/pry/history.rb +12 -4
  22. data/lib/pry/method.rb +2 -2
  23. data/lib/pry/pry_class.rb +7 -1
  24. data/lib/pry/pry_instance.rb +6 -0
  25. data/lib/pry/rbx_path.rb +6 -18
  26. data/lib/pry/version.rb +1 -1
  27. data/pry.gemspec +8 -8
  28. data/test/helper.rb +8 -0
  29. data/test/test_command.rb +256 -2
  30. data/test/test_command_integration.rb +2 -13
  31. data/test/test_command_set.rb +13 -23
  32. data/test/test_default_commands/test_help.rb +57 -0
  33. data/test/test_default_commands/test_introspection.rb +23 -0
  34. data/test/test_pry.rb +11 -0
  35. metadata +13 -9
  36. data/lib/pry/default_commands/documentation.rb +0 -209
  37. data/lib/pry/default_commands/input.rb +0 -247
  38. data/test/test_default_commands.rb +0 -58
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ 2/3/2012 version 0.9.8.3 minor update
2
+ * various tweaks to improve rbx support
3
+ * commands now support optional block arguments
4
+ * much improved help command
5
+ * updated method_source dependency
6
+ * added wtf command
7
+ * jruby should now work in windows (though without color)
8
+
1
9
  9/2/2012 version 0.9.8.2 bugfix
2
10
  * fixed bugs related to --super
3
11
  * upgraded slop dependency
@@ -1,4 +1,9 @@
1
1
  [![Build Status](https://secure.travis-ci.org/pry/pry.png)](http://travis-ci.org/pry/pry)
2
+
3
+ **RBX (Rubinius) users**
4
+
5
+ Please report bugs that you find because we are working very hard right now to make Pry more RBX friendly and it would be great to hear of your problems with Pry in RBX. File a ticket if you run into problems with Pry on RBX so we can try to fix it for our next major release.
6
+
2
7
  <center>
3
8
  ![Alt text](http://dl.dropbox.com/u/26521875/pry_logo_350.png)
4
9
 
@@ -32,10 +37,12 @@ these include:
32
37
  * Gist integration
33
38
  * Navigation around state (`cd`, `ls` and friends)
34
39
  * Runtime invocation (use Pry as a developer console or debugger)
35
- * Exotic object support (BasicObject instances, IClasses, ...)
36
- * A Powerful and flexible command system
37
- * Ability to view and replay history
40
+ * Exotic object support (BasicObject ins
41
+ * tances, IClasses, ...)
42
+ * A Powerful and flexible command system* Ability to view and replay history
43
+
38
44
  * Many convenience commands inspired by IPython, Smalltalk and other advanced REPLs
45
+ * A wide-range number of [plugins](https://github.com/pry/pry/wiki/Available-plugins) that provide remote sessions, full debugging functionality, and more.
39
46
 
40
47
  Pry also aims to be more than an IRB replacement; it is an
41
48
  attempt to bring REPL driven programming to the Ruby language. It is
@@ -79,7 +86,7 @@ Pry command will show a list of all private instance methods (in
79
86
  scope) that begin with 'pa'
80
87
 
81
88
  pry(YARD::Parser::SourceParser):5> ls -Mp --grep ^pa
82
- [:parser_class, :parser_type=, :parser_type_for_filename]
89
+ YARD::Parser::SourceParser#methods: parse parser_class parser_type parser_type= parser_type_for_filename
83
90
 
84
91
  ### Navigating around state
85
92
 
@@ -97,7 +104,7 @@ an instance variable inside that class:
97
104
  => 20
98
105
  pry(main)> cd Hello
99
106
  pry(Hello):1> ls -i
100
- => [:@x]
107
+ instance variables: @x
101
108
  pry(Hello):1> cd @x
102
109
  pry(20:2)> self + 10
103
110
  => 30
@@ -121,8 +128,6 @@ We can then jump back to any of the previous nesting levels by using
121
128
  the `jump-to` command:
122
129
 
123
130
  pry("friend":3)> jump-to 1
124
- Ending Pry session for "friend"
125
- Ending Pry session for 100
126
131
  => 100
127
132
  pry(Hello):1>
128
133
 
@@ -223,8 +228,8 @@ In the following example we will enter the `Pry` class, list the
223
228
  instance methods beginning with 're' and display the source code for the `rep` method:
224
229
 
225
230
  pry(main)> cd Pry
226
- pry(Pry):1> ls -M --grep ^re
227
- [:re, :readline, :rep, :repl, :repl_epilogue, :repl_prologue, :retrieve_line]
231
+ pry(Pry)> ls -M --grep re
232
+ Pry#methods: re readline refresh rep repl repl_epilogue repl_prologue retrieve_line
228
233
  pry(Pry):1> show-method rep -l
229
234
 
230
235
  From: /home/john/ruby/projects/pry/lib/pry/pry_instance.rb @ line 143:
@@ -319,12 +324,12 @@ We can also use `ri` in the normal way:
319
324
  ### Gist integration
320
325
 
321
326
  If the `gist` gem is installed then method source or documentation can be gisted to github with the
322
- `gist-method` command. The `gist-method` command accepts the same two
323
- syntaxes as `show-method`. In the example below we will gist the C source
327
+ `gist` command. The `gist` command is capable of gisting [almost any REPL content](https://gist.github.com/cae143e4533416529726), including methods, documentation,
328
+ input expressions, command source, and so on. In the example below we will gist the C source
324
329
  code for the `Symbol#to_proc` method to github:
325
330
 
326
- pry(main)> gist-method Symbol#to_proc
327
- https://gist.github.com/5332c38afc46d902ce46
331
+ pry(main)> gist -m Symbol#to_proc
332
+ Gist created at https://gist.github.com/5332c38afc46d902ce46 and added to clipboard.
328
333
  pry(main)>
329
334
 
330
335
  You can see the actual gist generated here: [https://gist.github.com/5332c38afc46d902ce46](https://gist.github.com/5332c38afc46d902ce46)
@@ -363,6 +368,8 @@ avaiable.
363
368
 
364
369
  MyArtChannel has kindly provided a hack to replace the `rails console` command in Rails 3: [https://gist.github.com/941174](https://gist.github.com/941174) This is not recommended for code bases with multiple developers, as they may not all want to use Pry.
365
370
 
371
+ Also check out the [wiki](https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry) for other strategies on integrating Pry with rails, notably the [pry-rails](https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry#wiki-pry_rails) plugin.
372
+
366
373
  ### Limitations:
367
374
 
368
375
  * Tab completion is currently a bit broken/limited this will have a
data/Rakefile CHANGED
@@ -21,7 +21,7 @@ def apply_spec_defaults(s)
21
21
  s.test_files = `git ls-files -- test/*`.split("\n")
22
22
  s.add_dependency('coderay', '~> 1.0.5')
23
23
  s.add_dependency('slop', ['>= 2.4.4', '< 3'])
24
- s.add_dependency('method_source','~> 0.7')
24
+ s.add_dependency('method_source','~> 0.7.1')
25
25
  s.add_development_dependency('bacon', '~> 1.1')
26
26
  s.add_development_dependency('open4', '~> 1.3')
27
27
  s.add_development_dependency('rake', '~> 0.9')
data/lib/pry.rb CHANGED
@@ -179,8 +179,7 @@ if Pry::Helpers::BaseHelpers.windows?
179
179
  begin
180
180
  require 'win32console'
181
181
  rescue LoadError
182
- warn "Need to `gem install win32console`"
183
- exit 1
182
+ warn "You should: `gem install win32console` for better auto-indent and color support."
184
183
  end
185
184
  end
186
185
 
@@ -83,6 +83,30 @@ class Pry
83
83
  command_regex =~ val
84
84
  end
85
85
 
86
+ # How well does this command match the given line?
87
+ #
88
+ # Higher scores are better because they imply that this command matches
89
+ # the line more closely.
90
+ #
91
+ # The score is calculated by taking the number of characters at the start
92
+ # of the string that are used only to identify the command, not as part of
93
+ # the arguments.
94
+ #
95
+ # @example
96
+ # /\.(.*)/.match_score(".foo") #=> 1
97
+ # /\.*(.*)/.match_score("...foo") #=> 3
98
+ # 'hi'.match_score("hi there") #=> 2
99
+ #
100
+ # @param String a line input at the REPL
101
+ # @return Fixnum
102
+ def match_score(val)
103
+ if command_regex =~ val
104
+ Regexp.last_match.size > 1 ? Regexp.last_match.begin(1) : Regexp.last_match.end(0)
105
+ else
106
+ -1
107
+ end
108
+ end
109
+
86
110
  # Store hooks to be run before or after the command body.
87
111
  # @see {Pry::CommandSet#before_command}
88
112
  # @see {Pry::CommandSet#after_command}
@@ -105,8 +129,27 @@ class Pry
105
129
  obj
106
130
  end
107
131
  end
108
- end
109
132
 
133
+ # The group in which the command should be displayed in "help" output.
134
+ # This is usually auto-generated from directory naming, but it can be
135
+ # manually overridden if necessary.
136
+ def group(name=nil)
137
+ @group = name if name
138
+ @group ||=(
139
+ case Pry::Method(block).source_file
140
+ when %r{/pry/.*_commands/(.*).rb}
141
+ $1.capitalize.gsub(/_/, " ")
142
+ when %r{(pry-[\w_]+)-([\d\.]+)}
143
+ name, version = $1, $2
144
+ "#{name.to_s} (v#{version.to_s})"
145
+ when /pryrc/
146
+ "~/.pryrc"
147
+ else
148
+ "(other)"
149
+ end
150
+ )
151
+ end
152
+ end
110
153
 
111
154
  # Properties of one execution of a command (passed by {Pry#run_command} as a hash of
112
155
  # context and expanded in {#initialize}
@@ -119,6 +162,14 @@ class Pry
119
162
  attr_accessor :command_set
120
163
  attr_accessor :_pry_
121
164
 
165
+ # The block we pass *into* a command so long as `:takes_block` is
166
+ # not equal to `false`
167
+ # @example
168
+ # my-command | do
169
+ # puts "block content"
170
+ # end
171
+ attr_accessor :command_block
172
+
122
173
  # Run a command from another command.
123
174
  # @param [String] command_string The string that invokes the command
124
175
  # @param [Array] args Further arguments to pass to the command
@@ -217,6 +268,9 @@ class Pry
217
268
  # remove the one leading space if it exists
218
269
  arg_string.slice!(0) if arg_string.start_with?(" ")
219
270
 
271
+ # process and pass a block if one is found
272
+ pass_block(arg_string) if command_options[:takes_block]
273
+
220
274
  if arg_string
221
275
  args = command_options[:shellwords] ? Shellwords.shellwords(arg_string) : arg_string.split(" ")
222
276
  else
@@ -241,6 +295,36 @@ class Pry
241
295
  call_safely(*(captures + args))
242
296
  end
243
297
 
298
+ # Pass a block argument to a command.
299
+ # @param [String] arg_string The arguments (as a string) passed to the command.
300
+ # We inspect these for a '| do' or a '| {' and if we find it we use it
301
+ # to start a block input sequence. Once we have a complete
302
+ # block, we save it to an accessor that can be retrieved from the command context.
303
+ # Note that if we find the '| do' or '| {' we delete this and the
304
+ # elements following it from `arg_string`.
305
+ def pass_block(arg_string)
306
+ block_index = arg_string.rindex /\| *(?:do|\{)/
307
+
308
+ return if !block_index
309
+
310
+ block_init_string = arg_string.slice!(block_index..-1)[1..-1]
311
+ prime_string = "proc #{block_init_string}\n"
312
+
313
+ if !_pry_.complete_expression?(prime_string)
314
+ block_string = _pry_.r(target, prime_string)
315
+ else
316
+ block_string = prime_string
317
+ end
318
+
319
+ begin
320
+ self.command_block = target.eval(block_string)
321
+ rescue Pry::RescuableException
322
+ raise CommandError, "Incomplete block definition."
323
+ end
324
+ end
325
+
326
+ private :pass_block
327
+
244
328
  # Run the command with the given {args}.
245
329
  #
246
330
  # This is a public wrapper around {#call} which ensures all preconditions are met.
@@ -327,7 +411,9 @@ class Pry
327
411
  instance_exec(*correct_arg_arity(block.arity, args), &block)
328
412
  end
329
413
 
330
- def help; description; end
414
+ def help
415
+ "#{command_options[:listing].to_s.ljust(18)} #{description}"
416
+ end
331
417
  end
332
418
 
333
419
  # A super-class ofr Commands with structure.
@@ -21,7 +21,6 @@ class Pry
21
21
  @commands = {}
22
22
  @helper_module = Module.new
23
23
 
24
- define_default_commands
25
24
  import(*imported_sets)
26
25
 
27
26
  instance_eval(&block) if block
@@ -281,7 +280,19 @@ class Pry
281
280
  # @param [String] the line that may be a command invocation
282
281
  # @return [Pry::Command, nil]
283
282
  def find_command(val)
284
- commands.values.detect{ |c| c.matches?(val) }
283
+ commands.values.select{ |c| c.matches?(val) }.sort_by{ |c| c.match_score(val) }.last
284
+ end
285
+
286
+ # Find the command that the user might be trying to refer to.
287
+ #
288
+ # @param [String] the user's search.
289
+ # @return [Pry::Command, nil]
290
+ def find_command_for_help(search)
291
+ find_command(search) || (begin
292
+ find_command_by_name_or_listing(search)
293
+ rescue ArgumentError
294
+ nil
295
+ end)
285
296
  end
286
297
 
287
298
  # Is the given line a command invocation?
@@ -324,91 +335,10 @@ class Pry
324
335
  :interpolate => true,
325
336
  :shellwords => true,
326
337
  :listing => name,
327
- :use_prefix => true
338
+ :use_prefix => true,
339
+ :takes_block => false
328
340
  }
329
341
  end
330
-
331
- def define_default_commands
332
-
333
- create_command "help" do |cmd|
334
- description "Show a list of commands, or help for one command"
335
-
336
- banner <<-BANNER
337
- Usage: help [ COMMAND ]
338
-
339
- With no arguments, help lists all the available commands in the current
340
- command-set along with their description.
341
-
342
- When given a command name as an argument, shows the help for that command.
343
- BANNER
344
-
345
- def process
346
- if cmd = args.first
347
- if command = find_command(cmd)
348
- output.puts command.new.help
349
- else
350
- output.puts "No info for command: #{cmd}"
351
- end
352
- else
353
- output.puts
354
- help_text = heading("Command List: ") + "\n"
355
-
356
- help_text << commands.map do |key, command|
357
- if command.description && !command.description.empty?
358
- "#{command.options[:listing].to_s.ljust(18)} #{command.description}"
359
- end
360
- end.compact.sort.join("\n")
361
-
362
- stagger_output(help_text)
363
- end
364
- end
365
- end
366
-
367
- create_command "install-command", "Install a disabled command." do |name|
368
-
369
- banner <<-BANNER
370
- Usage: install-command COMMAND
371
-
372
- Installs the gems necessary to run the given COMMAND. You will generally not
373
- need to run this unless told to by an error message.
374
- BANNER
375
-
376
- def process(name)
377
- require 'rubygems/dependency_installer' unless defined? Gem::DependencyInstaller
378
- command = find_command(name)
379
-
380
- if command_dependencies_met?(command.options)
381
- output.puts "Dependencies for #{command.name} are met. Nothing to do."
382
- return
383
- end
384
-
385
- output.puts "Attempting to install `#{name}` command..."
386
- gems_to_install = Array(command.options[:requires_gem])
387
-
388
- gems_to_install.each do |g|
389
- next if gem_installed?(g)
390
- output.puts "Installing `#{g}` gem..."
391
-
392
- begin
393
- Gem::DependencyInstaller.new.install(g)
394
- rescue Gem::GemNotFoundException
395
- raise CommandError, "Required Gem: `#{g}` not found. Aborting command installation."
396
- end
397
- end
398
-
399
- Gem.refresh
400
- gems_to_install.each do |g|
401
- begin
402
- require g
403
- rescue LoadError
404
- raise CommandError, "Required Gem: `#{g}` installed but not found?!. Aborting command installation."
405
- end
406
- end
407
-
408
- output.puts "Installation of `#{name}` successful! Type `help #{name}` for information"
409
- end
410
- end
411
- end
412
342
  end
413
343
 
414
344
  # Wraps the return result of process_commands, indicates if the
@@ -1,10 +1,12 @@
1
- require "pry/default_commands/basic"
2
- require "pry/default_commands/documentation"
1
+ require "pry/default_commands/misc"
2
+ require "pry/default_commands/help"
3
3
  require "pry/default_commands/gems"
4
4
  require "pry/default_commands/context"
5
- require "pry/default_commands/input"
6
- require "pry/default_commands/shell"
5
+ require "pry/default_commands/commands"
6
+ require "pry/default_commands/input_and_output"
7
7
  require "pry/default_commands/introspection"
8
+ require "pry/default_commands/editing"
9
+ require "pry/default_commands/navigating_pry"
8
10
  require "pry/default_commands/easter_eggs"
9
11
 
10
12
  require "pry/extended_commands/experimental"
@@ -13,13 +15,15 @@ class Pry
13
15
 
14
16
  # Default commands used by Pry.
15
17
  Commands = Pry::CommandSet.new do
16
- import DefaultCommands::Basic
17
- import DefaultCommands::Documentation
18
+ import DefaultCommands::Misc
19
+ import DefaultCommands::Help
18
20
  import DefaultCommands::Gems
19
21
  import DefaultCommands::Context
20
- import DefaultCommands::Input
21
- import DefaultCommands::Shell
22
+ import DefaultCommands::NavigatingPry
23
+ import DefaultCommands::Editing
24
+ import DefaultCommands::InputAndOutput
22
25
  import DefaultCommands::Introspection
23
26
  import DefaultCommands::EasterEggs
27
+ import DefaultCommands::Commands
24
28
  end
25
29
  end
@@ -0,0 +1,58 @@
1
+ class Pry
2
+ module DefaultCommands
3
+ Cd = Pry::CommandSet.new do
4
+ create_command "cd" do
5
+ group "Context"
6
+ description "Move into a new context (object or scope)."
7
+
8
+ banner <<-BANNER
9
+ Usage: cd [OPTIONS] [--help]
10
+
11
+ Move into new context (object or scope). As in unix shells use
12
+ `cd ..` to go back and `cd /` to return to Pry top-level).
13
+ Complex syntax (e.g cd ../@x/y) also supported.
14
+
15
+ e.g: `cd @x`
16
+ e.g: `cd ..
17
+ e.g: `cd /`
18
+
19
+ https://github.com/pry/pry/wiki/State-navigation#wiki-Changing_scope
20
+ BANNER
21
+
22
+ def process
23
+ path = arg_string.split(/\//)
24
+ stack = _pry_.binding_stack.dup
25
+
26
+ # special case when we only get a single "/", return to root
27
+ stack = [stack.first] if path.empty?
28
+
29
+ path.each do |context|
30
+ begin
31
+ case context.chomp
32
+ when ""
33
+ stack = [stack.first]
34
+ when "::"
35
+ stack.push(TOPLEVEL_BINDING)
36
+ when "."
37
+ next
38
+ when ".."
39
+ unless stack.size == 1
40
+ stack.pop
41
+ end
42
+ else
43
+ stack.push(Pry.binding_for(stack.last.eval(context)))
44
+ end
45
+
46
+ rescue RescuableException => e
47
+ output.puts "Bad object path: #{arg_string.chomp}. Failed trying to resolve: #{context}"
48
+ output.puts e.inspect
49
+ return
50
+ end
51
+ end
52
+
53
+ _pry_.binding_stack = stack
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end