summaryrefslogtreecommitdiff
path: root/lib/pry/commands.rb
blob: 02958f09daf600b3193bdfcd28cdcdd78b8793f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
require "optparse"
require "method_source"
require "pry/command_base"
require "pry/pry_instance"

begin
  require "pry-doc"
rescue LoadError
end

class Pry

  # Default commands used by Pry.
  class Commands < CommandBase

    # We make this a lambda to avoid documenting it
    meth_name_from_binding = lambda do |b|
      meth_name = b.eval('__method__')

      # :__script__ for rubinius
      if [:__script__, nil, :__binding__, :__binding_impl__].include?(meth_name)
        nil
      else
        meth_name
      end
    end

    check_for_dynamically_defined_method = lambda do |file|
      if file =~ /(\(.*\))|<.*>/
        raise "Cannot retrieve source for dynamically defined method."
      end
    end

    remove_first_word = lambda do |text|
      text.split.drop(1).join(' ')
    end

    get_method_object = lambda do |meth_name, target, options|
      if options[:M]
        target.eval("instance_method(:#{meth_name})")
      elsif options[:m]
        target.eval("method(:#{meth_name})")
      else
        begin
          target.eval("instance_method(:#{meth_name})")
        rescue
          target.eval("method(:#{meth_name})")
        end
      end
    end

    make_header = lambda do |file, line, code_type|
      header = case code_type
               when :ruby
                 "--\nFrom #{file} @ line #{line}:\n--"
               else
                 "--\nFrom Ruby Core (C Method):\n--"
               end
    end

    command "!", "Clear the input buffer. Useful if the parsing process goes wrong and you get stuck in the read loop." do
      output.puts "Input buffer cleared!"
      opts[:eval_string].clear
    end

    command "!pry", "Start a Pry session on current self; this even works mid-expression." do
      Pry.start(target)
    end

    command "exit-program", "End the current program. Aliases: quit-program, !!!" do
      exit
    end

    alias_command "quit-program", "exit-program", ""
    alias_command "!!!", "exit-program", ""

    command "toggle-color", "Toggle syntax highlighting." do
      Pry.color = !Pry.color
      output.puts "Syntax highlighting #{Pry.color ? "on" : "off"}"
    end

    command "simple-prompt", "Toggle the simple prompt." do
      case Pry.active_instance.prompt
      when Pry::SIMPLE_PROMPT
        Pry.active_instance.prompt = Pry::DEFAULT_PROMPT
      else
        Pry.active_instance.prompt = Pry::SIMPLE_PROMPT
      end
    end

    command "nesting", "Show nesting information." do 
      nesting = opts[:nesting]
      
      output.puts "Nesting status:"
      output.puts "--"
      nesting.each do |level, obj|
        if level == 0
          output.puts "#{level}. #{Pry.view_clip(obj)} (Pry top level)"
        else
          output.puts "#{level}. #{Pry.view_clip(obj)}"
        end
      end
    end

    command "status", "Show status information." do
      nesting = opts[:nesting]
      
      output.puts "Status:"
      output.puts "--"
      output.puts "Receiver: #{Pry.view_clip(target.eval('self'))}"
      output.puts "Nesting level: #{nesting.level}"
      output.puts "Pry version: #{Pry::VERSION}"
      output.puts "Ruby version: #{RUBY_VERSION}"

      mn = meth_name_from_binding.call(target)
      output.puts "Current method: #{mn ? mn : "N/A"}"
      output.puts "Pry instance: #{Pry.active_instance}"
      output.puts "Last result: #{Pry.view(Pry.last_result)}"
    end

    command "whereami", "Show the code context for the session." do
      file = target.eval('__FILE__')
      line_num = target.eval('__LINE__')
      klass = target.eval('self.class')

      meth_name = meth_name_from_binding.call(target)
      if !meth_name
        output.puts "Cannot find containing method. Did you remember to use \`binding.pry\` ?"
        next
      end

      check_for_dynamically_defined_method.call(file)
     
      output.puts "--\nFrom #{file} @ line #{line_num} in #{klass}##{meth_name}:\n--"
      
      # This method inspired by http://rubygems.org/gems/ir_b
      File.open(file).each_with_index do |line, index|
        line_n = index + 1
        next unless line_n > (line_num - 6)
        break if line_n > (line_num + 5)
        if line_n == line_num
          code =" =>#{line_n.to_s.rjust(3)}: #{line.chomp}"
          if Pry.color
            code = CodeRay.scan(code, :ruby).term
          end
          output.puts code
          code
        else
          code = "#{line_n.to_s.rjust(6)}: #{line.chomp}"
          if Pry.color
            code = CodeRay.scan(code, :ruby).term
          end
          output.puts code
          code
        end
      end
    end
    
    command "version", "Show Pry version." do
      output.puts "Pry version: #{Pry::VERSION} on Ruby #{RUBY_VERSION}."
    end
    
    command "exit-all", "End all nested Pry sessions. Accepts optional return value. Aliases: !@" do 
      str = remove_first_word.call(opts[:val])
      throw(:breakout, [0, target.eval(str)])
    end

    alias_command "!@", "exit-all", ""

    command "ls", "Show the list of vars in the current scope. Type `ls --help` for more info." do |*args|
      options = {}
      
      # Set target local to the default -- note that we can set a different target for
      # ls if we like: e.g ls my_var
      target = target()
      
      OptionParser.new do |opts|
        opts.banner = %{Usage: ls [OPTIONS] [VAR]\n\
List information about VAR (the current context by default).
Shows local and instance variables by default.
--
}
        opts.on("-g", "--globals", "Display global variables.") do
          options[:g] = true
        end
        
        opts.on("-c", "--constants", "Display constants.") do
          options[:c] = true
        end

        opts.on("-l", "--locals", "Display locals.") do
          options[:l] = true
        end

        opts.on("-i", "--ivars", "Display instance variables.") do 
          options[:i] = true
        end

        opts.on("-k", "--class-vars", "Display class variables.") do 
          options[:k] = true
        end        

        opts.on("-m", "--methods", "Display methods (public methods by default).") do 
          options[:m] = true
        end

        opts.on("-M", "--instance-methods", "Display instance methods (only relevant to classes and modules).") do
          options[:M] = true
        end

        opts.on("-P", "--public", "Display public methods (with -m).") do 
          options[:P] = true
        end

        opts.on("-r", "--protected", "Display protected methods (with -m).") do 
          options[:r] = true
        end        

        opts.on("-p", "--private", "Display private methods (with -m).") do 
          options[:p] = true
        end

        opts.on("-j", "--just-singletons", "Display just the singleton methods (with -m).") do 
          options[:j] = true
        end        

        opts.on("-s", "--super", "Include superclass entries (relevant to constant and methods options).") do 
          options[:s] = true
        end
        
        opts.on("-a", "--all", "Display all types of entries.") do
          options[:a] = true
        end

        opts.on("-v", "--verbose", "Verbose ouput.") do 
          options[:v] = true
        end

        opts.on_tail("-h", "--help", "Show this message.") do
          output.puts opts
          options[:h] = true
        end
      end.order(args) do |new_target|
        target = Pry.binding_for(target.eval("#{new_target}")) if !options[:h]
      end

      # exit if we've displayed help
      next if options[:h]

      # default is locals/ivars/class vars.
      # Only occurs when no options or when only option is verbose
      options.merge!({
                       :l => true,
                       :i => true,
                       :k => true
                     }) if options.empty? || (options.size == 1 && options[:v])

      # Display public methods by default if -m or -M switch is used.
      options[:P] = true if (options[:m] || options[:M]) && !(options[:p] || options[:r] || options[:j])
      
      info = {}
      target_self = target.eval('self')

      # ensure we have a real boolean and not a `nil` (important when
      # interpolating in the string)
      options[:s] = !!options[:s]
      
      # Numbers (e.g 0, 1, 2) are for ordering the hash values in Ruby 1.8
      i = -1

      # Start collecting the entries selected by the user
      info["local variables"] = [Array(target.eval("local_variables")).sort, i += 1] if options[:l] || options[:a]
      info["instance variables"] = [Array(target.eval("instance_variables")).sort, i += 1] if options[:i] || options[:a]

      info["class variables"] = [if target_self.is_a?(Module)
                                   Array(target.eval("class_variables")).sort
                                 else
                                   Array(target.eval("self.class.class_variables")).sort
                                 end, i += 1] if options[:k] || options[:a]

      info["global variables"] = [Array(target.eval("global_variables")).sort, i += 1] if options[:g] || options[:a]
      
      info["public methods"] = [Array(target.eval("public_methods(#{options[:s]})")).uniq.sort, i += 1] if (options[:m] && options[:P]) || options[:a]

      info["protected methods"] = [Array(target.eval("protected_methods(#{options[:s]})")).sort, i += 1] if (options[:m] && options[:r]) || options[:a]

      info["private methods"] = [Array(target.eval("private_methods(#{options[:s]})")).sort, i += 1] if (options[:m] && options[:p]) || options[:a]

      info["just singleton methods"] = [Array(target.eval("methods(#{options[:s]})")).sort, i += 1] if (options[:m] && options[:j]) || options[:a]
      
      info["public instance methods"] = [Array(target.eval("public_instance_methods(#{options[:s]})")).uniq.sort, i += 1] if target_self.is_a?(Module) && ((options[:M] && options[:P]) || options[:a])

      info["protected instance methods"] = [Array(target.eval("protected_instance_methods(#{options[:s]})")).uniq.sort, i += 1] if target_self.is_a?(Module) && ((options[:M] && options[:r]) || options[:a])

      info["private instance methods"] = [Array(target.eval("private_instance_methods(#{options[:s]})")).uniq.sort, i += 1] if target_self.is_a?(Module) && ((options[:M] && options[:p]) || options[:a])
      
      # dealing with 1.8/1.9 compatibility issues :/
      csuper = options[:s]
      if Module.method(:constants).arity == 0
        csuper = nil
      end
      
      info["constants"] = [Array(target_self.is_a?(Module) ? target.eval("constants(#{csuper})") :
                                 target.eval("self.class.constants(#{csuper})")).uniq.sort, i += 1] if options[:c] || options[:a]

      # verbose output?
      if options[:v]

        # verbose
        info.sort_by { |k, v| v.last }.each do |k, v|
          if !v.first.empty?
            output.puts "#{k}:\n--"
            if Pry.color
              output.puts CodeRay.scan(Pry.view(v.first), :ruby).term
            else
              output.puts Pry.view(v.first)
            end
            output.puts
          end
        end

      # plain
      else
        list = info.values.sort_by(&:last).map(&:first).inject(&:+)
        list.uniq! if list
        if Pry.color
          output.puts CodeRay.scan(Pry.view(list), :ruby).term
        else
          output.puts Pry.view(list)
        end
        list
      end
    end

    command "cat-file", "Show output of file FILE" do |file_name|
      if !file_name
        output.puts "Must provide a file name."
        next
      end

      contents = File.read(File.expand_path(file_name))
      output.puts contents
      contents
    end

    command "eval-file", "Eval a Ruby script. Type `eval-file --help` for more info." do |*args|
      options = {}
      target = target()
      file_name = nil
      
      OptionParser.new do |opts|
        opts.banner = %{Usage: eval-file [OPTIONS] FILE
Eval a Ruby script at top-level or in the specified context. Defaults to top-level.
e.g: eval-file -c self "hello.rb"
--
}
        opts.on("-c", "--context CONTEXT", "Eval the script in the specified context.") do |context|
          options[:c] = true
          target = Pry.binding_for(target.eval(context))
        end

        opts.on_tail("-h", "--help", "This message.") do 
          output.puts opts
          options[:h] = true
        end
      end.order(args) do |v|
        file_name = v
      end

      next if options[:h]

      if !file_name
        output.puts "You need to specify a file name. Type `eval-file --help` for help"
        next
      end

      old_constants = Object.constants
      if options[:c]
        target_self = target.eval('self')
        target.eval(File.read(File.expand_path(file_name)))
        output.puts "--\nEval'd '#{file_name}' in the `#{target_self}`  context."
      else
        TOPLEVEL_BINDING.eval(File.read(File.expand_path(file_name)))
        output.puts "--\nEval'd '#{file_name}' at top-level."
      end
      new_constants = Object.constants - old_constants
      output.puts "Brought in the following top-level constants: #{new_constants.inspect}" if !new_constants.empty?
    end      

    command "cat", "Show output of VAR.inspect. Aliases: inspect" do |obj|
      if !obj
        output.puts "Must provide an object to inspect."
        next
      end
      
      output.puts Pry.view(target.eval("#{obj}"))
    end

    alias_command "inspect", "cat", ""
    
    command "cd", "Start a Pry session on VAR (use `cd ..` to go back and `cd /` to return to Pry top-level)",  :keep_retval => true do |obj|
      if !obj
        output.puts "Must provide an object."
        next
      end
      
      throw(:breakout, opts[:nesting].level) if obj == ".."

      if obj == "/" 
        throw(:breakout, 1) if opts[:nesting].level > 0
        next
      end    

      target.eval("#{obj}.pry")
    end

    process_comment_markup = lambda do |comment, code_type|
      comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { Pry.color ? CodeRay.scan($1, code_type).term : $1 }.
        gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { Pry.color ? "\e[32m#{$1}\e[0m": $1 }.
        gsub(/<i>(?:\s*\n)?(.*?)\s*<\/i>/m) { Pry.color ? "\e[34m#{$1}\e[0m" : $1 }.
        gsub(/\B\+(\w*?)\+\B/)  { Pry.color ? "\e[32m#{$1}\e[0m": $1 }.
        gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/)  { Pry.color ? CodeRay.scan($1, code_type).term : $1 }
    end

    strip_leading_hash_from_ruby_comments = lambda do |comment|
      comment.gsub /^\s*#\s*/, ''
    end

    command "show-doc", "Show the comments above METH. Type `show-doc --help` for more info." do |*args|
      options = {}
      target = target()
      meth_name = nil
      
      OptionParser.new do |opts|
        opts.banner = %{Usage: show-doc [OPTIONS] [METH]
Show the comments above method METH. Tries instance methods first and then methods by default.
e.g show-doc hello_method
--
}
        opts.on("-M", "--instance-methods", "Operate on instance methods.") do 
          options[:M] = true
        end

        opts.on("-m", "--methods", "Operate on methods.") do 
          options[:m] = true
        end

        opts.on("-c", "--context CONTEXT", "Select object context to run under.") do |context|
          target = Pry.binding_for(target.eval(context))
        end

        opts.on_tail("-h", "--help", "This message.") do 
          output.puts opts
          options[:h] = true
        end
      end.order(args) do |v|
        meth_name = v
      end

      next if options[:h]

      if !meth_name
        output.puts "You need to specify a method. Type `show-doc --help` for help"
        next
      end
      
      begin
        meth = get_method_object.call(meth_name, target, options)
      rescue
        output.puts "Invalid method name: #{meth_name}. Type `show-doc --help` for help"
        next
      end

      code_type = :ruby
      if Pry.has_pry_doc && meth.source_location.nil?
        info = Pry::MethodInfo.info_for(meth)
        if !info
          output.puts "Cannot find docs for C method: #{meth_name}"
          next
        end
        doc = info.docstring
        code_type = info.source_type
      else
        begin
          doc = meth.comment
        rescue
          output.puts "Cannot locate source for this method: #{meth_name}. Try `gem install pry-doc` to get access to Ruby Core documentation."
          next
        end
        doc = strip_leading_hash_from_ruby_comments.call(doc)
      end

      doc = process_comment_markup.call(doc, code_type)
      
      file, line = meth.source_location
      check_for_dynamically_defined_method.call(file)

      output.puts make_header.call(file, line, code_type)
      
      output.puts doc
      doc
    end

    strip_comments_from_c_code = lambda do |code|
      code.sub /\A\s*\/\*.*?\*\/\s*/m, ''
    end
    
    command "show-method", "Show the source for METH. Type `show-method --help` for more info." do |*args|
      options = {}
      target = target()
      meth_name = nil
      
      OptionParser.new do |opts|
        opts.banner = %{Usage: show-method [OPTIONS] [METH]
Show the source for method METH. Tries instance methods first and then methods by default.
e.g: show-method hello_method
--
}
        opts.on("-M", "--instance-methods", "Operate on instance methods.") do 
          options[:M] = true
        end

        opts.on("-m", "--methods", "Operate on methods.") do 
          options[:m] = true
        end

        opts.on("-c", "--context CONTEXT", "Select object context to run under.") do |context|
          target = Pry.binding_for(target.eval(context))
        end

        opts.on_tail("-h", "--help", "This message.") do 
          output.puts opts
          options[:h] = true
        end
      end.order(args) do |v|
        meth_name = v
      end

      next if options[:h]

      # If no method name is given then use current method, if it exists
      meth_name = meth_name_from_binding.call(target) if !meth_name
      if !meth_name
        output.puts "You need to specify a method. Type `show-method --help` for help"
        next
      end
      
      begin
        meth = get_method_object.call(meth_name, target, options)
      rescue
        output.puts "Invalid method name: #{meth_name}. Type `show-method --help` for help"
        next
      end

      code_type = :ruby

      # Try to find source for C methods using MethodInfo (if possible)
      if Pry.has_pry_doc && meth.source_location.nil?
        info = Pry::MethodInfo.info_for(meth)
        if !info || !info.source
          output.puts "Cannot find source for C method: #{meth_name}"
          next
        end
        code = info.source
        code = strip_comments_from_c_code.call(code)
        code_type = info.source_type
      else
        begin
          code = meth.source
        rescue
          output.puts "Cannot locate source for this method: #{meth_name}. Try `gem install pry-doc` to get access to Ruby Core documentation."
          next
        end
      end
      
      file, line = meth.source_location
      check_for_dynamically_defined_method.call(file)
      
      output.puts make_header.call(file, line, code_type)

      if Pry.color
        code = CodeRay.scan(code, code_type).term
      end
      
      output.puts code
      code
    end
    
    command "show-command", "Show sourcecode for a Pry command, e.g: show-command cd" do |command_name|
      if !command_name
        output.puts "You must provide a command name."
        next
      end
      
      if commands[command_name]
        meth = commands[command_name][:action]

        code = meth.source
        file, line = meth.source_location
        check_for_dynamically_defined_method.call(file)

        output.puts "--\nFrom #{file} @ line #{line}:\n--"

        if Pry.color
          code = CodeRay.scan(code, :ruby).term
        end

        output.puts code
        code
      else
        output.puts "No such command: #{command_name}."
      end
    end
    
    command "jump-to", "Jump to a Pry session further up the stack, exiting all sessions below." do |break_level|
      break_level = break_level.to_i
      nesting = opts[:nesting]

      case break_level
      when nesting.level
        output.puts "Already at nesting level #{nesting.level}"
      when (0...nesting.level)
        throw(:breakout, break_level + 1)
      else
        max_nest_level = nesting.level - 1
        output.puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{break_level}."
      end
    end

    command "exit", "End the current Pry session. Accepts optional return value. Aliases: quit, back" do 
      str = remove_first_word.call(opts[:val])
      throw(:breakout, [opts[:nesting].level, target.eval(str)])
    end

    alias_command "quit", "exit", ""
    alias_command "back", "exit", ""

    command "game", "" do |highest|
      highest = highest ? highest.to_i : 100
      num = rand(highest)
      output.puts "Guess the number between 0-#{highest}: ('.' to quit)"
      count = 0
      while(true)
        count += 1
        str = Readline.readline("game > ", true)
        break if str == "." || !str
        val = str.to_i
        output.puts "Too large!" if val > num
        output.puts "Too small!" if val < num
        if val == num
          output.puts "Well done! You guessed right! It took you #{count} guesses."
          break
        end
      end
    end

    command "east-coker", "" do
      text = %{
--
Now the light falls
Across the open field, leaving the deep lane
Shuttered with branches, dark in the afternoon,
Where you lean against a bank while a van passes,
And the deep lane insists on the direction
Into the village, in the electric heat
Hypnotised. In a warm haze the sultry light
Is absorbed, not refracted, by grey stone.
The dahlias sleep in the empty silence.
Wait for the early owl.
-- T.S Eliot
}
      output.puts text
      text
    end
    
    command "cohen-poem", "" do
      text = %{
--
When this American woman,
whose thighs are bound in casual red cloth,
comes thundering past my sitting place
like a forest-burning Mongol tribe,
the city is ravished
and brittle buildings of a hundred years
splash into the street;
and my eyes are burnt
for the embroidered Chinese girls,
already old,
and so small between the thin pines
on these enormous landscapes,
that if you turn your head
they are lost for hours.
-- Leonard Cohen                    
                }
  output.puts text
  text
end
end
end