pry 0.10.3 → 0.14.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +439 -16
  3. data/LICENSE +1 -1
  4. data/README.md +362 -302
  5. data/bin/pry +4 -7
  6. data/lib/pry/basic_object.rb +10 -0
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +84 -97
  10. data/lib/pry/code/code_file.rb +37 -26
  11. data/lib/pry/code/code_range.rb +7 -5
  12. data/lib/pry/code/loc.rb +26 -13
  13. data/lib/pry/code.rb +42 -31
  14. data/lib/pry/code_object.rb +53 -28
  15. data/lib/pry/color_printer.rb +46 -35
  16. data/lib/pry/command.rb +197 -369
  17. data/lib/pry/command_set.rb +89 -114
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -82
  20. data/lib/pry/commands/bang.rb +18 -14
  21. data/lib/pry/commands/bang_pry.rb +15 -11
  22. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  23. data/lib/pry/commands/cat/exception_formatter.rb +85 -72
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -46
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +62 -54
  27. data/lib/pry/commands/cd.rb +40 -35
  28. data/lib/pry/commands/change_inspector.rb +29 -22
  29. data/lib/pry/commands/change_prompt.rb +48 -23
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -131
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +23 -34
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  36. data/lib/pry/commands/edit.rb +185 -157
  37. data/lib/pry/commands/exit.rb +40 -35
  38. data/lib/pry/commands/exit_all.rb +24 -20
  39. data/lib/pry/commands/exit_program.rb +20 -16
  40. data/lib/pry/commands/find_method.rb +168 -162
  41. data/lib/pry/commands/fix_indent.rb +16 -12
  42. data/lib/pry/commands/help.rb +140 -133
  43. data/lib/pry/commands/hist.rb +151 -149
  44. data/lib/pry/commands/import_set.rb +20 -15
  45. data/lib/pry/commands/jump_to.rb +25 -21
  46. data/lib/pry/commands/list_inspectors.rb +35 -28
  47. data/lib/pry/commands/ls/constants.rb +59 -31
  48. data/lib/pry/commands/ls/formatter.rb +42 -36
  49. data/lib/pry/commands/ls/globals.rb +38 -36
  50. data/lib/pry/commands/ls/grep.rb +17 -15
  51. data/lib/pry/commands/ls/instance_vars.rb +29 -28
  52. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  53. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  54. data/lib/pry/commands/ls/local_names.rb +26 -24
  55. data/lib/pry/commands/ls/local_vars.rb +38 -30
  56. data/lib/pry/commands/ls/ls_entity.rb +47 -52
  57. data/lib/pry/commands/ls/methods.rb +49 -51
  58. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  59. data/lib/pry/commands/ls/self_methods.rb +23 -21
  60. data/lib/pry/commands/ls.rb +124 -103
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +92 -82
  63. data/lib/pry/commands/pry_backtrace.rb +22 -17
  64. data/lib/pry/commands/pry_version.rb +15 -11
  65. data/lib/pry/commands/raise_up.rb +33 -27
  66. data/lib/pry/commands/reload_code.rb +60 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -42
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +56 -29
  71. data/lib/pry/commands/shell_mode.rb +22 -18
  72. data/lib/pry/commands/show_doc.rb +80 -70
  73. data/lib/pry/commands/show_info.rb +194 -155
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +110 -42
  76. data/lib/pry/commands/stat.rb +35 -31
  77. data/lib/pry/commands/switch_to.rb +21 -15
  78. data/lib/pry/commands/toggle_color.rb +20 -16
  79. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  80. data/lib/pry/commands/watch_expression.rb +89 -84
  81. data/lib/pry/commands/whereami.rb +156 -141
  82. data/lib/pry/commands/wtf.rb +78 -40
  83. data/lib/pry/config/attributable.rb +22 -0
  84. data/lib/pry/config/lazy_value.rb +29 -0
  85. data/lib/pry/config/memoized_value.rb +34 -0
  86. data/lib/pry/config/value.rb +24 -0
  87. data/lib/pry/config.rb +310 -20
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +22 -9
  90. data/lib/pry/editor.rb +56 -34
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +13 -18
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +20 -62
  96. data/lib/pry/helpers/command_helpers.rb +52 -62
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -12
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  99. data/lib/pry/helpers/platform.rb +55 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +96 -85
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +81 -55
  104. data/lib/pry/hooks.rb +60 -110
  105. data/lib/pry/indent.rb +74 -68
  106. data/lib/pry/input_completer.rb +199 -158
  107. data/lib/pry/input_lock.rb +7 -10
  108. data/lib/pry/inspector.rb +36 -24
  109. data/lib/pry/last_exception.rb +45 -45
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/method/patcher.rb +14 -8
  112. data/lib/pry/method/weird_method_locator.rb +79 -45
  113. data/lib/pry/method.rb +178 -124
  114. data/lib/pry/object_path.rb +37 -28
  115. data/lib/pry/output.rb +102 -16
  116. data/lib/pry/pager.rb +187 -174
  117. data/lib/pry/prompt.rb +213 -25
  118. data/lib/pry/pry_class.rb +119 -98
  119. data/lib/pry/pry_instance.rb +261 -224
  120. data/lib/pry/repl.rb +83 -29
  121. data/lib/pry/repl_file_loader.rb +27 -22
  122. data/lib/pry/ring.rb +89 -0
  123. data/lib/pry/slop/LICENSE +20 -0
  124. data/lib/pry/slop/commands.rb +190 -0
  125. data/lib/pry/slop/option.rb +210 -0
  126. data/lib/pry/slop.rb +672 -0
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable/evalable.rb +24 -0
  130. data/lib/pry/testable/mockable.rb +22 -0
  131. data/lib/pry/testable/pry_tester.rb +88 -0
  132. data/lib/pry/testable/utility.rb +34 -0
  133. data/lib/pry/testable/variables.rb +52 -0
  134. data/lib/pry/testable.rb +68 -0
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +20 -0
  137. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +35 -32
  138. data/lib/pry/wrapped_module.rb +68 -63
  139. data/lib/pry.rb +133 -149
  140. metadata +58 -69
  141. data/lib/pry/commands/disabled_commands.rb +0 -2
  142. data/lib/pry/commands/gem_cd.rb +0 -26
  143. data/lib/pry/commands/gem_install.rb +0 -32
  144. data/lib/pry/commands/gem_list.rb +0 -33
  145. data/lib/pry/commands/gem_open.rb +0 -29
  146. data/lib/pry/commands/gist.rb +0 -101
  147. data/lib/pry/commands/install_command.rb +0 -53
  148. data/lib/pry/commands/list_prompts.rb +0 -35
  149. data/lib/pry/commands/simple_prompt.rb +0 -22
  150. data/lib/pry/commands.rb +0 -6
  151. data/lib/pry/config/behavior.rb +0 -139
  152. data/lib/pry/config/convenience.rb +0 -25
  153. data/lib/pry/config/default.rb +0 -161
  154. data/lib/pry/history_array.rb +0 -121
  155. data/lib/pry/plugins.rb +0 -103
  156. data/lib/pry/rbx_path.rb +0 -22
  157. data/lib/pry/rubygem.rb +0 -82
  158. data/lib/pry/terminal.rb +0 -79
  159. data/lib/pry/test/helper.rb +0 -170
@@ -1,62 +1,74 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::ReloadCode < Pry::ClassCommand
3
- match 'reload-code'
4
- group 'Misc'
5
- description 'Reload the source file that contains the specified code object.'
6
-
7
- banner <<-'BANNER'
8
- Reload the source file that contains the specified code object.
9
-
10
- e.g reload-code MyClass#my_method #=> reload a method
11
- reload-code MyClass #=> reload a class
12
- reload-code my-command #=> reload a pry command
13
- reload-code self #=> reload the current object
14
- reload-code #=> reload the current file or object
15
- BANNER
16
-
17
- def process
18
- if !args.empty?
19
- reload_object(args.join(" "))
20
- elsif internal_binding?(target)
21
- reload_object("self")
22
- else
23
- reload_current_file
4
+ class Command
5
+ class ReloadCode < Pry::ClassCommand
6
+ match 'reload-code'
7
+ group 'Misc'
8
+ description 'Reload the source file that contains the specified code object.'
9
+
10
+ banner <<-'BANNER'
11
+ Reload the source file that contains the specified code object.
12
+
13
+ e.g reload-code MyClass#my_method #=> reload a method
14
+ reload-code MyClass #=> reload a class
15
+ reload-code my-command #=> reload a pry command
16
+ reload-code self #=> reload the current object
17
+ reload-code #=> reload the current file or object
18
+ BANNER
19
+
20
+ def process
21
+ if !args.empty?
22
+ reload_object(args.join(" "))
23
+ elsif internal_binding?(target)
24
+ reload_object("self")
25
+ else
26
+ reload_current_file
27
+ end
24
28
  end
25
- end
26
29
 
27
- private
30
+ private
28
31
 
29
- def current_file
30
- File.expand_path target.eval("__FILE__")
31
- end
32
+ def current_file
33
+ file =
34
+ if target.respond_to?(:source_location)
35
+ target.source_location.first
36
+ else
37
+ target.eval("__FILE__")
38
+ end
39
+ File.expand_path file
40
+ end
32
41
 
33
- def reload_current_file
34
- if !File.exists?(current_file)
35
- raise CommandError, "Current file: #{current_file} cannot be found on disk!"
42
+ def reload_current_file
43
+ unless File.exist?(current_file)
44
+ raise CommandError, "Current file: #{current_file} cannot be found on disk!"
45
+ end
46
+
47
+ load current_file
48
+ output.puts "The current file: #{current_file} was reloaded!"
36
49
  end
37
50
 
38
- load current_file
39
- output.puts "The current file: #{current_file} was reloaded!"
40
- end
51
+ def reload_object(identifier)
52
+ code_object = Pry::CodeObject.lookup(identifier, pry_instance)
53
+ check_for_reloadability(code_object, identifier)
54
+ load code_object.source_file
55
+ output.puts "#{identifier} was reloaded!"
56
+ end
41
57
 
42
- def reload_object(identifier)
43
- code_object = Pry::CodeObject.lookup(identifier, _pry_)
44
- check_for_reloadability(code_object, identifier)
45
- load code_object.source_file
46
- output.puts "#{identifier} was reloaded!"
47
- end
58
+ def check_for_reloadability(code_object, identifier)
59
+ if !code_object || !code_object.source_file
60
+ raise CommandError, "Cannot locate #{identifier}!"
61
+ end
62
+
63
+ return if File.exist?(code_object.source_file)
48
64
 
49
- def check_for_reloadability(code_object, identifier)
50
- if !code_object || !code_object.source_file
51
- raise CommandError, "Cannot locate #{identifier}!"
52
- elsif !File.exists?(code_object.source_file)
53
65
  raise CommandError,
54
- "Cannot reload #{identifier} as it has no associated file on disk. " \
55
- "File found was: #{code_object.source_file}"
66
+ "Cannot reload #{identifier} as it has no associated file on disk. " \
67
+ "File found was: #{code_object.source_file}"
56
68
  end
57
69
  end
58
- end
59
70
 
60
- Pry::Commands.add_command(Pry::Command::ReloadCode)
61
- Pry::Commands.alias_command 'reload-method', 'reload-code'
71
+ Pry::Commands.add_command(Pry::Command::ReloadCode)
72
+ Pry::Commands.alias_command 'reload-method', 'reload-code'
73
+ end
62
74
  end
@@ -1,18 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::Reset < Pry::ClassCommand
3
- match 'reset'
4
- group 'Context'
5
- description 'Reset the REPL to a clean state.'
4
+ class Command
5
+ class Reset < Pry::ClassCommand
6
+ match 'reset'
7
+ group 'Context'
8
+ description 'Reset the REPL to a clean state.'
6
9
 
7
- banner <<-'BANNER'
8
- Reset the REPL to a clean state.
9
- BANNER
10
+ banner <<-'BANNER'
11
+ Reset the REPL to a clean state.
12
+ BANNER
10
13
 
11
- def process
12
- output.puts 'Pry reset.'
13
- exec 'pry'
14
+ def process
15
+ output.puts 'Pry reset.'
16
+ exec 'pry'
17
+ end
14
18
  end
15
- end
16
19
 
17
- Pry::Commands.add_command(Pry::Command::Reset)
20
+ Pry::Commands.add_command(Pry::Command::Reset)
21
+ end
18
22
  end
@@ -1,60 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'stringio'
4
+
1
5
  class Pry
2
- class Command::Ri < Pry::ClassCommand
3
- match 'ri'
4
- group 'Introspection'
5
- description 'View ri documentation.'
6
+ class Command
7
+ class Ri < Pry::ClassCommand
8
+ match 'ri'
9
+ group 'Introspection'
10
+ description 'View ri documentation.'
6
11
 
7
- banner <<-'BANNER'
8
- Usage: ri [spec]
12
+ banner <<-'BANNER'
13
+ Usage: ri [spec]
9
14
 
10
- View ri documentation. Relies on the "rdoc" gem being installed.
11
- See also "show-doc" command.
15
+ View ri documentation. Relies on the "rdoc" gem being installed.
16
+ See also "show-doc" command.
12
17
 
13
- ri Array#each
14
- BANNER
18
+ ri Array#each
19
+ BANNER
15
20
 
16
- def process(spec)
17
- # Lazily load RI
18
- require 'rdoc/ri/driver'
21
+ def process(spec)
22
+ unless spec
23
+ return output.puts(
24
+ "Please provide a class, module, or method name (e.g: ri Array#push)"
25
+ )
26
+ end
19
27
 
20
- unless defined? RDoc::RI::PryDriver
28
+ # Lazily load RI
29
+ require 'rdoc/ri/driver'
21
30
 
22
- # Subclass RI so that it formats its output nicely, and uses `lesspipe`.
23
- subclass = Class.new(RDoc::RI::Driver) # the hard way.
31
+ unless defined? RDoc::RI::PryDriver
24
32
 
25
- subclass.class_eval do
26
- def initialize(pager, opts)
27
- @pager = pager
28
- super opts
29
- end
30
- def page
31
- paging_text = StringIO.new
32
- yield paging_text
33
- @pager.page(paging_text.string)
34
- end
33
+ # Subclass RI so that it formats its output nicely, and uses `lesspipe`.
34
+ subclass = Class.new(RDoc::RI::Driver) # the hard way.
35
+
36
+ subclass.class_eval do
37
+ def initialize(pager, opts)
38
+ @pager = pager
39
+ super opts
40
+ end
35
41
 
36
- def formatter(io)
37
- if @formatter_klass
38
- @formatter_klass.new
39
- else
40
- RDoc::Markup::ToAnsi.new
42
+ def page
43
+ paging_text = StringIO.new
44
+ yield paging_text
45
+ @pager.page(paging_text.string)
46
+ end
47
+
48
+ def formatter(_io)
49
+ if @formatter_klass
50
+ @formatter_klass.new
51
+ else
52
+ RDoc::Markup::ToAnsi.new
53
+ end
41
54
  end
42
55
  end
43
- end
44
56
 
45
- RDoc::RI.const_set :PryDriver, subclass # hook it up!
46
- end
57
+ RDoc::RI.const_set :PryDriver, subclass # hook it up!
58
+ end
47
59
 
48
- # Spin-up an RI insance.
49
- ri = RDoc::RI::PryDriver.new _pry_.pager, :use_stdout => true, :interactive => false
60
+ # Spin-up an RI insance.
61
+ ri = RDoc::RI::PryDriver.new(
62
+ pry_instance.pager, use_stdout: true, interactive: false
63
+ )
50
64
 
51
- begin
52
- ri.display_names [spec] # Get the documentation (finally!)
53
- rescue RDoc::RI::Driver::NotFoundError => e
54
- output.puts "error: '#{e.name}' not found"
65
+ begin
66
+ ri.display_names [spec] # Get the documentation (finally!)
67
+ rescue RDoc::RI::Driver::NotFoundError => e
68
+ output.puts "error: '#{e.name}' not found"
69
+ end
55
70
  end
56
71
  end
57
- end
58
72
 
59
- Pry::Commands.add_command(Pry::Command::Ri)
73
+ Pry::Commands.add_command(Pry::Command::Ri)
74
+ end
60
75
  end
@@ -1,61 +1,63 @@
1
- require 'pry/commands/code_collector'
1
+ # frozen_string_literal: true
2
2
 
3
3
  class Pry
4
- class Command::SaveFile < Pry::ClassCommand
5
- match 'save-file'
6
- group 'Input and Output'
7
- description 'Export to a file using content from the REPL.'
4
+ class Command
5
+ class SaveFile < Pry::ClassCommand
6
+ match 'save-file'
7
+ group 'Input and Output'
8
+ description 'Export to a file using content from the REPL.'
8
9
 
9
- banner <<-'BANNER'
10
- Usage: save-file [OPTIONS] --to [FILE]
10
+ banner <<-'BANNER'
11
+ Usage: save-file [OPTIONS] --to [FILE]
11
12
 
12
- Export to a file using content from the REPL.
13
+ Export to a file using content from the REPL.
13
14
 
14
- save-file my_method --to hello.rb
15
- save-file -i 1..10 --to hello.rb --append
16
- save-file show-method --to my_command.rb
17
- save-file sample_file.rb --lines 2..10 --to output_file.rb
18
- BANNER
15
+ save-file my_method --to hello.rb
16
+ save-file -i 1..10 --to hello.rb --append
17
+ save-file show-method --to my_command.rb
18
+ save-file sample_file.rb --lines 2..10 --to output_file.rb
19
+ BANNER
19
20
 
20
- def options(opt)
21
- CodeCollector.inject_options(opt)
21
+ def options(opt)
22
+ CodeCollector.inject_options(opt)
22
23
 
23
- opt.on :to=, "Specify the output file path"
24
- opt.on :a, :append, "Append output to file"
25
- end
24
+ opt.on :to=, "Specify the output file path"
25
+ opt.on :a, :append, "Append output to file"
26
+ end
26
27
 
27
- def process
28
- @cc = CodeCollector.new(args, opts, _pry_)
29
- raise CommandError, "Found no code to save." if @cc.content.empty?
28
+ def process
29
+ @cc = CodeCollector.new(args, opts, pry_instance)
30
+ raise CommandError, "Found no code to save." if @cc.content.empty?
30
31
 
31
- if !file_name
32
- display_content
33
- else
34
- save_file
32
+ if !file_name
33
+ display_content
34
+ else
35
+ save_file
36
+ end
35
37
  end
36
- end
37
38
 
38
- def file_name
39
- opts[:to] || nil
40
- end
39
+ def file_name
40
+ opts[:to] || nil
41
+ end
41
42
 
42
- def save_file
43
- File.open(file_name, mode) do |f|
44
- f.puts @cc.content
43
+ def save_file
44
+ File.open(file_name, mode) do |f|
45
+ f.puts @cc.content
46
+ end
47
+ output.puts "#{file_name} successfully saved"
45
48
  end
46
- output.puts "#{file_name} successfully saved"
47
- end
48
49
 
49
- def display_content
50
- output.puts @cc.content
51
- output.puts "\n\n--\nPlease use `--to FILE` to export to a file."
52
- output.puts "No file saved!\n--"
53
- end
50
+ def display_content
51
+ output.puts @cc.content
52
+ output.puts "\n\n--\nPlease use `--to FILE` to export to a file."
53
+ output.puts "No file saved!\n--"
54
+ end
54
55
 
55
- def mode
56
- opts.present?(:append) ? "a" : "w"
56
+ def mode
57
+ opts.present?(:append) ? "a" : "w"
58
+ end
57
59
  end
58
- end
59
60
 
60
- Pry::Commands.add_command(Pry::Command::SaveFile)
61
+ Pry::Commands.add_command(Pry::Command::SaveFile)
62
+ end
61
63
  end
@@ -1,48 +1,75 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::ShellCommand < Pry::ClassCommand
3
- match(/\.(.*)/)
4
- group 'Input and Output'
5
- description "All text following a '.' is forwarded to the shell."
6
- command_options :listing => '.<shell command>', :use_prefix => false,
7
- :takes_block => true
8
-
9
- banner <<-'BANNER'
10
- Usage: .COMMAND_NAME
11
-
12
- All text following a "." is forwarded to the shell.
13
-
14
- .ls -aF
15
- .uname
16
- BANNER
17
-
18
- def process(cmd)
19
- if cmd =~ /^cd\s*(.*)/i
20
- process_cd parse_destination($1)
21
- else
22
- pass_block(cmd)
23
- if command_block
24
- command_block.call `#{cmd}`
4
+ class Command
5
+ class ShellCommand < Pry::ClassCommand
6
+ match(/\.(.*)/)
7
+ group 'Input and Output'
8
+ description "All text following a '.' is forwarded to the shell."
9
+ command_options listing: '.<shell command>', use_prefix: false,
10
+ takes_block: true
11
+
12
+ banner <<-'BANNER'
13
+ Usage: .COMMAND_NAME
14
+
15
+ All text following a "." is forwarded to the shell.
16
+
17
+ .ls -aF
18
+ .uname
19
+ BANNER
20
+
21
+ def process(cmd)
22
+ if cmd =~ /^cd\s*(.*)/i
23
+ process_cd parse_destination(Regexp.last_match(1))
25
24
  else
26
- _pry_.config.system.call(output, cmd, _pry_)
25
+ pass_block(cmd)
26
+ if command_block
27
+ command_block.call `#{cmd}`
28
+ else
29
+ pry_instance.config.system.call(output, cmd, pry_instance)
30
+ end
27
31
  end
28
32
  end
29
- end
30
33
 
31
- private
34
+ private
32
35
 
33
36
  def parse_destination(dest)
34
37
  return "~" if dest.empty?
35
38
  return dest unless dest == "-"
39
+
36
40
  state.old_pwd || raise(CommandError, "No prior directory available")
37
41
  end
38
42
 
39
43
  def process_cd(dest)
40
44
  state.old_pwd = Dir.pwd
41
- Dir.chdir File.expand_path(dest)
45
+ Dir.chdir(File.expand_path(path_from_cd_path(dest) || dest))
42
46
  rescue Errno::ENOENT
43
47
  raise CommandError, "No such directory: #{dest}"
44
48
  end
45
- end
46
49
 
47
- Pry::Commands.add_command(Pry::Command::ShellCommand)
50
+ def cd_path_env
51
+ Pry::Env['CDPATH']
52
+ end
53
+
54
+ def cd_path_exists?
55
+ cd_path_env && cd_path_env.length.nonzero?
56
+ end
57
+
58
+ def path_from_cd_path(dest)
59
+ return if !(dest && cd_path_exists?) || special_case_path?(dest)
60
+
61
+ cd_path_env.split(File::PATH_SEPARATOR).each do |path|
62
+ return path if File.directory?(path) && path.split(File::SEPARATOR).last == dest
63
+ end
64
+
65
+ nil
66
+ end
67
+
68
+ def special_case_path?(dest)
69
+ ['.', '..', '-'].include?(dest) || dest =~ /\A[#{File::PATH_SEPARATOR}~]/
70
+ end
71
+ end
72
+
73
+ Pry::Commands.add_command(Pry::Command::ShellCommand)
74
+ end
48
75
  end
@@ -1,25 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::ShellMode < Pry::ClassCommand
3
- match 'shell-mode'
4
- group 'Input and Output'
5
- description 'Toggle shell mode. Bring in pwd prompt and file completion.'
4
+ class Command
5
+ class ShellMode < Pry::ClassCommand
6
+ match 'shell-mode'
7
+ group 'Input and Output'
8
+ description 'Toggle shell mode. Bring in pwd prompt and file completion.'
9
+
10
+ banner <<-'BANNER'
11
+ Toggle shell mode. Bring in pwd prompt and file completion.
12
+ BANNER
6
13
 
7
- banner <<-'BANNER'
8
- Toggle shell mode. Bring in pwd prompt and file completion.
9
- BANNER
14
+ def process
15
+ state.disabled ^= true
10
16
 
11
- def process
12
- case _pry_.prompt
13
- when Pry::SHELL_PROMPT
14
- _pry_.pop_prompt
15
- _pry_.custom_completions = _pry_.config.file_completions
16
- else
17
- _pry_.push_prompt Pry::SHELL_PROMPT
18
- _pry_.custom_completions = _pry_.config.command_completions
17
+ if state.disabled
18
+ state.prev_prompt = pry_instance.prompt
19
+ pry_instance.prompt = Pry::Prompt[:shell]
20
+ else
21
+ pry_instance.prompt = state.prev_prompt
22
+ end
19
23
  end
20
24
  end
21
- end
22
25
 
23
- Pry::Commands.add_command(Pry::Command::ShellMode)
24
- Pry::Commands.alias_command 'file-mode', 'shell-mode'
26
+ Pry::Commands.add_command(Pry::Command::ShellMode)
27
+ Pry::Commands.alias_command 'file-mode', 'shell-mode'
28
+ end
25
29
  end