pry 0.9.12.2 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1141 -0
  3. data/LICENSE +2 -2
  4. data/README.md +466 -0
  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 +97 -92
  10. data/lib/pry/code/code_file.rb +114 -0
  11. data/lib/pry/code/code_range.rb +7 -4
  12. data/lib/pry/code/loc.rb +27 -14
  13. data/lib/pry/code.rb +62 -90
  14. data/lib/pry/code_object.rb +83 -39
  15. data/lib/pry/color_printer.rb +66 -0
  16. data/lib/pry/command.rb +202 -371
  17. data/lib/pry/command_set.rb +151 -133
  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 -73
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -63
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +64 -47
  27. data/lib/pry/commands/cd.rb +42 -26
  28. data/lib/pry/commands/change_inspector.rb +34 -0
  29. data/lib/pry/commands/change_prompt.rb +51 -0
  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 +33 -24
  36. data/lib/pry/commands/edit.rb +183 -167
  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 -17
  40. data/lib/pry/commands/find_method.rb +167 -167
  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 +153 -132
  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 +42 -0
  47. data/lib/pry/commands/ls/constants.rb +75 -0
  48. data/lib/pry/commands/ls/formatter.rb +55 -0
  49. data/lib/pry/commands/ls/globals.rb +50 -0
  50. data/lib/pry/commands/ls/grep.rb +23 -0
  51. data/lib/pry/commands/ls/instance_vars.rb +40 -0
  52. data/lib/pry/commands/ls/interrogatable.rb +24 -0
  53. data/lib/pry/commands/ls/jruby_hacks.rb +55 -0
  54. data/lib/pry/commands/ls/local_names.rb +37 -0
  55. data/lib/pry/commands/ls/local_vars.rb +47 -0
  56. data/lib/pry/commands/ls/ls_entity.rb +65 -0
  57. data/lib/pry/commands/ls/methods.rb +55 -0
  58. data/lib/pry/commands/ls/methods_helper.rb +50 -0
  59. data/lib/pry/commands/ls/self_methods.rb +34 -0
  60. data/lib/pry/commands/ls.rb +100 -303
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +93 -49
  63. data/lib/pry/commands/pry_backtrace.rb +22 -18
  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 +57 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -38
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +66 -34
  71. data/lib/pry/commands/shell_mode.rb +22 -20
  72. data/lib/pry/commands/show_doc.rb +80 -65
  73. data/lib/pry/commands/show_info.rb +193 -159
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +113 -33
  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 +21 -13
  79. data/lib/pry/commands/watch_expression/expression.rb +43 -0
  80. data/lib/pry/commands/watch_expression.rb +110 -0
  81. data/lib/pry/commands/whereami.rb +157 -134
  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 +290 -220
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +50 -27
  90. data/lib/pry/editor.rb +130 -102
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +73 -0
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +22 -151
  96. data/lib/pry/helpers/command_helpers.rb +55 -63
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  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 -86
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +101 -70
  104. data/lib/pry/hooks.rb +67 -137
  105. data/lib/pry/indent.rb +79 -73
  106. data/lib/pry/input_completer.rb +283 -0
  107. data/lib/pry/input_lock.rb +129 -0
  108. data/lib/pry/inspector.rb +39 -0
  109. data/lib/pry/last_exception.rb +61 -0
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +51 -42
  112. data/lib/pry/method/weird_method_locator.rb +80 -44
  113. data/lib/pry/method.rb +225 -176
  114. data/lib/pry/object_path.rb +91 -0
  115. data/lib/pry/output.rb +136 -0
  116. data/lib/pry/pager.rb +227 -68
  117. data/lib/pry/prompt.rb +214 -0
  118. data/lib/pry/pry_class.rb +216 -289
  119. data/lib/pry/pry_instance.rb +438 -500
  120. data/lib/pry/repl.rb +256 -0
  121. data/lib/pry/repl_file_loader.rb +34 -35
  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} +36 -43
  138. data/lib/pry/wrapped_module.rb +102 -103
  139. data/lib/pry.rb +135 -261
  140. metadata +94 -283
  141. data/.document +0 -2
  142. data/.gitignore +0 -16
  143. data/.travis.yml +0 -21
  144. data/.yardopts +0 -1
  145. data/CHANGELOG +0 -534
  146. data/CONTRIBUTORS +0 -55
  147. data/Gemfile +0 -9
  148. data/Guardfile +0 -62
  149. data/README.markdown +0 -400
  150. data/Rakefile +0 -140
  151. data/TODO +0 -117
  152. data/lib/pry/commands/disabled_commands.rb +0 -2
  153. data/lib/pry/commands/gem_cd.rb +0 -26
  154. data/lib/pry/commands/gem_install.rb +0 -29
  155. data/lib/pry/commands/gem_list.rb +0 -33
  156. data/lib/pry/commands/gem_open.rb +0 -29
  157. data/lib/pry/commands/gist.rb +0 -102
  158. data/lib/pry/commands/install_command.rb +0 -51
  159. data/lib/pry/commands/simple_prompt.rb +0 -22
  160. data/lib/pry/commands.rb +0 -6
  161. data/lib/pry/completion.rb +0 -304
  162. data/lib/pry/custom_completions.rb +0 -6
  163. data/lib/pry/history_array.rb +0 -116
  164. data/lib/pry/plugins.rb +0 -103
  165. data/lib/pry/rbx_method.rb +0 -13
  166. data/lib/pry/rbx_path.rb +0 -22
  167. data/lib/pry/rubygem.rb +0 -74
  168. data/lib/pry/terminal.rb +0 -78
  169. data/lib/pry/test/helper.rb +0 -185
  170. data/man/pry.1 +0 -195
  171. data/man/pry.1.html +0 -204
  172. data/man/pry.1.ronn +0 -141
  173. data/pry.gemspec +0 -30
  174. data/spec/Procfile +0 -3
  175. data/spec/cli_spec.rb +0 -78
  176. data/spec/code_object_spec.rb +0 -277
  177. data/spec/code_spec.rb +0 -219
  178. data/spec/command_helpers_spec.rb +0 -29
  179. data/spec/command_integration_spec.rb +0 -644
  180. data/spec/command_set_spec.rb +0 -627
  181. data/spec/command_spec.rb +0 -821
  182. data/spec/commands/amend_line_spec.rb +0 -247
  183. data/spec/commands/bang_spec.rb +0 -19
  184. data/spec/commands/cat_spec.rb +0 -164
  185. data/spec/commands/cd_spec.rb +0 -250
  186. data/spec/commands/disable_pry_spec.rb +0 -25
  187. data/spec/commands/edit_spec.rb +0 -727
  188. data/spec/commands/exit_all_spec.rb +0 -34
  189. data/spec/commands/exit_program_spec.rb +0 -19
  190. data/spec/commands/exit_spec.rb +0 -34
  191. data/spec/commands/find_method_spec.rb +0 -70
  192. data/spec/commands/gem_list_spec.rb +0 -26
  193. data/spec/commands/gist_spec.rb +0 -79
  194. data/spec/commands/help_spec.rb +0 -56
  195. data/spec/commands/hist_spec.rb +0 -181
  196. data/spec/commands/jump_to_spec.rb +0 -15
  197. data/spec/commands/ls_spec.rb +0 -181
  198. data/spec/commands/play_spec.rb +0 -140
  199. data/spec/commands/raise_up_spec.rb +0 -56
  200. data/spec/commands/save_file_spec.rb +0 -177
  201. data/spec/commands/show_doc_spec.rb +0 -510
  202. data/spec/commands/show_input_spec.rb +0 -17
  203. data/spec/commands/show_source_spec.rb +0 -782
  204. data/spec/commands/whereami_spec.rb +0 -203
  205. data/spec/completion_spec.rb +0 -239
  206. data/spec/control_d_handler_spec.rb +0 -58
  207. data/spec/documentation_helper_spec.rb +0 -73
  208. data/spec/editor_spec.rb +0 -79
  209. data/spec/exception_whitelist_spec.rb +0 -21
  210. data/spec/fixtures/candidate_helper1.rb +0 -11
  211. data/spec/fixtures/candidate_helper2.rb +0 -8
  212. data/spec/fixtures/example.erb +0 -5
  213. data/spec/fixtures/example_nesting.rb +0 -33
  214. data/spec/fixtures/show_source_doc_examples.rb +0 -15
  215. data/spec/fixtures/testrc +0 -2
  216. data/spec/fixtures/testrcbad +0 -2
  217. data/spec/fixtures/whereami_helper.rb +0 -6
  218. data/spec/helper.rb +0 -34
  219. data/spec/helpers/bacon.rb +0 -86
  220. data/spec/helpers/mock_pry.rb +0 -43
  221. data/spec/helpers/table_spec.rb +0 -105
  222. data/spec/history_array_spec.rb +0 -67
  223. data/spec/hooks_spec.rb +0 -522
  224. data/spec/indent_spec.rb +0 -301
  225. data/spec/input_stack_spec.rb +0 -90
  226. data/spec/method_spec.rb +0 -482
  227. data/spec/prompt_spec.rb +0 -60
  228. data/spec/pry_defaults_spec.rb +0 -419
  229. data/spec/pry_history_spec.rb +0 -99
  230. data/spec/pry_output_spec.rb +0 -95
  231. data/spec/pry_spec.rb +0 -504
  232. data/spec/run_command_spec.rb +0 -25
  233. data/spec/sticky_locals_spec.rb +0 -157
  234. data/spec/syntax_checking_spec.rb +0 -81
  235. data/spec/wrapped_module_spec.rb +0 -261
  236. data/wiki/Customizing-pry.md +0 -397
  237. data/wiki/Home.md +0 -4
@@ -0,0 +1,194 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ # A super-class of Commands with structure.
5
+ #
6
+ # This class implements the bare-minimum functionality that a command should
7
+ # have, namely a --help switch, and then delegates actual processing to its
8
+ # subclasses.
9
+ #
10
+ # Create subclasses using {Pry::CommandSet#create_command}, and override the
11
+ # `options(opt)` method to set up an instance of Pry::Slop, and the `process`
12
+ # method to actually run the command. If necessary, you can also override
13
+ # `setup` which will be called before `options`, for example to require any
14
+ # gems your command needs to run, or to set up state.
15
+ class ClassCommand < Command
16
+ class << self
17
+ # Ensure that subclasses inherit the options, description and
18
+ # match from a ClassCommand super class.
19
+ def inherited(klass)
20
+ klass.match match
21
+ klass.description description
22
+ klass.command_options options
23
+ end
24
+
25
+ def source
26
+ source_object.source
27
+ end
28
+
29
+ def doc
30
+ new.help
31
+ end
32
+
33
+ def source_location
34
+ source_object.source_location
35
+ end
36
+
37
+ def source_file
38
+ source_object.source_file
39
+ end
40
+ alias file source_file
41
+
42
+ def source_line
43
+ source_object.source_line
44
+ end
45
+ alias line source_line
46
+
47
+ private
48
+
49
+ # The object used to extract the source for the command.
50
+ #
51
+ # This should be a `Pry::Method(block)` for a command made with `create_command`
52
+ # and a `Pry::WrappedModule(self)` for a command that's a standard class.
53
+ # @return [Pry::WrappedModule, Pry::Method]
54
+ def source_object
55
+ @source_object ||= if name =~ /^[A-Z]/
56
+ Pry::WrappedModule(self)
57
+ else
58
+ Pry::Method(block)
59
+ end
60
+ end
61
+ end
62
+
63
+ attr_accessor :opts
64
+ attr_accessor :args
65
+
66
+ # Set up `opts` and `args`, and then call `process`.
67
+ #
68
+ # This method will display help if necessary.
69
+ #
70
+ # @param [Array<String>] args The arguments passed
71
+ # @return [Object] The return value of `process` or VOID_VALUE
72
+ def call(*args)
73
+ setup
74
+
75
+ self.opts = slop
76
+ self.args = opts.parse!(args)
77
+
78
+ if opts.present?(:help)
79
+ output.puts slop.help
80
+ void
81
+ else
82
+ process(*normalize_method_args(method(:process), args))
83
+ end
84
+ end
85
+
86
+ # Return the help generated by Pry::Slop for this command.
87
+ def help
88
+ slop.help
89
+ end
90
+
91
+ # Return an instance of Pry::Slop that can parse either subcommands or the
92
+ # options that this command accepts.
93
+ def slop
94
+ Pry::Slop.new do |opt|
95
+ opt.banner(unindent(self.class.banner))
96
+ subcommands(opt)
97
+ options(opt)
98
+ opt.on :h, :help, 'Show this message.'
99
+ end
100
+ end
101
+
102
+ # Generate shell completions
103
+ # @param [String] search The line typed so far
104
+ # @return [Array<String>] the words to complete
105
+ def complete(search)
106
+ slop.flat_map do |opt|
107
+ [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
108
+ end.compact + super
109
+ end
110
+
111
+ # A method called just before `options(opt)` as part of `call`.
112
+ #
113
+ # This method can be used to set up any context your command needs to run,
114
+ # for example requiring gems, or setting default values for options.
115
+ #
116
+ # @example
117
+ # def setup
118
+ # require 'gist'
119
+ # @action = :method
120
+ # end
121
+ def setup; end
122
+
123
+ # A method to setup Pry::Slop commands so it can parse the subcommands your
124
+ # command expects. If you need to set up default values, use `setup`
125
+ # instead.
126
+ #
127
+ # @example A minimal example
128
+ # def subcommands(cmd)
129
+ # cmd.command :download do |opt|
130
+ # description 'Downloads a content from a server'
131
+ #
132
+ # opt.on :verbose, 'Use verbose output'
133
+ #
134
+ # run do |options, arguments|
135
+ # ContentDownloader.download(options, arguments)
136
+ # end
137
+ # end
138
+ # end
139
+ #
140
+ # @example Define the invokation block anywhere you want
141
+ # def subcommands(cmd)
142
+ # cmd.command :download do |opt|
143
+ # description 'Downloads a content from a server'
144
+ #
145
+ # opt.on :verbose, 'Use verbose output'
146
+ # end
147
+ # end
148
+ #
149
+ # def process
150
+ # # Perform calculations...
151
+ # opts.fetch_command(:download).run do |options, arguments|
152
+ # ContentDownloader.download(options, arguments)
153
+ # end
154
+ # # More calculations...
155
+ # end
156
+ def subcommands(cmd); end
157
+
158
+ # A method to setup Pry::Slop so it can parse the options your command expects.
159
+ #
160
+ # @note Please don't do anything side-effecty in the main part of this
161
+ # method, as it may be called by Pry at any time for introspection reasons.
162
+ # If you need to set up default values, use `setup` instead.
163
+ #
164
+ # @example
165
+ # def options(opt)
166
+ # opt.banner "Gists methods or classes"
167
+ # opt.on(:c, :class, "gist a class") do
168
+ # @action = :class
169
+ # end
170
+ # end
171
+ def options(opt); end
172
+
173
+ # The actual body of your command should go here.
174
+ #
175
+ # The `opts` mehod can be called to get the options that Pry::Slop has passed,
176
+ # and `args` gives the remaining, unparsed arguments.
177
+ #
178
+ # The return value of this method is discarded unless the command was
179
+ # created with `:keep_retval => true`, in which case it is returned to the
180
+ # repl.
181
+ #
182
+ # @example
183
+ # def process
184
+ # if opts.present?(:class)
185
+ # gist_class
186
+ # else
187
+ # gist_method
188
+ # end
189
+ # end
190
+ def process
191
+ raise CommandError, "command '#{command_name}' not implemented"
192
+ end
193
+ end
194
+ end
data/lib/pry/cli.rb CHANGED
@@ -1,24 +1,26 @@
1
+ # frozen_string_literal: true
1
2
 
2
- class Pry
3
+ require 'stringio'
3
4
 
5
+ class Pry
4
6
  # Manage the processing of command line options
5
7
  class CLI
6
-
7
8
  NoOptionsError = Class.new(StandardError)
8
9
 
9
10
  class << self
10
-
11
11
  # @return [Proc] The Proc defining the valid command line options.
12
12
  attr_accessor :options
13
13
 
14
- # @return [Array] The Procs that process the parsed options.
14
+ # @return [Array] The Procs that process the parsed options. Plugins can
15
+ # utilize this facility in order to add and process their own Pry
16
+ # options.
15
17
  attr_accessor :option_processors
16
18
 
17
19
  # @return [Array<String>] The input array of strings to process
18
20
  # as CLI options.
19
21
  attr_accessor :input_args
20
22
 
21
- # Add another set of CLI options (a Slop block)
23
+ # Add another set of CLI options (a Pry::Slop block)
22
24
  def add_options(&block)
23
25
  if options
24
26
  old_options = options
@@ -33,17 +35,8 @@ class Pry
33
35
  self
34
36
  end
35
37
 
36
- # Bring in options defined in plugins
37
- def add_plugin_options
38
- Pry.plugins.values.each do |plugin|
39
- plugin.load_cli_options
40
- end
41
-
42
- self
43
- end
44
-
45
38
  # Add a block responsible for processing parsed options.
46
- def process_options(&block)
39
+ def add_option_processor(&block)
47
40
  self.option_processors ||= []
48
41
  option_processors << block
49
42
 
@@ -56,54 +49,88 @@ class Pry
56
49
  self.option_processors = nil
57
50
  end
58
51
 
59
- def parse_options(args=ARGV.dup)
52
+ def parse_options(args = ARGV)
60
53
  unless options
61
- raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options."
54
+ raise NoOptionsError,
55
+ "No command line options defined! Use Pry::CLI.add_options to " \
56
+ "add command line options."
57
+ end
58
+
59
+ @pass_argv = args.index { |cli_arg| %w[- --].include?(cli_arg) }
60
+ if @pass_argv
61
+ slop_args = args[0...@pass_argv]
62
+ self.input_args = args.replace(args[@pass_argv + 1..-1])
63
+ else
64
+ self.input_args = slop_args = args
62
65
  end
63
66
 
64
- self.input_args = args
67
+ begin
68
+ opts = Pry::Slop.parse!(
69
+ slop_args,
70
+ help: true,
71
+ multiple_switches: false,
72
+ strict: true,
73
+ &options
74
+ )
75
+ rescue Pry::Slop::InvalidOptionError
76
+ # Display help message on unknown switches and exit.
77
+ puts Pry::Slop.new(&options)
78
+ Kernel.exit
79
+ end
65
80
 
66
- opts = Slop.parse!(args, :help => true, :multiple_switches => false, &options)
81
+ Pry.initial_session_setup
82
+ Pry.final_session_setup
67
83
 
68
84
  # Option processors are optional.
69
- if option_processors
70
- option_processors.each { |processor| processor.call(opts) }
71
- end
85
+ option_processors.each { |processor| processor.call(opts) } if option_processors
72
86
 
73
- self
87
+ opts
74
88
  end
75
89
 
76
- end
90
+ def start(opts)
91
+ Kernel.exit if opts.help?
77
92
 
78
- reset
79
- end
80
- end
93
+ # invoked via cli
94
+ Pry.cli = true
81
95
 
96
+ # create the actual context
97
+ if opts[:context]
98
+ Pry.initial_session_setup
99
+ context = Pry.binding_for(eval(opts[:context])) # rubocop:disable Security/Eval
100
+ Pry.final_session_setup
101
+ else
102
+ context = Pry.toplevel_binding
103
+ end
82
104
 
83
- # String that is built to be executed on start (created by -e and -exec switches)
84
- exec_string = ""
105
+ if !@pass_argv && Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
106
+ full_name = File.expand_path(Pry::CLI.input_args.first)
107
+ Pry.load_file_through_repl(full_name)
108
+ Kernel.exit
109
+ end
85
110
 
86
- # Bring in options defined by plugins
87
- Slop.new do
88
- on "no-plugins" do
89
- Pry.config.should_load_plugins = false
90
- end
91
- end.parse(ARGV.dup)
111
+ # Start the session (running any code passed with -e, if there is any)
112
+ Pry.start(context, input: StringIO.new(Pry.config.exec_string))
113
+ end
114
+ end
92
115
 
93
- if Pry.config.should_load_plugins
94
- Pry::CLI.add_plugin_options
116
+ reset
117
+ end
95
118
  end
96
119
 
97
120
  # The default Pry command line options (before plugin options are included)
98
121
  Pry::CLI.add_options do
99
- banner %{Usage: pry [OPTIONS]
100
- Start a Pry session.
101
- See: `https://github.com/pry` for more information.
102
- Copyright (c) 2013 John Mair (banisterfiend)
103
- --
104
- }
105
- on :e, :exec=, "A line of code to execute in context before the session starts" do |input|
106
- exec_string << input + "\n"
122
+ banner(
123
+ "Usage: pry [OPTIONS]\n" \
124
+ "Start a Pry session.\n" \
125
+ "See http://pry.github.io/ for more information.\n" \
126
+ "Copyright (c) 2016 John Mair (banisterfiend)" \
127
+ )
128
+
129
+ on(
130
+ :e, :exec=, "A line of code to execute in context before the session starts"
131
+ ) do |input|
132
+ Pry.config.exec_string += "\n" unless Pry.config.exec_string.empty?
133
+ Pry.config.exec_string += input
107
134
  end
108
135
 
109
136
  on "no-pager", "Disable pager for long output" do
@@ -111,57 +138,57 @@ Copyright (c) 2013 John Mair (banisterfiend)
111
138
  end
112
139
 
113
140
  on "no-history", "Disable history loading" do
114
- Pry.config.history.should_load = false
141
+ Pry.config.history_load = false
115
142
  end
116
143
 
117
144
  on "no-color", "Disable syntax highlighting for session" do
118
- Pry.color = false
145
+ Pry.config.color = false
119
146
  end
120
147
 
121
- on :f, "Suppress loading of ~/.pryrc and ./.pryrc" do
148
+ on :f, "Suppress loading of pryrc" do
122
149
  Pry.config.should_load_rc = false
123
150
  Pry.config.should_load_local_rc = false
124
151
  end
125
152
 
126
- on :s, "select-plugin=", "Only load specified plugin (and no others)." do |plugin_name|
127
- Pry.config.should_load_plugins = false
128
- Pry.plugins[plugin_name].activate!
153
+ on :s, "select-plugin=", "Only load specified plugin (and no others)." do |_plugin_name|
154
+ warn "The --select-plugin option is deprecated and has no effect"
129
155
  end
130
156
 
131
- on :d, "disable-plugin=", "Disable a specific plugin." do |plugin_name|
132
- Pry.plugins[plugin_name].disable!
157
+ on :d, "disable-plugin=", "Disable a specific plugin." do |_plugin_name|
158
+ warn "The --disable-plugin option is deprecated and has no effect"
133
159
  end
134
160
 
135
161
  on "no-plugins", "Suppress loading of plugins." do
136
- Pry.config.should_load_plugins = false
162
+ warn "The --no-plugins option is deprecated and has no effect"
137
163
  end
138
164
 
139
- on "installed-plugins", "List installed plugins." do
140
- puts "Installed Plugins:"
141
- puts "--"
142
- Pry.locate_plugins.each do |plugin|
143
- puts "#{plugin.name}".ljust(18) + plugin.spec.summary
144
- end
145
- exit
165
+ on "plugins", "List installed plugins." do
166
+ warn "The --plugins option is deprecated and has no effect"
167
+ warn "Try using `gem list pry-`"
168
+ Kernel.exit
146
169
  end
147
170
 
148
171
  on "simple-prompt", "Enable simple prompt mode" do
149
- Pry.config.prompt = Pry::SIMPLE_PROMPT
172
+ Pry.config.prompt = Pry::Prompt[:simple]
173
+ end
174
+
175
+ on "noprompt", "No prompt mode" do
176
+ Pry.config.prompt = Pry::Prompt[:none]
150
177
  end
151
178
 
152
179
  on :r, :require=, "`require` a Ruby script at startup" do |file|
153
180
  Pry.config.requires << file
154
181
  end
155
182
 
156
- on :I=, "Add a path to the $LOAD_PATH", :as => Array, :delimiter => ":" do |load_path|
183
+ on(:I=, "Add a path to the $LOAD_PATH", as: Array, delimiter: ":") do |load_path|
157
184
  load_path.map! do |path|
158
- /\A\.\// =~ path ? path : File.expand_path(path)
185
+ %r{\A\./} =~ path ? path : File.expand_path(path)
159
186
  end
160
187
 
161
188
  $LOAD_PATH.unshift(*load_path)
162
189
  end
163
190
 
164
- on "gem", "Shorthand for -I./lib -rgemname" do |load_path|
191
+ on "gem", "Shorthand for -I./lib -rgemname" do |_load_path|
165
192
  $LOAD_PATH.unshift("./lib")
166
193
  Dir["./lib/*.rb"].each do |file|
167
194
  Pry.config.requires << file
@@ -170,33 +197,11 @@ Copyright (c) 2013 John Mair (banisterfiend)
170
197
 
171
198
  on :v, :version, "Display the Pry version" do
172
199
  puts "Pry version #{Pry::VERSION} on Ruby #{RUBY_VERSION}"
173
- exit
174
- end
175
-
176
- on(:c, :context=,
177
- "Start the session in the specified context. Equivalent to `context.pry` in a session.",
178
- :default => "Pry.toplevel_binding"
179
- )
180
- end.process_options do |opts|
181
-
182
- exit if opts.help?
183
-
184
- # invoked via cli
185
- Pry.cli = true
186
-
187
- # create the actual context
188
- context = Pry.binding_for(eval(opts[:context]))
189
-
190
- if Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
191
- full_name = File.expand_path(Pry::CLI.input_args.first)
192
- Pry.load_file_through_repl(full_name)
193
- exit
194
- end
195
-
196
- if Pry.config.should_load_plugins
197
- parser = Slop.new
200
+ Kernel.exit
198
201
  end
199
202
 
200
- # Start the session (running any code passed with -e, if there is any)
201
- Pry.start(context, :input => StringIO.new(exec_string))
203
+ on :c, :context=,
204
+ "Start the session in the specified context. Equivalent to " \
205
+ "`context.pry` in a session.",
206
+ default: "Pry.toplevel_binding"
202
207
  end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'method_source'
4
+
5
+ class Pry
6
+ class CodeFile
7
+ DEFAULT_EXT = '.rb'.freeze
8
+
9
+ # List of all supported languages.
10
+ # @return [Hash]
11
+ EXTENSIONS = {
12
+ %w[.py] => :python,
13
+ %w[.js] => :javascript,
14
+ %w[.css] => :css,
15
+ %w[.xml] => :xml,
16
+ %w[.php] => :php,
17
+ %w[.html] => :html,
18
+ %w[.diff] => :diff,
19
+ %w[.java] => :java,
20
+ %w[.json] => :json,
21
+ %w[.c .h] => :c,
22
+ %w[.rhtml] => :rhtml,
23
+ %w[.yaml .yml] => :yaml,
24
+ %w[.cpp .hpp .cc .h .cxx] => :cpp,
25
+ %w[.rb .ru .irbrc .gemspec .pryrc .rake] => :ruby
26
+ }.freeze
27
+
28
+ FILES = {
29
+ %w[Gemfile Rakefile Guardfile Capfile] => :ruby
30
+ }.freeze
31
+
32
+ # Store the current working directory. This allows show-source etc. to work if
33
+ # your process has changed directory since boot. [Issue #675]
34
+ INITIAL_PWD = Dir.pwd
35
+
36
+ # @return [Symbol] The type of code stored in this wrapper.
37
+ attr_reader :code_type
38
+
39
+ # @param [String] filename The name of a file with code to be detected
40
+ # @param [Symbol] code_type The type of code the `filename` contains
41
+ def initialize(filename, code_type = type_from_filename(filename))
42
+ @filename = filename
43
+ @code_type = code_type
44
+ end
45
+
46
+ # @return [String] The code contained in the current `@filename`.
47
+ def code
48
+ if @filename == Pry.eval_path
49
+ Pry.line_buffer.drop(1)
50
+ elsif Pry::Method::Patcher.code_for(@filename)
51
+ Pry::Method::Patcher.code_for(@filename)
52
+ else
53
+ path = abs_path
54
+ @code_type = type_from_filename(path)
55
+ File.read(path)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ # @raise [MethodSource::SourceNotFoundError] if the `filename` is not
62
+ # readable for some reason.
63
+ # @return [String] absolute path for the given `filename`.
64
+ def abs_path
65
+ code_path.detect { |path| readable?(path) } ||
66
+ raise(MethodSource::SourceNotFoundError,
67
+ "Cannot open #{@filename.inspect} for reading.")
68
+ end
69
+
70
+ # @param [String] path
71
+ # @return [Boolean] if the path, with or without the default ext,
72
+ # is a readable file then `true`, otherwise `false`.
73
+ def readable?(path)
74
+ File.readable?(path) && !File.directory?(path) ||
75
+ File.readable?(path << DEFAULT_EXT)
76
+ end
77
+
78
+ # @return [Array] All the paths that contain code that Pry can use for its
79
+ # API's. Skips directories.
80
+ def code_path
81
+ [from_pwd, from_pry_init_pwd, *from_load_path]
82
+ end
83
+
84
+ # @param [String] filename
85
+ # @param [Symbol] default (:unknown) the file type to assume if none could be
86
+ # detected.
87
+ # @return [Symbol, nil] The SyntaxHighlighter type of a file from its
88
+ # extension, or `nil` if `:unknown`.
89
+ def type_from_filename(filename, default = :unknown)
90
+ _, @code_type = EXTENSIONS.find do |k, _|
91
+ k.any? { |ext| ext == File.extname(filename) }
92
+ end || FILES.find do |k, _|
93
+ k.any? { |file_name| file_name == File.basename(filename) }
94
+ end
95
+
96
+ code_type || default
97
+ end
98
+
99
+ # @return [String]
100
+ def from_pwd
101
+ File.expand_path(@filename, Dir.pwd)
102
+ end
103
+
104
+ # @return [String]
105
+ def from_pry_init_pwd
106
+ File.expand_path(@filename, INITIAL_PWD)
107
+ end
108
+
109
+ # @return [String]
110
+ def from_load_path
111
+ $LOAD_PATH.map { |path| File.expand_path(@filename, path) }
112
+ end
113
+ end
114
+ end
@@ -1,11 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  class Code
3
-
4
5
  # Represents a range of lines in a code listing.
5
6
  #
6
7
  # @api private
7
8
  class CodeRange
8
-
9
9
  # @param [Integer] start_line
10
10
  # @param [Integer?] end_line
11
11
  def initialize(start_line, end_line = nil)
@@ -22,7 +22,9 @@ class Pry
22
22
 
23
23
  private
24
24
 
25
- attr_reader :start_line, :end_line
25
+ attr_reader :start_line
26
+
27
+ attr_reader :end_line
26
28
 
27
29
  # If `end_line` is equal to `nil`, then calculate it from the first
28
30
  # parameter, `start_line`. Otherwise, leave it as it is.
@@ -47,12 +49,14 @@ class Pry
47
49
  # @return [Integer]
48
50
  def find_start_index(lines)
49
51
  return start_line if start_line < 0
52
+
50
53
  lines.index { |loc| loc.lineno >= start_line } || lines.length
51
54
  end
52
55
 
53
56
  # @return [Integer]
54
57
  def find_end_index(lines)
55
58
  return end_line if end_line < 0
59
+
56
60
  (lines.index { |loc| loc.lineno > end_line } || 0) - 1
57
61
  end
58
62
 
@@ -65,6 +69,5 @@ class Pry
65
69
  @start_line = start_line.first
66
70
  end
67
71
  end
68
-
69
72
  end
70
73
  end