super-smart-mod 0.0.1

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 (143) hide show
  1. checksums.yaml +7 -0
  2. data/pry-0.16.0/CHANGELOG.md +1211 -0
  3. data/pry-0.16.0/LICENSE +25 -0
  4. data/pry-0.16.0/README.md +469 -0
  5. data/pry-0.16.0/bin/pry +13 -0
  6. data/pry-0.16.0/lib/pry/basic_object.rb +10 -0
  7. data/pry-0.16.0/lib/pry/block_command.rb +22 -0
  8. data/pry-0.16.0/lib/pry/class_command.rb +194 -0
  9. data/pry-0.16.0/lib/pry/cli.rb +211 -0
  10. data/pry-0.16.0/lib/pry/code/code_file.rb +114 -0
  11. data/pry-0.16.0/lib/pry/code/code_range.rb +73 -0
  12. data/pry-0.16.0/lib/pry/code/loc.rb +105 -0
  13. data/pry-0.16.0/lib/pry/code.rb +357 -0
  14. data/pry-0.16.0/lib/pry/code_object.rb +197 -0
  15. data/pry-0.16.0/lib/pry/color_printer.rb +66 -0
  16. data/pry-0.16.0/lib/pry/command.rb +520 -0
  17. data/pry-0.16.0/lib/pry/command_set.rb +418 -0
  18. data/pry-0.16.0/lib/pry/command_state.rb +36 -0
  19. data/pry-0.16.0/lib/pry/commands/amend_line.rb +103 -0
  20. data/pry-0.16.0/lib/pry/commands/bang.rb +24 -0
  21. data/pry-0.16.0/lib/pry/commands/bang_pry.rb +21 -0
  22. data/pry-0.16.0/lib/pry/commands/cat/abstract_formatter.rb +32 -0
  23. data/pry-0.16.0/lib/pry/commands/cat/exception_formatter.rb +90 -0
  24. data/pry-0.16.0/lib/pry/commands/cat/file_formatter.rb +77 -0
  25. data/pry-0.16.0/lib/pry/commands/cat/input_expression_formatter.rb +48 -0
  26. data/pry-0.16.0/lib/pry/commands/cat.rb +70 -0
  27. data/pry-0.16.0/lib/pry/commands/cd.rb +48 -0
  28. data/pry-0.16.0/lib/pry/commands/change_inspector.rb +34 -0
  29. data/pry-0.16.0/lib/pry/commands/change_prompt.rb +51 -0
  30. data/pry-0.16.0/lib/pry/commands/clear_screen.rb +20 -0
  31. data/pry-0.16.0/lib/pry/commands/code_collector.rb +182 -0
  32. data/pry-0.16.0/lib/pry/commands/disable_pry.rb +31 -0
  33. data/pry-0.16.0/lib/pry/commands/easter_eggs.rb +101 -0
  34. data/pry-0.16.0/lib/pry/commands/edit/exception_patcher.rb +29 -0
  35. data/pry-0.16.0/lib/pry/commands/edit/file_and_line_locator.rb +47 -0
  36. data/pry-0.16.0/lib/pry/commands/edit.rb +225 -0
  37. data/pry-0.16.0/lib/pry/commands/exit.rb +47 -0
  38. data/pry-0.16.0/lib/pry/commands/exit_all.rb +33 -0
  39. data/pry-0.16.0/lib/pry/commands/exit_program.rb +27 -0
  40. data/pry-0.16.0/lib/pry/commands/find_method.rb +199 -0
  41. data/pry-0.16.0/lib/pry/commands/fix_indent.rb +23 -0
  42. data/pry-0.16.0/lib/pry/commands/help.rb +171 -0
  43. data/pry-0.16.0/lib/pry/commands/hist.rb +182 -0
  44. data/pry-0.16.0/lib/pry/commands/import_set.rb +27 -0
  45. data/pry-0.16.0/lib/pry/commands/jump_to.rb +33 -0
  46. data/pry-0.16.0/lib/pry/commands/list_inspectors.rb +42 -0
  47. data/pry-0.16.0/lib/pry/commands/ls/config.rb +54 -0
  48. data/pry-0.16.0/lib/pry/commands/ls/constants.rb +75 -0
  49. data/pry-0.16.0/lib/pry/commands/ls/formatter.rb +55 -0
  50. data/pry-0.16.0/lib/pry/commands/ls/globals.rb +50 -0
  51. data/pry-0.16.0/lib/pry/commands/ls/grep.rb +23 -0
  52. data/pry-0.16.0/lib/pry/commands/ls/instance_vars.rb +40 -0
  53. data/pry-0.16.0/lib/pry/commands/ls/interrogatable.rb +24 -0
  54. data/pry-0.16.0/lib/pry/commands/ls/jruby_hacks.rb +55 -0
  55. data/pry-0.16.0/lib/pry/commands/ls/local_names.rb +37 -0
  56. data/pry-0.16.0/lib/pry/commands/ls/local_vars.rb +47 -0
  57. data/pry-0.16.0/lib/pry/commands/ls/ls_entity.rb +65 -0
  58. data/pry-0.16.0/lib/pry/commands/ls/methods.rb +55 -0
  59. data/pry-0.16.0/lib/pry/commands/ls/methods_helper.rb +50 -0
  60. data/pry-0.16.0/lib/pry/commands/ls/self_methods.rb +34 -0
  61. data/pry-0.16.0/lib/pry/commands/ls.rb +114 -0
  62. data/pry-0.16.0/lib/pry/commands/nesting.rb +29 -0
  63. data/pry-0.16.0/lib/pry/commands/play.rb +113 -0
  64. data/pry-0.16.0/lib/pry/commands/pry_backtrace.rb +30 -0
  65. data/pry-0.16.0/lib/pry/commands/pry_version.rb +21 -0
  66. data/pry-0.16.0/lib/pry/commands/raise_up.rb +38 -0
  67. data/pry-0.16.0/lib/pry/commands/reload_code.rb +74 -0
  68. data/pry-0.16.0/lib/pry/commands/reset.rb +22 -0
  69. data/pry-0.16.0/lib/pry/commands/ri.rb +75 -0
  70. data/pry-0.16.0/lib/pry/commands/save_file.rb +63 -0
  71. data/pry-0.16.0/lib/pry/commands/shell_command.rb +75 -0
  72. data/pry-0.16.0/lib/pry/commands/shell_mode.rb +30 -0
  73. data/pry-0.16.0/lib/pry/commands/show_doc.rb +84 -0
  74. data/pry-0.16.0/lib/pry/commands/show_info.rb +234 -0
  75. data/pry-0.16.0/lib/pry/commands/show_input.rb +22 -0
  76. data/pry-0.16.0/lib/pry/commands/show_source.rb +57 -0
  77. data/pry-0.16.0/lib/pry/commands/stat.rb +44 -0
  78. data/pry-0.16.0/lib/pry/commands/switch_to.rb +29 -0
  79. data/pry-0.16.0/lib/pry/commands/toggle_color.rb +28 -0
  80. data/pry-0.16.0/lib/pry/commands/watch_expression/expression.rb +43 -0
  81. data/pry-0.16.0/lib/pry/commands/watch_expression.rb +108 -0
  82. data/pry-0.16.0/lib/pry/commands/whereami.rb +205 -0
  83. data/pry-0.16.0/lib/pry/commands/wtf.rb +95 -0
  84. data/pry-0.16.0/lib/pry/config/attributable.rb +22 -0
  85. data/pry-0.16.0/lib/pry/config/lazy_value.rb +29 -0
  86. data/pry-0.16.0/lib/pry/config/memoized_value.rb +34 -0
  87. data/pry-0.16.0/lib/pry/config/value.rb +24 -0
  88. data/pry-0.16.0/lib/pry/config.rb +321 -0
  89. data/pry-0.16.0/lib/pry/control_d_handler.rb +28 -0
  90. data/pry-0.16.0/lib/pry/core_extensions.rb +144 -0
  91. data/pry-0.16.0/lib/pry/editor.rb +157 -0
  92. data/pry-0.16.0/lib/pry/env.rb +18 -0
  93. data/pry-0.16.0/lib/pry/exception_handler.rb +48 -0
  94. data/pry-0.16.0/lib/pry/exceptions.rb +73 -0
  95. data/pry-0.16.0/lib/pry/forwardable.rb +27 -0
  96. data/pry-0.16.0/lib/pry/helpers/base_helpers.rb +71 -0
  97. data/pry-0.16.0/lib/pry/helpers/command_helpers.rb +146 -0
  98. data/pry-0.16.0/lib/pry/helpers/documentation_helpers.rb +84 -0
  99. data/pry-0.16.0/lib/pry/helpers/options_helpers.rb +34 -0
  100. data/pry-0.16.0/lib/pry/helpers/platform.rb +55 -0
  101. data/pry-0.16.0/lib/pry/helpers/table.rb +121 -0
  102. data/pry-0.16.0/lib/pry/helpers/text.rb +118 -0
  103. data/pry-0.16.0/lib/pry/helpers.rb +8 -0
  104. data/pry-0.16.0/lib/pry/history.rb +153 -0
  105. data/pry-0.16.0/lib/pry/hooks.rb +180 -0
  106. data/pry-0.16.0/lib/pry/indent.rb +414 -0
  107. data/pry-0.16.0/lib/pry/input/simple_stdio.rb +13 -0
  108. data/pry-0.16.0/lib/pry/input_completer.rb +283 -0
  109. data/pry-0.16.0/lib/pry/input_lock.rb +129 -0
  110. data/pry-0.16.0/lib/pry/inspector.rb +39 -0
  111. data/pry-0.16.0/lib/pry/last_exception.rb +61 -0
  112. data/pry-0.16.0/lib/pry/method/disowned.rb +67 -0
  113. data/pry-0.16.0/lib/pry/method/patcher.rb +131 -0
  114. data/pry-0.16.0/lib/pry/method/weird_method_locator.rb +222 -0
  115. data/pry-0.16.0/lib/pry/method.rb +599 -0
  116. data/pry-0.16.0/lib/pry/object_path.rb +91 -0
  117. data/pry-0.16.0/lib/pry/output.rb +136 -0
  118. data/pry-0.16.0/lib/pry/pager.rb +249 -0
  119. data/pry-0.16.0/lib/pry/prompt.rb +214 -0
  120. data/pry-0.16.0/lib/pry/pry_class.rb +371 -0
  121. data/pry-0.16.0/lib/pry/pry_instance.rb +663 -0
  122. data/pry-0.16.0/lib/pry/repl.rb +326 -0
  123. data/pry-0.16.0/lib/pry/repl_file_loader.rb +79 -0
  124. data/pry-0.16.0/lib/pry/ring.rb +89 -0
  125. data/pry-0.16.0/lib/pry/slop/LICENSE +20 -0
  126. data/pry-0.16.0/lib/pry/slop/commands.rb +190 -0
  127. data/pry-0.16.0/lib/pry/slop/option.rb +210 -0
  128. data/pry-0.16.0/lib/pry/slop.rb +672 -0
  129. data/pry-0.16.0/lib/pry/syntax_highlighter.rb +26 -0
  130. data/pry-0.16.0/lib/pry/system_command_handler.rb +17 -0
  131. data/pry-0.16.0/lib/pry/testable/evalable.rb +24 -0
  132. data/pry-0.16.0/lib/pry/testable/mockable.rb +22 -0
  133. data/pry-0.16.0/lib/pry/testable/pry_tester.rb +88 -0
  134. data/pry-0.16.0/lib/pry/testable/utility.rb +34 -0
  135. data/pry-0.16.0/lib/pry/testable/variables.rb +52 -0
  136. data/pry-0.16.0/lib/pry/testable.rb +68 -0
  137. data/pry-0.16.0/lib/pry/version.rb +5 -0
  138. data/pry-0.16.0/lib/pry/warning.rb +20 -0
  139. data/pry-0.16.0/lib/pry/wrapped_module/candidate.rb +145 -0
  140. data/pry-0.16.0/lib/pry/wrapped_module.rb +382 -0
  141. data/pry-0.16.0/lib/pry.rb +148 -0
  142. data/super-smart-mod.gemspec +12 -0
  143. metadata +182 -0
@@ -0,0 +1,326 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ class REPL
5
+ extend Pry::Forwardable
6
+ def_delegators :@pry, :input, :output
7
+
8
+ # @return [Pry] The instance of {Pry} that the user is controlling.
9
+ attr_accessor :pry
10
+
11
+ # Instantiate a new {Pry} instance with the given options, then start a
12
+ # {REPL} instance wrapping it.
13
+ # @option options See {Pry#initialize}
14
+ def self.start(options)
15
+ new(Pry.new(options)).start
16
+ end
17
+
18
+ # Create an instance of {REPL} wrapping the given {Pry}.
19
+ # @param [Pry] pry The instance of {Pry} that this {REPL} will control.
20
+ # @param [Hash] options Options for this {REPL} instance.
21
+ # @option options [Object] :target The initial target of the session.
22
+ def initialize(pry, options = {})
23
+ @pry = pry
24
+ @indent = Pry::Indent.new(pry)
25
+
26
+ @readline_output = nil
27
+
28
+ @pry.push_binding options[:target] if options[:target]
29
+ end
30
+
31
+ # Start the read-eval-print loop.
32
+ # @return [Object?] If the session throws `:breakout`, return the value
33
+ # thrown with it.
34
+ # @raise [Exception] If the session throws `:raise_up`, raise the exception
35
+ # thrown with it.
36
+ def start
37
+ prologue
38
+ Pry::InputLock.for(:all).with_ownership { repl }
39
+ ensure
40
+ epilogue
41
+ end
42
+
43
+ private
44
+
45
+ # Set up the repl session.
46
+ # @return [void]
47
+ def prologue
48
+ pry.exec_hook :before_session, pry.output, pry.current_binding, pry
49
+
50
+ return unless pry.config.correct_indent
51
+
52
+ # Clear the line before starting Pry. This fixes issue #566.
53
+ output.print(Helpers::Platform.windows_ansi? ? "\e[0F" : "\e[0G")
54
+ end
55
+
56
+ # The actual read-eval-print loop.
57
+ #
58
+ # The {REPL} instance is responsible for reading and looping, whereas the
59
+ # {Pry} instance is responsible for evaluating user input and printing
60
+ # return values and command output.
61
+ #
62
+ # @return [Object?] If the session throws `:breakout`, return the value
63
+ # thrown with it.
64
+ # @raise [Exception] If the session throws `:raise_up`, raise the exception
65
+ # thrown with it.
66
+ def repl
67
+ loop do
68
+ case val = read
69
+ when :control_c
70
+ output.puts ""
71
+ pry.reset_eval_string
72
+ when :no_more_input
73
+ output.puts "" if output.tty?
74
+ break
75
+ else
76
+ output.puts "" if val.nil? && output.tty?
77
+ return pry.exit_value unless pry.eval(val)
78
+ end
79
+ end
80
+ end
81
+
82
+ # Clean up after the repl session.
83
+ # @return [void]
84
+ def epilogue
85
+ pry.exec_hook :after_session, pry.output, pry.current_binding, pry
86
+ end
87
+
88
+ # Read a line of input from the user.
89
+ # @return [String] The line entered by the user.
90
+ # @return [nil] On `<Ctrl-D>`.
91
+ # @return [:control_c] On `<Ctrl+C>`.
92
+ # @return [:no_more_input] On EOF.
93
+ def read
94
+ @indent.reset if pry.eval_string.empty?
95
+ current_prompt = pry.select_prompt
96
+ indentation = pry.config.auto_indent ? @indent.current_prefix : ''
97
+
98
+ val = read_line("#{current_prompt}#{indentation}")
99
+
100
+ # Return nil for EOF, :no_more_input for error, or :control_c for <Ctrl-C>
101
+ return val unless val.is_a?(String)
102
+
103
+ if pry.config.auto_indent && !input_multiline?
104
+ original_val = "#{indentation}#{val}"
105
+ indented_val = @indent.indent(val)
106
+
107
+ if output.tty? &&
108
+ pry.config.correct_indent &&
109
+ Pry::Helpers::BaseHelpers.use_ansi_codes?
110
+ output.print @indent.correct_indentation(
111
+ current_prompt,
112
+ indented_val,
113
+ calculate_overhang(current_prompt, original_val, indented_val)
114
+ )
115
+ output.flush
116
+ end
117
+ else
118
+ indented_val = val
119
+ end
120
+
121
+ indented_val
122
+ end
123
+
124
+ # Manage switching of input objects on encountering `EOFError`s.
125
+ # @return [Object] Whatever the given block returns.
126
+ # @return [:no_more_input] Indicates that no more input can be read.
127
+ def handle_read_errors
128
+ should_retry = true
129
+ exception_count = 0
130
+
131
+ begin
132
+ yield
133
+ rescue EOFError
134
+ pry.config.input = Pry.config.input
135
+ unless should_retry
136
+ output.puts "Error: Pry ran out of things to read from! " \
137
+ "Attempting to break out of REPL."
138
+ return :no_more_input
139
+ end
140
+ should_retry = false
141
+ retry
142
+
143
+ # Handle <Ctrl+C> like Bash: empty the current input buffer, but don't
144
+ # quit.
145
+ rescue Interrupt
146
+ return :control_c
147
+
148
+ # If we get a random error when trying to read a line we don't want to
149
+ # automatically retry, as the user will see a lot of error messages
150
+ # scroll past and be unable to do anything about it.
151
+ rescue RescuableException => e
152
+ puts "Error: #{e.message}"
153
+ output.puts e.backtrace
154
+ exception_count += 1
155
+ retry if exception_count < 5
156
+ puts "FATAL: Pry failed to get user input using `#{input}`."
157
+ puts "To fix this you may be able to pass input and output file " \
158
+ "descriptors to pry directly. e.g."
159
+ puts " Pry.config.input = STDIN"
160
+ puts " Pry.config.output = STDOUT"
161
+ puts " binding.pry"
162
+ return :no_more_input
163
+ end
164
+ end
165
+
166
+ # Returns the next line of input to be sent to the {Pry} instance.
167
+ # @param [String] current_prompt The prompt to use for input.
168
+ # @return [String?] The next line of input, or `nil` on <Ctrl-D>.
169
+ def read_line(current_prompt)
170
+ handle_read_errors do
171
+ if coolline_available?
172
+ input.completion_proc = proc do |cool|
173
+ completions = @pry.complete cool.completed_word
174
+ completions.compact
175
+ end
176
+ elsif input.respond_to? :completion_proc=
177
+ input.completion_proc = proc do |inp|
178
+ @pry.complete inp
179
+ end
180
+ end
181
+
182
+ if reline_available?
183
+ Reline.output_modifier_proc = lambda do |text, _|
184
+ if pry.color
185
+ SyntaxHighlighter.highlight(text)
186
+ else
187
+ text
188
+ end
189
+ end
190
+
191
+ if pry.config.auto_indent
192
+ Reline.auto_indent_proc = lambda do |lines, line_index, _byte_ptr, _newline|
193
+ if line_index == 0
194
+ 0
195
+ else
196
+ pry_indentation = Pry::Indent.new
197
+ pry_indentation.indent(lines.join("\n"))
198
+ pry_indentation.last_indent_level.length
199
+ end
200
+ end
201
+ end
202
+ end
203
+
204
+ if input_multiline?
205
+ input_readmultiline(current_prompt, false)
206
+ elsif readline_available?
207
+ set_readline_output
208
+ input_readline(current_prompt, false) # false since we'll add it manually
209
+ elsif coolline_available?
210
+ input_readline(current_prompt)
211
+ elsif input.method(:readline).arity == 1
212
+ input_readline(current_prompt)
213
+ else
214
+ input_readline
215
+ end
216
+ end
217
+ end
218
+
219
+ def input_readmultiline(*args)
220
+ Pry::InputLock.for(:all).interruptible_region do
221
+ input.readmultiline(*args) do |multiline_input|
222
+ Pry.commands.find_command(multiline_input) ||
223
+ (complete_expression?(multiline_input) && !Reline::IOGate.in_pasting?)
224
+ end
225
+ end
226
+ end
227
+
228
+ def input_readline(*args)
229
+ Pry::InputLock.for(:all).interruptible_region do
230
+ input.readline(*args)
231
+ end
232
+ end
233
+
234
+ def input_multiline?
235
+ !!pry.config.multiline && reline_available?
236
+ end
237
+
238
+ def reline_available?
239
+ defined?(Reline) && input == Reline
240
+ end
241
+
242
+ def readline_available?
243
+ defined?(Readline) && input == Readline
244
+ end
245
+
246
+ def coolline_available?
247
+ defined?(Coolline) && input.is_a?(Coolline)
248
+ end
249
+
250
+ def prism_available?
251
+ @prism_available ||= begin
252
+ # rubocop:disable Lint/SuppressedException
253
+ begin
254
+ require 'prism'
255
+ rescue LoadError
256
+ end
257
+ # rubocop:enable Lint/SuppressedException
258
+
259
+ defined?(Prism::VERSION) &&
260
+ Gem::Version.new(Prism::VERSION) >= Gem::Version.new('0.25.0')
261
+ end
262
+ end
263
+
264
+ # If `$stdout` is not a tty, it's probably a pipe.
265
+ # @example
266
+ # # `piping?` returns `false`
267
+ # % pry
268
+ # [1] pry(main)
269
+ #
270
+ # # `piping?` returns `true`
271
+ # % pry | tee log
272
+ def piping?
273
+ return false unless $stdout.respond_to?(:tty?)
274
+
275
+ !$stdout.tty? && $stdin.tty? && !Helpers::Platform.windows?
276
+ end
277
+
278
+ # @return [void]
279
+ def set_readline_output
280
+ return if @readline_output
281
+
282
+ @readline_output = (Readline.output = Pry.config.output) if piping?
283
+ end
284
+
285
+ UNEXPECTED_TOKENS = %i[unexpected_token_ignore lambda_open].freeze
286
+
287
+ def complete_expression?(multiline_input)
288
+ if prism_available?
289
+ lex = Prism.lex(multiline_input)
290
+
291
+ errors = lex.errors
292
+ return true if errors.empty?
293
+
294
+ errors.any? { |error| UNEXPECTED_TOKENS.include?(error.type) }
295
+ else
296
+ Pry::Code.complete_expression?(multiline_input)
297
+ end
298
+ end
299
+
300
+ # Calculates correct overhang for current line. Supports vi Readline
301
+ # mode and its indicators such as "(ins)" or "(cmd)".
302
+ #
303
+ # @return [Integer]
304
+ # @note This doesn't calculate overhang for Readline's emacs mode with an
305
+ # indicator because emacs is the default mode and it doesn't use
306
+ # indicators in 99% of cases.
307
+ def calculate_overhang(current_prompt, original_val, indented_val)
308
+ overhang = original_val.length - indented_val.length
309
+
310
+ if readline_available? && Readline.respond_to?(:vi_editing_mode?)
311
+ begin
312
+ # rb-readline doesn't support this method:
313
+ # https://github.com/ConnorAtherton/rb-readline/issues/152
314
+ if Readline.vi_editing_mode?
315
+ overhang = output.width - current_prompt.size - indented_val.size
316
+ end
317
+ rescue NotImplementedError
318
+ # VI editing mode is unsupported on JRuby.
319
+ # https://github.com/pry/pry/issues/1840
320
+ nil
321
+ end
322
+ end
323
+ [0, overhang].max
324
+ end
325
+ end
326
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ # A class to manage the loading of files through the REPL loop.
5
+ # This is an interesting trick as it processes your file as if it
6
+ # was user input in an interactive session. As a result, all Pry
7
+ # commands are available, and they are executed non-interactively. Furthermore
8
+ # the session becomes interactive when the repl loop processes a
9
+ # 'make-interactive' command in the file. The session also becomes
10
+ # interactive when an exception is encountered, enabling you to fix
11
+ # the error before returning to non-interactive processing with the
12
+ # 'make-non-interactive' command.
13
+
14
+ class REPLFileLoader
15
+ def initialize(file_name)
16
+ full_name = File.expand_path(file_name)
17
+ raise "No such file: #{full_name}" unless File.exist?(full_name)
18
+
19
+ define_additional_commands
20
+ @content = File.read(full_name)
21
+ end
22
+
23
+ # Switch to interactive mode, i.e take input from the user
24
+ # and use the regular print and exception handlers.
25
+ # @param [Pry] pry_instance the Pry instance to make interactive.
26
+ def interactive_mode(pry_instance)
27
+ pry_instance.config.input = Pry.config.input
28
+ pry_instance.config.print = Pry.config.print
29
+ pry_instance.config.exception_handler = Pry.config.exception_handler
30
+ Pry::REPL.new(pry_instance).start
31
+ end
32
+
33
+ # Switch to non-interactive mode. Essentially
34
+ # this means there is no result output
35
+ # and that the session becomes interactive when an exception is encountered.
36
+ # @param [Pry] pry_instance the Pry instance to make non-interactive.
37
+ def non_interactive_mode(pry_instance, content)
38
+ pry_instance.print = proc {}
39
+ pry_instance.exception_handler = proc do |o, _e, p|
40
+ p.run_command "cat --ex"
41
+ o.puts "...exception encountered, going interactive!"
42
+ interactive_mode(pry_instance)
43
+ end
44
+
45
+ content.lines.each do |line|
46
+ break unless pry_instance.eval line, generated: true
47
+ end
48
+
49
+ return if pry_instance.eval_string.empty?
50
+
51
+ pry_instance.output.puts(
52
+ "#{pry_instance.eval_string}...exception encountered, going interactive!"
53
+ )
54
+ interactive_mode(pry_instance)
55
+ end
56
+
57
+ # Define a few extra commands useful for flipping back & forth
58
+ # between interactive/non-interactive modes
59
+ def define_additional_commands
60
+ s = self
61
+
62
+ Pry::Commands.command "make-interactive", "Make the session interactive" do
63
+ s.interactive_mode(pry_instance)
64
+ end
65
+
66
+ Pry::Commands.command(
67
+ "load-file", "Load another file through the repl"
68
+ ) do |file_name|
69
+ s.non_interactive_mode(pry_instance, File.read(File.expand_path(file_name)))
70
+ end
71
+ end
72
+
73
+ # Actually load the file through the REPL by setting file content
74
+ # as the REPL input stream.
75
+ def load
76
+ non_interactive_mode(Pry.new, @content)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ # A ring is a thread-safe fixed-capacity array to which you can only add
5
+ # elements. Older entries are overwritten as you add new elements, so that the
6
+ # ring can never contain more than `max_size` elements.
7
+ #
8
+ # @example
9
+ # ring = Pry::Ring.new(3)
10
+ # ring << 1 << 2 << 3
11
+ # ring.to_a #=> [1, 2, 3]
12
+ # ring << 4
13
+ # ring.to_a #=> [2, 3, 4]
14
+ #
15
+ # ring[0] #=> 2
16
+ # ring[-1] #=> 4
17
+ # ring.clear
18
+ # ring[0] #=> nil
19
+ #
20
+ # @api public
21
+ # @since v0.12.0
22
+ class Ring
23
+ # @return [Integer] maximum buffer size
24
+ attr_reader :max_size
25
+
26
+ # @return [Integer] how many objects were added during the lifetime of the
27
+ # ring
28
+ attr_reader :count
29
+ alias size count
30
+
31
+ # @param [Integer] max_size Maximum buffer size. The buffer will start
32
+ # overwriting elements once its reaches its maximum capacity
33
+ def initialize(max_size)
34
+ @max_size = max_size
35
+ @mutex = Mutex.new
36
+ clear
37
+ end
38
+
39
+ # Push `value` to the current index.
40
+ #
41
+ # @param [Object] value
42
+ # @return [self]
43
+ def <<(value)
44
+ @mutex.synchronize do
45
+ @buffer[count % max_size] = value
46
+ @count += 1
47
+ self
48
+ end
49
+ end
50
+
51
+ # Read the value stored at `index`.
52
+ #
53
+ # @param [Integer, Range] index The element (if Integer) or elements
54
+ # (if Range) associated with `index`
55
+ # @return [Object, Array<Object>, nil] element(s) at `index`, `nil` if none
56
+ # exist
57
+ def [](index)
58
+ @mutex.synchronize do
59
+ return @buffer[index] if count <= max_size
60
+ return @buffer[(count + index) % max_size] if index.is_a?(Integer)
61
+
62
+ transpose_buffer_tail[index]
63
+ end
64
+ end
65
+
66
+ # @return [Array<Object>] the buffer as unwinded array
67
+ def to_a
68
+ return @buffer.dup if count <= max_size
69
+
70
+ transpose_buffer_tail
71
+ end
72
+
73
+ # Clear the buffer and reset count.
74
+ # @return [void]
75
+ def clear
76
+ @mutex.synchronize do
77
+ @buffer = []
78
+ @count = 0
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ def transpose_buffer_tail
85
+ tail = @buffer.slice(count % max_size, @buffer.size)
86
+ tail.concat @buffer.slice(0, count % max_size)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Lee Jarvis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Pry
4
+ class Slop
5
+ class Commands
6
+ include Enumerable
7
+
8
+ attr_reader :config, :commands, :arguments
9
+ attr_writer :banner
10
+
11
+ # Create a new instance of Slop::Commands and optionally build
12
+ # Slop instances via a block. Any configuration options used in
13
+ # this method will be the default configuration options sent to
14
+ # each Slop object created.
15
+ #
16
+ # config - An optional configuration Hash.
17
+ # block - Optional block used to define commands.
18
+ #
19
+ # Examples:
20
+ #
21
+ # commands = Slop::Commands.new do
22
+ # on :new do
23
+ # on '-o', '--outdir=', 'The output directory'
24
+ # on '-v', '--verbose', 'Enable verbose mode'
25
+ # end
26
+ #
27
+ # on :generate do
28
+ # on '--assets', 'Generate assets', :default => true
29
+ # end
30
+ #
31
+ # global do
32
+ # on '-D', '--debug', 'Enable debug mode', :default => false
33
+ # end
34
+ # end
35
+ #
36
+ # commands[:new].class #=> Slop
37
+ # commands.parse
38
+ #
39
+ def initialize(config = {}, &block)
40
+ @config = config
41
+ @commands = {}
42
+ @banner = nil
43
+ @triggered_command = nil
44
+
45
+ warn "[DEPRECATED] Slop::Commands is deprecated and will be removed in "\
46
+ "Slop version 4. Check out http://injekt.github.com/slop/#commands for "\
47
+ "a new implementation of commands."
48
+
49
+ return unless block_given?
50
+
51
+ block.arity == 1 ? yield(self) : instance_eval(&block)
52
+ end
53
+
54
+ # Optionally set the banner for this command help output.
55
+ #
56
+ # banner - The String text to set the banner.
57
+ #
58
+ # Returns the String banner if one is set.
59
+ def banner(banner = nil)
60
+ @banner = banner if banner
61
+ @banner
62
+ end
63
+
64
+ # Add a Slop instance for a specific command.
65
+ #
66
+ # command - A String or Symbol key used to identify this command.
67
+ # config - A Hash of configuration options to pass to Slop.
68
+ # block - An optional block used to pass options to Slop.
69
+ #
70
+ # Returns the newly created Slop instance mapped to command.
71
+ def on(command, config = {}, &block)
72
+ commands[command.to_s] = Slop.new(@config.merge(config), &block)
73
+ end
74
+
75
+ # Add a Slop instance used when no other commands exist.
76
+ #
77
+ # config - A Hash of configuration options to pass to Slop.
78
+ # block - An optional block used to pass options to Slop.
79
+ #
80
+ # Returns the newly created Slop instance mapped to default.
81
+ def default(config = {}, &block)
82
+ on('default', config, &block)
83
+ end
84
+
85
+ # Add a global Slop instance.
86
+ #
87
+ # config - A Hash of configuration options to pass to Slop.
88
+ # block - An optional block used to pass options to Slop.
89
+ #
90
+ # Returns the newly created Slop instance mapped to global.
91
+ def global(config = {}, &block)
92
+ on('global', config, &block)
93
+ end
94
+
95
+ # Fetch the instance of Slop tied to a command.
96
+ #
97
+ # key - The String or Symbol key used to locate this command.
98
+ #
99
+ # Returns the Slop instance if this key is found, nil otherwise.
100
+ def [](key)
101
+ commands[key.to_s]
102
+ end
103
+ alias get []
104
+
105
+ # Check for a command presence.
106
+ #
107
+ # Examples:
108
+ #
109
+ # cmds.parse %w( foo )
110
+ # cmds.present?(:foo) #=> true
111
+ # cmds.present?(:bar) #=> false
112
+ #
113
+ # Returns true if the given key is present in the parsed arguments.
114
+ def present?(key)
115
+ key.to_s == @triggered_command
116
+ end
117
+
118
+ # Enumerable interface.
119
+ def each(&block)
120
+ @commands.each(&block)
121
+ end
122
+
123
+ # Parse a list of items.
124
+ #
125
+ # items - The Array of items to parse.
126
+ #
127
+ # Returns the original Array of items.
128
+ def parse(items = ARGV)
129
+ parse! items.dup
130
+ items
131
+ end
132
+
133
+ # Parse a list of items, removing any options or option arguments found.
134
+ #
135
+ # items - The Array of items to parse.
136
+ #
137
+ # Returns the original Array of items with options removed.
138
+ def parse!(items = ARGV)
139
+ if (opts = commands[items[0].to_s])
140
+ @triggered_command = items.shift
141
+ execute_arguments! items
142
+ opts.parse! items
143
+ elsif (opts = commands['default'])
144
+ opts.parse! items
145
+ elsif config[:strict] && items[0]
146
+ raise InvalidCommandError, "Unknown command `#{items[0]}`"
147
+ end
148
+ execute_global_opts! items
149
+ items
150
+ end
151
+
152
+ # Returns a nested Hash with Slop options and values. See Slop#to_hash.
153
+ def to_hash
154
+ Hash[commands.map { |k, v| [k.to_sym, v.to_hash] }]
155
+ end
156
+
157
+ # Returns the help String.
158
+ def to_s
159
+ defaults = commands.delete('default')
160
+ globals = commands.delete('global')
161
+ helps = commands.reject { |_, v| v.options.none? }
162
+ helps['Global options'] = globals.to_s if globals && globals.options.any?
163
+ helps['Other options'] = defaults.to_s if defaults && defaults.options.any?
164
+ banner = @banner ? "#{@banner}\n" : ""
165
+ banner + helps.map { |key, opts| " #{key}\n#{opts}" }.join("\n\n")
166
+ end
167
+ alias help to_s
168
+
169
+ # Returns the inspection String.
170
+ def inspect
171
+ "#<Slop::Commands #{config.inspect} #{commands.values.map(&:inspect)}>"
172
+ end
173
+
174
+ private
175
+
176
+ # Returns nothing.
177
+ def execute_arguments!(items)
178
+ @arguments = items.take_while { |arg| !arg.start_with?('-') }
179
+ items.shift @arguments.size
180
+ end
181
+
182
+ # Returns nothing.
183
+ def execute_global_opts!(items)
184
+ return unless (global_opts = commands['global'])
185
+
186
+ global_opts.parse!(items)
187
+ end
188
+ end
189
+ end
190
+ end