pry 0.10.3 → 0.12.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +251 -16
  3. data/LICENSE +1 -1
  4. data/README.md +35 -51
  5. data/bin/pry +3 -11
  6. data/lib/pry/basic_object.rb +6 -0
  7. data/lib/pry/cli.rb +50 -52
  8. data/lib/pry/code/code_file.rb +13 -6
  9. data/lib/pry/code/code_range.rb +3 -3
  10. data/lib/pry/code/loc.rb +14 -8
  11. data/lib/pry/code.rb +12 -5
  12. data/lib/pry/code_object.rb +27 -4
  13. data/lib/pry/color_printer.rb +20 -10
  14. data/lib/pry/command.rb +76 -45
  15. data/lib/pry/command_set.rb +17 -45
  16. data/lib/pry/commands/amend_line.rb +3 -4
  17. data/lib/pry/commands/bang.rb +1 -1
  18. data/lib/pry/commands/cat/exception_formatter.rb +10 -8
  19. data/lib/pry/commands/cat/file_formatter.rb +7 -3
  20. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  21. data/lib/pry/commands/cat.rb +7 -6
  22. data/lib/pry/commands/change_prompt.rb +29 -9
  23. data/lib/pry/commands/clear_screen.rb +14 -0
  24. data/lib/pry/commands/code_collector.rb +25 -23
  25. data/lib/pry/commands/easter_eggs.rb +12 -12
  26. data/lib/pry/commands/edit/file_and_line_locator.rb +1 -1
  27. data/lib/pry/commands/edit.rb +15 -10
  28. data/lib/pry/commands/exit.rb +2 -1
  29. data/lib/pry/commands/find_method.rb +12 -14
  30. data/lib/pry/commands/gem_cd.rb +1 -1
  31. data/lib/pry/commands/gem_install.rb +2 -2
  32. data/lib/pry/commands/gem_list.rb +2 -2
  33. data/lib/pry/commands/gem_open.rb +2 -2
  34. data/lib/pry/commands/gem_readme.rb +25 -0
  35. data/lib/pry/commands/gem_search.rb +40 -0
  36. data/lib/pry/commands/gem_stats.rb +83 -0
  37. data/lib/pry/commands/gist.rb +7 -6
  38. data/lib/pry/commands/help.rb +3 -3
  39. data/lib/pry/commands/hist.rb +11 -10
  40. data/lib/pry/commands/import_set.rb +2 -1
  41. data/lib/pry/commands/install_command.rb +7 -6
  42. data/lib/pry/commands/jump_to.rb +7 -7
  43. data/lib/pry/commands/list_inspectors.rb +2 -2
  44. data/lib/pry/commands/ls/constants.rb +14 -3
  45. data/lib/pry/commands/ls/formatter.rb +4 -2
  46. data/lib/pry/commands/ls/globals.rb +0 -2
  47. data/lib/pry/commands/ls/grep.rb +0 -2
  48. data/lib/pry/commands/ls/instance_vars.rb +0 -1
  49. data/lib/pry/commands/ls/jruby_hacks.rb +2 -2
  50. data/lib/pry/commands/ls/local_names.rb +0 -2
  51. data/lib/pry/commands/ls/local_vars.rb +0 -2
  52. data/lib/pry/commands/ls/ls_entity.rb +0 -1
  53. data/lib/pry/commands/ls/methods.rb +0 -3
  54. data/lib/pry/commands/ls/methods_helper.rb +1 -1
  55. data/lib/pry/commands/ls/self_methods.rb +2 -1
  56. data/lib/pry/commands/ls.rb +30 -31
  57. data/lib/pry/commands/play.rb +3 -4
  58. data/lib/pry/commands/pry_backtrace.rb +1 -1
  59. data/lib/pry/commands/raise_up.rb +2 -1
  60. data/lib/pry/commands/reload_code.rb +2 -2
  61. data/lib/pry/commands/ri.rb +9 -4
  62. data/lib/pry/commands/shell_command.rb +36 -9
  63. data/lib/pry/commands/shell_mode.rb +6 -6
  64. data/lib/pry/commands/show_doc.rb +5 -7
  65. data/lib/pry/commands/show_info.rb +41 -20
  66. data/lib/pry/commands/show_source.rb +5 -2
  67. data/lib/pry/commands/stat.rb +1 -1
  68. data/lib/pry/commands/watch_expression/expression.rb +1 -1
  69. data/lib/pry/commands/watch_expression.rb +9 -7
  70. data/lib/pry/commands/whereami.rb +16 -9
  71. data/lib/pry/commands/wtf.rb +15 -2
  72. data/lib/pry/config/behavior.rb +230 -114
  73. data/lib/pry/config/convenience.rb +24 -21
  74. data/lib/pry/config/default.rb +151 -153
  75. data/lib/pry/config/memoization.rb +48 -0
  76. data/lib/pry/config.rb +30 -19
  77. data/lib/pry/core_extensions.rb +15 -4
  78. data/lib/pry/editor.rb +5 -12
  79. data/lib/pry/exceptions.rb +1 -3
  80. data/lib/pry/forwardable.rb +23 -0
  81. data/lib/pry/helpers/base_helpers.rb +197 -110
  82. data/lib/pry/helpers/command_helpers.rb +5 -4
  83. data/lib/pry/helpers/documentation_helpers.rb +3 -2
  84. data/lib/pry/helpers/options_helpers.rb +6 -6
  85. data/lib/pry/helpers/platform.rb +58 -0
  86. data/lib/pry/helpers/table.rb +20 -15
  87. data/lib/pry/helpers/text.rb +82 -74
  88. data/lib/pry/helpers.rb +1 -0
  89. data/lib/pry/history.rb +44 -10
  90. data/lib/pry/hooks.rb +50 -109
  91. data/lib/pry/indent.rb +21 -19
  92. data/lib/pry/input_completer.rb +146 -123
  93. data/lib/pry/input_lock.rb +0 -2
  94. data/lib/pry/last_exception.rb +2 -2
  95. data/lib/pry/method/disowned.rb +3 -1
  96. data/lib/pry/method/patcher.rb +2 -5
  97. data/lib/pry/method/weird_method_locator.rb +21 -11
  98. data/lib/pry/method.rb +44 -38
  99. data/lib/pry/object_path.rb +5 -4
  100. data/lib/pry/output.rb +37 -37
  101. data/lib/pry/pager.rb +195 -181
  102. data/lib/pry/platform.rb +91 -0
  103. data/lib/pry/plugins.rb +27 -8
  104. data/lib/pry/prompt.rb +144 -25
  105. data/lib/pry/pry_class.rb +83 -33
  106. data/lib/pry/pry_instance.rb +94 -59
  107. data/lib/pry/repl.rb +70 -11
  108. data/lib/pry/repl_file_loader.rb +2 -3
  109. data/lib/pry/ring.rb +84 -0
  110. data/lib/pry/rubygem.rb +9 -7
  111. data/lib/pry/slop/LICENSE +20 -0
  112. data/lib/pry/slop/commands.rb +195 -0
  113. data/lib/pry/slop/option.rb +206 -0
  114. data/lib/pry/slop.rb +661 -0
  115. data/lib/pry/terminal.rb +18 -6
  116. data/lib/pry/testable/evalable.rb +15 -0
  117. data/lib/pry/testable/mockable.rb +14 -0
  118. data/lib/pry/testable/pry_tester.rb +73 -0
  119. data/lib/pry/testable/utility.rb +26 -0
  120. data/lib/pry/testable/variables.rb +46 -0
  121. data/lib/pry/testable.rb +70 -0
  122. data/lib/pry/version.rb +1 -1
  123. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +9 -14
  124. data/lib/pry/wrapped_module.rb +22 -21
  125. data/lib/pry.rb +21 -50
  126. metadata +35 -46
  127. data/lib/pry/commands/list_prompts.rb +0 -35
  128. data/lib/pry/commands/simple_prompt.rb +0 -22
  129. data/lib/pry/history_array.rb +0 -121
  130. data/lib/pry/rbx_path.rb +0 -22
  131. data/lib/pry/test/helper.rb +0 -170
data/lib/pry/command.rb CHANGED
@@ -2,7 +2,6 @@ require 'delegate'
2
2
  require 'pry/helpers/documentation_helpers'
3
3
 
4
4
  class Pry
5
-
6
5
  # The super-class of all commands, new commands should be created by calling
7
6
  # {Pry::CommandSet#command} which creates a BlockCommand or {Pry::CommandSet#create_command}
8
7
  # which creates a ClassCommand. Please don't use this class directly.
@@ -24,7 +23,7 @@ class Pry
24
23
  attr_writer :command_options
25
24
  attr_writer :match
26
25
 
27
- def match(arg=nil)
26
+ def match(arg = nil)
28
27
  if arg
29
28
  @command_options ||= default_options(arg)
30
29
  @command_options[:listing] = arg.is_a?(String) ? arg : arg.inspect
@@ -34,13 +33,13 @@ class Pry
34
33
  end
35
34
 
36
35
  # Define or get the command's description
37
- def description(arg=nil)
36
+ def description(arg = nil)
38
37
  @description = arg if arg
39
38
  @description ||= nil
40
39
  end
41
40
 
42
41
  # Define or get the command's options
43
- def command_options(arg=nil)
42
+ def command_options(arg = nil)
44
43
  @command_options ||= default_options(match)
45
44
  @command_options.merge!(arg) if arg
46
45
  @command_options
@@ -50,9 +49,9 @@ class Pry
50
49
  alias_method :options=, :command_options=
51
50
 
52
51
  # Define or get the command's banner
53
- def banner(arg=nil)
52
+ def banner(arg = nil)
54
53
  @banner = arg if arg
55
- @banner || description
54
+ @banner ||= description
56
55
  end
57
56
 
58
57
  def block
@@ -68,10 +67,6 @@ class Pry
68
67
  new.help
69
68
  end
70
69
 
71
- def source_location
72
- block.source_location
73
- end
74
-
75
70
  def source_file
76
71
  Array(block.source_location).first
77
72
  end
@@ -84,27 +79,32 @@ class Pry
84
79
 
85
80
  def default_options(match)
86
81
  {
87
- :requires_gem => [],
88
- :keep_retval => false,
89
- :argument_required => false,
90
- :interpolate => true,
91
- :shellwords => true,
92
- :listing => (String === match ? match : match.inspect),
93
- :use_prefix => true,
94
- :takes_block => false
82
+ requires_gem: [],
83
+ keep_retval: false,
84
+ argument_required: false,
85
+ interpolate: true,
86
+ shellwords: true,
87
+ listing: (String === match ? match : match.inspect),
88
+ use_prefix: true,
89
+ takes_block: false
95
90
  }
96
91
  end
97
92
  end
98
93
 
99
94
  # Make those properties accessible to instances
100
95
  def name; self.class.name; end
96
+
101
97
  def match; self.class.match; end
98
+
102
99
  def description; self.class.description; end
100
+
103
101
  def block; self.class.block; end
102
+
104
103
  def command_options; self.class.options; end
104
+
105
105
  def command_name; self.class.command_name; end
106
+
106
107
  def source; self.class.source; end
107
- def source_location; self.class.source_location; end
108
108
 
109
109
  class << self
110
110
  def name
@@ -167,11 +167,10 @@ class Pry
167
167
  end
168
168
  end
169
169
 
170
+ # @deprecated Replaced with {Pry::Hooks#add_hook}. Left for compatibility.
170
171
  # Store hooks to be run before or after the command body.
171
- # @see {Pry::CommandSet#before_command}
172
- # @see {Pry::CommandSet#after_command}
173
172
  def hooks
174
- @hooks ||= {:before => [], :after => []}
173
+ Pry.hooks
175
174
  end
176
175
 
177
176
  def command_regex
@@ -195,7 +194,7 @@ class Pry
195
194
  # This is usually auto-generated from directory naming, but it can be
196
195
  # manually overridden if necessary.
197
196
  # Group should not be changed once it is initialized.
198
- def group(name=nil)
197
+ def group(name = nil)
199
198
  @group ||= if name
200
199
  name
201
200
  else
@@ -204,9 +203,9 @@ class Pry
204
203
  $1.capitalize.gsub(/_/, " ")
205
204
  when %r{(pry-\w+)-([\d\.]+([\w\.]+)?)}
206
205
  name, version = $1, $2
207
- "#{name.to_s} (v#{version.to_s})"
206
+ "#{name} (v#{version})"
208
207
  when /pryrc/
209
- "~/.pryrc"
208
+ "pryrc"
210
209
  else
211
210
  "(other)"
212
211
  end
@@ -223,6 +222,7 @@ class Pry
223
222
  attr_accessor :arg_string
224
223
  attr_accessor :context
225
224
  attr_accessor :command_set
225
+ attr_accessor :hooks
226
226
  attr_accessor :_pry_
227
227
 
228
228
  # The block we pass *into* a command so long as `:takes_block` is
@@ -252,6 +252,14 @@ class Pry
252
252
  command_set.to_hash
253
253
  end
254
254
 
255
+ #
256
+ # @deprecated
257
+ # Please use black(), white(), etc directly instead (as you would with helper
258
+ # functions from BaseHelpers and CommandHelpers)
259
+ #
260
+ # @return [Module]
261
+ # Returns Pry::Helpers::Text
262
+ #
255
263
  def text
256
264
  Pry::Helpers::Text
257
265
  end
@@ -262,15 +270,17 @@ class Pry
262
270
 
263
271
  include Pry::Helpers::BaseHelpers
264
272
  include Pry::Helpers::CommandHelpers
273
+ include Pry::Helpers::Text
265
274
 
266
275
  # Instantiate a command, in preparation for calling it.
267
276
  # @param [Hash] context The runtime context to use with this command.
268
- def initialize(context={})
277
+ def initialize(context = {})
269
278
  self.context = context
270
279
  self.target = context[:target]
271
280
  self.output = context[:output]
272
281
  self.eval_string = context[:eval_string]
273
282
  self.command_set = context[:command_set]
283
+ self.hooks = context[:hooks]
274
284
  self._pry_ = context[:pry_instance]
275
285
  end
276
286
 
@@ -335,6 +345,7 @@ class Pry
335
345
 
336
346
  # please call Command.matches? before Command#call_safely
337
347
  raise CommandError, "fatal: called a command which didn't match?!" unless Regexp.last_match
348
+
338
349
  captures = Regexp.last_match.captures
339
350
  pos = Regexp.last_match.end(0)
340
351
 
@@ -424,10 +435,20 @@ class Pry
424
435
  raise CommandError, "The command '#{command_name}' requires an argument."
425
436
  end
426
437
 
427
- ret = call_with_hooks(*args)
438
+ ret = use_unpatched_symbol do
439
+ call_with_hooks(*args)
440
+ end
428
441
  command_options[:keep_retval] ? ret : void
429
442
  end
430
443
 
444
+ def use_unpatched_symbol
445
+ call_method = Symbol.method_defined?(:call) && Symbol.instance_method(:call)
446
+ Symbol.class_eval { undef :call } if call_method
447
+ yield
448
+ ensure
449
+ Symbol.instance_eval { define_method(:call, call_method) } if call_method
450
+ end
451
+
431
452
  # Are all the gems required to use this command installed?
432
453
  #
433
454
  # @return Boolean
@@ -437,25 +458,36 @@ class Pry
437
458
 
438
459
  # Generate completions for this command
439
460
  #
440
- # @param [String] search The line typed so far
461
+ # @param [String] _search The line typed so far
441
462
  # @return [Array<String>] Completion words
442
- def complete(search)
463
+ def complete(_search)
443
464
  []
444
465
  end
445
466
 
446
467
  private
447
468
 
469
+ def find_hooks(event)
470
+ event_name = "#{event}_#{command_name}"
471
+ (self.hooks || self.class.hooks).get_hooks(event_name).values
472
+ end
473
+
474
+ def before_hooks
475
+ find_hooks('before')
476
+ end
477
+
478
+ def after_hooks
479
+ find_hooks('after')
480
+ end
481
+
448
482
  # Run the `#call` method and all the registered hooks.
449
483
  # @param [Array<String>] args The arguments to `#call`
450
484
  # @return [Object] The return value from `#call`
451
485
  def call_with_hooks(*args)
452
- self.class.hooks[:before].each do |block|
453
- instance_exec(*args, &block)
454
- end
486
+ before_hooks.each { |block| instance_exec(*args, &block) }
455
487
 
456
488
  ret = call(*args)
457
489
 
458
- self.class.hooks[:after].each do |block|
490
+ after_hooks.each do |block|
459
491
  ret = instance_exec(*args, &block)
460
492
  end
461
493
 
@@ -507,18 +539,17 @@ class Pry
507
539
  # subclasses.
508
540
  #
509
541
  # Create subclasses using {Pry::CommandSet#create_command}, and override the
510
- # `options(opt)` method to set up an instance of Slop, and the `process`
542
+ # `options(opt)` method to set up an instance of Pry::Slop, and the `process`
511
543
  # method to actually run the command. If necessary, you can also override
512
544
  # `setup` which will be called before `options`, for example to require any
513
545
  # gems your command needs to run, or to set up state.
514
546
  class ClassCommand < Command
515
547
  class << self
516
-
517
548
  # Ensure that subclasses inherit the options, description and
518
549
  # match from a ClassCommand super class.
519
550
  def inherited(klass)
520
- klass.match match
521
- klass.description description
551
+ klass.match match
552
+ klass.description description
522
553
  klass.command_options options
523
554
  end
524
555
 
@@ -583,15 +614,15 @@ class Pry
583
614
  end
584
615
  end
585
616
 
586
- # Return the help generated by Slop for this command.
617
+ # Return the help generated by Pry::Slop for this command.
587
618
  def help
588
619
  slop.help
589
620
  end
590
621
 
591
- # Return an instance of Slop that can parse either subcommands or the
622
+ # Return an instance of Pry::Slop that can parse either subcommands or the
592
623
  # options that this command accepts.
593
624
  def slop
594
- Slop.new do |opt|
625
+ Pry::Slop.new do |opt|
595
626
  opt.banner(unindent(self.class.banner))
596
627
  subcommands(opt)
597
628
  options(opt)
@@ -603,9 +634,9 @@ class Pry
603
634
  # @param [String] search The line typed so far
604
635
  # @return [Array<String>] the words to complete
605
636
  def complete(search)
606
- slop.map do |opt|
637
+ slop.flat_map do |opt|
607
638
  [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
608
- end.flatten(1).compact + super
639
+ end.compact + super
609
640
  end
610
641
 
611
642
  # A method called just before `options(opt)` as part of `call`.
@@ -620,7 +651,7 @@ class Pry
620
651
  # end
621
652
  def setup; end
622
653
 
623
- # A method to setup Slop commands so it can parse the subcommands your
654
+ # A method to setup Pry::Slop commands so it can parse the subcommands your
624
655
  # command expects. If you need to set up default values, use `setup`
625
656
  # instead.
626
657
  #
@@ -655,7 +686,7 @@ class Pry
655
686
  # end
656
687
  def subcommands(cmd); end
657
688
 
658
- # A method to setup Slop so it can parse the options your command expects.
689
+ # A method to setup Pry::Slop so it can parse the options your command expects.
659
690
  #
660
691
  # @note Please don't do anything side-effecty in the main part of this
661
692
  # method, as it may be called by Pry at any time for introspection reasons.
@@ -672,7 +703,7 @@ class Pry
672
703
 
673
704
  # The actual body of your command should go here.
674
705
  #
675
- # The `opts` mehod can be called to get the options that Slop has passed,
706
+ # The `opts` mehod can be called to get the options that Pry::Slop has passed,
676
707
  # and `args` gives the remaining, unparsed arguments.
677
708
  #
678
709
  # The return value of this method is discarded unless the command was
@@ -75,7 +75,7 @@ class Pry
75
75
  # # hello john, nice number: 10
76
76
  # # pry(main)> help number
77
77
  # # number-N regex command
78
- def block_command(match, description="No description.", options={}, &block)
78
+ def block_command(match, description = "No description.", options = {}, &block)
79
79
  description, options = ["No description.", description] if description.is_a?(Hash)
80
80
  options = Pry::Command.default_options(match).merge!(options)
81
81
 
@@ -107,7 +107,7 @@ class Pry
107
107
  # end
108
108
  # end
109
109
  #
110
- def create_command(match, description="No description.", options={}, &block)
110
+ def create_command(match, description = "No description.", options = {}, &block)
111
111
  description, options = ["No description.", description] if description.is_a?(Hash)
112
112
  options = Pry::Command.default_options(match).merge!(options)
113
113
 
@@ -116,34 +116,6 @@ class Pry
116
116
  @commands[match]
117
117
  end
118
118
 
119
- # Execute a block of code before a command is invoked. The block also
120
- # gets access to parameters that will be passed to the command and
121
- # is evaluated in the same context.
122
- # @param [String, Regexp] search The match or listing of the command.
123
- # @yield The block to be run before the command.
124
- # @example Display parameter before invoking command
125
- # Pry.config.commands.before_command("whereami") do |n|
126
- # output.puts "parameter passed was #{n}"
127
- # end
128
- def before_command(search, &block)
129
- cmd = find_command_by_match_or_listing(search)
130
- cmd.hooks[:before].unshift block
131
- end
132
-
133
- # Execute a block of code after a command is invoked. The block also
134
- # gets access to parameters that will be passed to the command and
135
- # is evaluated in the same context.
136
- # @param [String, Regexp] search The match or listing of the command.
137
- # @yield The block to be run after the command.
138
- # @example Display text 'command complete' after invoking command
139
- # Pry.config.commands.after_command("whereami") do |n|
140
- # output.puts "command complete!"
141
- # end
142
- def after_command(search, &block)
143
- cmd = find_command_by_match_or_listing(search)
144
- cmd.hooks[:after] << block
145
- end
146
-
147
119
  def each(&block)
148
120
  @commands.each(&block)
149
121
  end
@@ -202,13 +174,13 @@ class Pry
202
174
  # Pry.config.commands.alias_command "lM", "ls -M"
203
175
  # @example Pass explicit description (overriding default).
204
176
  # Pry.config.commands.alias_command "lM", "ls -M", :desc => "cutiepie"
205
- def alias_command(match, action, options={})
177
+ def alias_command(match, action, options = {})
206
178
  cmd = find_command(action) or fail "Command: `#{action}` not found"
207
179
  original_options = cmd.options.dup
208
180
 
209
181
  options = original_options.merge!({
210
- :desc => "Alias for `#{action}`",
211
- :listing => match
182
+ desc: "Alias for `#{action}`",
183
+ listing: match
212
184
  }).merge!(options)
213
185
 
214
186
  # ensure default description is used if desc is nil
@@ -238,12 +210,12 @@ class Pry
238
210
  # command description to be passed this way too.
239
211
  # @example Renaming the `ls` command and changing its description.
240
212
  # Pry.config.commands.rename "dir", "ls", :description => "DOS friendly ls"
241
- def rename_command(new_match, search, options={})
213
+ def rename_command(new_match, search, options = {})
242
214
  cmd = find_command_by_match_or_listing(search)
243
215
 
244
216
  options = {
245
- :listing => new_match,
246
- :description => cmd.description
217
+ listing: new_match,
218
+ description: cmd.description
247
219
  }.merge!(options)
248
220
 
249
221
  @commands[new_match] = cmd.dup
@@ -253,7 +225,7 @@ class Pry
253
225
  @commands.delete(cmd.match)
254
226
  end
255
227
 
256
- def disabled_command(name_of_disabled_command, message, matcher=name_of_disabled_command)
228
+ def disabled_command(name_of_disabled_command, message, matcher = name_of_disabled_command)
257
229
  create_command name_of_disabled_command do
258
230
  match matcher
259
231
  description ""
@@ -275,7 +247,7 @@ class Pry
275
247
  # end
276
248
  # @example Getting
277
249
  # Pry.config.commands.desc "amend-line"
278
- def desc(search, description=nil)
250
+ def desc(search, description = nil)
279
251
  cmd = find_command_by_match_or_listing(search)
280
252
  return cmd.description if !description
281
253
 
@@ -298,7 +270,6 @@ class Pry
298
270
  helper_module.class_eval(&block)
299
271
  end
300
272
 
301
-
302
273
  # @return [Array]
303
274
  # The list of commands provided by the command set.
304
275
  def list_commands
@@ -345,6 +316,7 @@ class Pry
345
316
  unless Class === command && command < Pry::Command
346
317
  raise TypeError, "command is not a subclass of Pry::Command"
347
318
  end
319
+
348
320
  bind_command_to_pattern = pattern != command.match
349
321
  if bind_command_to_pattern
350
322
  command_copy = command.dup
@@ -387,9 +359,9 @@ class Pry
387
359
  # @param [String] val The line to execute
388
360
  # @param [Hash] context The context to execute the commands with
389
361
  # @return [CommandSet::Result]
390
- def process_line(val, context={})
391
- if command = find_command(val)
392
- context = context.merge(:command_set => self)
362
+ def process_line(val, context = {})
363
+ if (command = find_command(val))
364
+ context = context.merge(command_set: self)
393
365
  retval = command.new(context).process_line(val)
394
366
  Result.new(true, retval)
395
367
  else
@@ -407,13 +379,13 @@ class Pry
407
379
  # @param [String] search The line to search for
408
380
  # @param [Hash] context The context to create the command with
409
381
  # @return [Array<String>]
410
- def complete(search, context={})
411
- if command = find_command(search)
382
+ def complete(search, context = {})
383
+ if (command = find_command(search))
412
384
  command.new(context).complete(search)
413
385
  else
414
386
  @commands.keys.select do |key|
415
387
  String === key && key.start_with?(search)
416
- end.map{ |key| key + " " }
388
+ end.map { |key| key + " " }
417
389
  end
418
390
  end
419
391
  end
@@ -3,7 +3,7 @@ class Pry
3
3
  match(/amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/)
4
4
  group 'Editing'
5
5
  description 'Amend a line of input in multi-line mode.'
6
- command_options :interpolate => false, :listing => 'amend-line'
6
+ command_options interpolate: false, listing: 'amend-line'
7
7
 
8
8
  banner <<-'BANNER'
9
9
  Amend a line of input in multi-line mode. `amend-line N`, where the N represents
@@ -19,16 +19,15 @@ class Pry
19
19
  def process
20
20
  raise CommandError, "No input to amend." if eval_string.empty?
21
21
 
22
- eval_string.replace amended_input(eval_string)
22
+ eval_string.replace(amend_input)
23
23
  run "fix-indent"
24
24
  run "show-input"
25
25
  end
26
26
 
27
27
  private
28
28
 
29
- # @param [String] string The string to amend.
30
29
  # @return [String] A new string with the amendments applied to it.
31
- def amended_input(string)
30
+ def amend_input
32
31
  input_array = eval_string.each_line.to_a
33
32
 
34
33
  if arg_string == "!"
@@ -3,7 +3,7 @@ class Pry
3
3
  match(/^\s*!\s*$/)
4
4
  group 'Editing'
5
5
  description 'Clear the input buffer.'
6
- command_options :use_prefix => false
6
+ command_options use_prefix: false
7
7
 
8
8
  banner <<-'BANNER'
9
9
  Clear the input buffer. Useful if the parsing process goes wrong and you get
@@ -4,6 +4,7 @@ class Pry
4
4
  attr_reader :ex
5
5
  attr_reader :opts
6
6
  attr_reader :_pry_
7
+ include Pry::Helpers::Text
7
8
 
8
9
  def initialize(exception, _pry_, opts)
9
10
  @ex = exception
@@ -27,16 +28,18 @@ class Pry
27
28
  end
28
29
 
29
30
  def backtrace_level
30
- return @backtrace_level if @backtrace_level
31
-
32
- bl = if opts[:ex].nil?
31
+ @backtrace_level ||=
32
+ begin
33
+ bl =
34
+ if opts[:ex].nil?
33
35
  ex.bt_index
34
36
  else
35
37
  ex.bt_index = absolute_index_number(opts[:ex], ex.backtrace.size)
36
38
  end
37
39
 
38
- increment_backtrace_level
39
- @backtrace_level = bl
40
+ increment_backtrace_level
41
+ bl
42
+ end
40
43
  end
41
44
 
42
45
  def increment_backtrace_level
@@ -65,13 +68,12 @@ class Pry
65
68
 
66
69
  def header
67
70
  unindent %{
68
- #{Helpers::Text.bold 'Exception:'} #{ex.class}: #{ex.message}
71
+ #{bold 'Exception:'} #{ex.class}: #{ex.message}
69
72
  --
70
- #{Helpers::Text.bold('From:')} #{backtrace_file} @ line #{backtrace_line} @ #{Helpers::Text.bold("level: #{backtrace_level}")} of backtrace (of #{ex.backtrace.size - 1}).
73
+ #{bold('From:')} #{backtrace_file} @ line #{backtrace_line} @ #{bold("level: #{backtrace_level}")} of backtrace (of #{ex.backtrace.size - 1}).
71
74
 
72
75
  }
73
76
  end
74
-
75
77
  end
76
78
  end
77
79
  end
@@ -6,6 +6,8 @@ class Pry
6
6
  attr_reader :_pry_
7
7
 
8
8
  def initialize(file_with_embedded_line, _pry_, opts)
9
+ raise CommandError, "Must provide a filename, --in, or --ex." if !file_with_embedded_line
10
+
9
11
  @file_with_embedded_line = file_with_embedded_line
10
12
  @opts = opts
11
13
  @_pry_ = _pry_
@@ -13,8 +15,6 @@ class Pry
13
15
  end
14
16
 
15
17
  def format
16
- raise CommandError, "Must provide a filename, --in, or --ex." if !file_with_embedded_line
17
-
18
18
  set_file_and_dir_locals(file_name, _pry_, _pry_.current_context)
19
19
  decorate(@code_from_file)
20
20
  end
@@ -40,7 +40,11 @@ class Pry
40
40
  end
41
41
 
42
42
  def decorate(content)
43
- line_number ? super.around(line_number, code_window_size) : super
43
+ if line_number
44
+ super(content.around(line_number, code_window_size))
45
+ else
46
+ super
47
+ end
44
48
  end
45
49
 
46
50
  def code_type
@@ -36,7 +36,7 @@ class Pry
36
36
  end
37
37
 
38
38
  def normalized_expression_range
39
- absolute_index_range(opts[:i], input_expressions.length)
39
+ absolute_index_range(opts[:i], input_expressions.count)
40
40
  end
41
41
  end
42
42
  end
@@ -23,12 +23,12 @@ class Pry
23
23
  BANNER
24
24
 
25
25
  def options(opt)
26
- opt.on :ex, "Show the context of the last exception", :optional_argument => true, :as => Integer
27
- opt.on :i, :in, "Show one or more entries from Pry's expression history", :optional_argument => true, :as => Range, :default => -5..-1
28
- opt.on :s, :start, "Starting line (defaults to the first line)", :optional_argument => true, :as => Integer
29
- opt.on :e, :end, "Ending line (defaults to the last line)", :optional_argument => true, :as => Integer
26
+ opt.on :ex, "Show the context of the last exception", optional_argument: true, as: Integer
27
+ opt.on :i, :in, "Show one or more entries from Pry's expression history", optional_argument: true, as: Range, default: -5..-1
28
+ opt.on :s, :start, "Starting line (defaults to the first line)", optional_argument: true, as: Integer
29
+ opt.on :e, :end, "Ending line (defaults to the last line)", optional_argument: true, as: Integer
30
30
  opt.on :l, :'line-numbers', "Show line numbers"
31
- opt.on :t, :type, "The file type for syntax highlighting (e.g., 'ruby' or 'python')", :argument => true, :as => Symbol
31
+ opt.on :t, :type, "The file type for syntax highlighting (e.g., 'ruby' or 'python')", argument: true, as: Symbol
32
32
  end
33
33
 
34
34
  def process
@@ -36,7 +36,7 @@ class Pry
36
36
  when opts.present?(:ex)
37
37
  ExceptionFormatter.new(_pry_.last_exception, _pry_, opts).format
38
38
  when opts.present?(:in)
39
- InputExpressionFormatter.new(_pry_.input_array, opts).format
39
+ InputExpressionFormatter.new(_pry_.input_ring, opts).format
40
40
  else
41
41
  FileFormatter.new(args.first, _pry_, opts).format
42
42
  end
@@ -52,6 +52,7 @@ class Pry
52
52
  $LOAD_PATH.flat_map do |path|
53
53
  Dir[path + '/**/*'].map { |f|
54
54
  next if File.directory?(f)
55
+
55
56
  f.sub!(path + '/', '')
56
57
  }
57
58
  end
@@ -4,23 +4,43 @@ class Pry::Command::ChangePrompt < Pry::ClassCommand
4
4
  description 'Change the current prompt.'
5
5
  command_options argument_required: true
6
6
  banner <<-BANNER
7
- Usage: change-prompt NAME
7
+ Usage: change-prompt [OPTIONS] [NAME]
8
8
 
9
- Change the current prompt. See list-prompts for a list of available
10
- prompts.
9
+ Change the current prompt.
11
10
  BANNER
12
11
 
12
+ def options(opt)
13
+ opt.on(:l, :list, 'List the available prompts')
14
+ end
15
+
13
16
  def process(prompt)
14
- if prompt_map.key?(prompt)
15
- _pry_.prompt = prompt_map[prompt][:value]
17
+ if opts.present?(:l)
18
+ list_prompts
16
19
  else
17
- raise Pry::CommandError, "'#{prompt}' isn't a known prompt!"
20
+ change_prompt(prompt)
18
21
  end
19
22
  end
20
23
 
21
- private
22
- def prompt_map
23
- Pry::Prompt::MAP
24
+ private
25
+
26
+ def list_prompts
27
+ prompts = Pry::Prompt.all.map do |name, prompt|
28
+ "#{bold(name)}#{red(' (selected)') if _pry_.prompt == prompt[:value]}\n" +
29
+ prompt[:description]
30
+ end
31
+ output.puts(prompts.join("\n"))
24
32
  end
33
+
34
+ def change_prompt(prompt)
35
+ if Pry::Prompt.all.key?(prompt)
36
+ _pry_.prompt = Pry::Prompt.all[prompt][:value]
37
+ else
38
+ raise(Pry::CommandError, <<MSG)
39
+ '#{prompt}' isn't a known prompt. Run `change-prompt --list` to see
40
+ the list of known prompts.
41
+ MSG
42
+ end
43
+ end
44
+
25
45
  Pry::Commands.add_command(self)
26
46
  end