pry 0.9.10pre1-i386-mingw32 → 0.9.11-i386-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. data/.travis.yml +3 -1
  2. data/CHANGELOG +63 -2
  3. data/CONTRIBUTORS +43 -25
  4. data/Gemfile +7 -0
  5. data/Guardfile +62 -0
  6. data/README.markdown +4 -4
  7. data/Rakefile +34 -35
  8. data/lib/pry.rb +107 -54
  9. data/lib/pry/cli.rb +34 -11
  10. data/lib/pry/code.rb +165 -182
  11. data/lib/pry/code/code_range.rb +70 -0
  12. data/lib/pry/code/loc.rb +92 -0
  13. data/lib/pry/code_object.rb +153 -0
  14. data/lib/pry/command.rb +160 -22
  15. data/lib/pry/command_set.rb +37 -26
  16. data/lib/pry/commands.rb +4 -27
  17. data/lib/pry/commands/amend_line.rb +99 -0
  18. data/lib/pry/commands/bang.rb +20 -0
  19. data/lib/pry/commands/bang_pry.rb +17 -0
  20. data/lib/pry/commands/cat.rb +53 -0
  21. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  22. data/lib/pry/commands/cat/exception_formatter.rb +78 -0
  23. data/lib/pry/commands/cat/file_formatter.rb +84 -0
  24. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  25. data/lib/pry/commands/cd.rb +30 -0
  26. data/lib/pry/commands/code_collector.rb +165 -0
  27. data/lib/pry/commands/deprecated_commands.rb +2 -0
  28. data/lib/pry/commands/disable_pry.rb +27 -0
  29. data/lib/pry/commands/easter_eggs.rb +112 -0
  30. data/lib/pry/commands/edit.rb +206 -0
  31. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  32. data/lib/pry/commands/edit/file_and_line_locator.rb +38 -0
  33. data/lib/pry/commands/edit/method_patcher.rb +122 -0
  34. data/lib/pry/commands/exit.rb +42 -0
  35. data/lib/pry/commands/exit_all.rb +29 -0
  36. data/lib/pry/commands/exit_program.rb +24 -0
  37. data/lib/pry/commands/find_method.rb +199 -0
  38. data/lib/pry/commands/fix_indent.rb +19 -0
  39. data/lib/pry/commands/gem_cd.rb +26 -0
  40. data/lib/pry/commands/gem_install.rb +29 -0
  41. data/lib/pry/commands/gem_list.rb +33 -0
  42. data/lib/pry/commands/gem_open.rb +29 -0
  43. data/lib/pry/commands/gist.rb +95 -0
  44. data/lib/pry/commands/help.rb +164 -0
  45. data/lib/pry/commands/hist.rb +161 -0
  46. data/lib/pry/commands/import_set.rb +22 -0
  47. data/lib/pry/commands/install_command.rb +51 -0
  48. data/lib/pry/commands/jump_to.rb +29 -0
  49. data/lib/pry/commands/ls.rb +339 -0
  50. data/lib/pry/commands/nesting.rb +25 -0
  51. data/lib/pry/commands/play.rb +69 -0
  52. data/lib/pry/commands/pry_backtrace.rb +26 -0
  53. data/lib/pry/commands/pry_version.rb +17 -0
  54. data/lib/pry/commands/raise_up.rb +32 -0
  55. data/lib/pry/commands/reload_code.rb +39 -0
  56. data/lib/pry/commands/reset.rb +18 -0
  57. data/lib/pry/commands/ri.rb +56 -0
  58. data/lib/pry/commands/save_file.rb +61 -0
  59. data/lib/pry/commands/shell_command.rb +43 -0
  60. data/lib/pry/commands/shell_mode.rb +27 -0
  61. data/lib/pry/commands/show_doc.rb +78 -0
  62. data/lib/pry/commands/show_info.rb +139 -0
  63. data/lib/pry/commands/show_input.rb +17 -0
  64. data/lib/pry/commands/show_source.rb +37 -0
  65. data/lib/pry/commands/simple_prompt.rb +22 -0
  66. data/lib/pry/commands/stat.rb +40 -0
  67. data/lib/pry/commands/switch_to.rb +23 -0
  68. data/lib/pry/commands/toggle_color.rb +20 -0
  69. data/lib/pry/commands/whereami.rb +114 -0
  70. data/lib/pry/commands/wtf.rb +57 -0
  71. data/lib/pry/completion.rb +120 -46
  72. data/lib/pry/config.rb +11 -0
  73. data/lib/pry/core_extensions.rb +30 -19
  74. data/lib/pry/editor.rb +129 -0
  75. data/lib/pry/helpers.rb +1 -0
  76. data/lib/pry/helpers/base_helpers.rb +89 -119
  77. data/lib/pry/helpers/command_helpers.rb +7 -122
  78. data/lib/pry/helpers/table.rb +100 -0
  79. data/lib/pry/helpers/text.rb +4 -4
  80. data/lib/pry/history_array.rb +5 -0
  81. data/lib/pry/hooks.rb +1 -3
  82. data/lib/pry/indent.rb +104 -30
  83. data/lib/pry/method.rb +66 -22
  84. data/lib/pry/module_candidate.rb +26 -15
  85. data/lib/pry/pager.rb +70 -0
  86. data/lib/pry/plugins.rb +1 -2
  87. data/lib/pry/pry_class.rb +63 -22
  88. data/lib/pry/pry_instance.rb +58 -37
  89. data/lib/pry/rubygem.rb +74 -0
  90. data/lib/pry/terminal_info.rb +43 -0
  91. data/lib/pry/test/helper.rb +185 -0
  92. data/lib/pry/version.rb +1 -1
  93. data/lib/pry/wrapped_module.rb +58 -24
  94. data/pry.gemspec +21 -37
  95. data/{test/test_cli.rb → spec/cli_spec.rb} +0 -0
  96. data/spec/code_object_spec.rb +277 -0
  97. data/{test/test_code.rb → spec/code_spec.rb} +19 -1
  98. data/{test/test_command_helpers.rb → spec/command_helpers_spec.rb} +0 -0
  99. data/{test/test_command_integration.rb → spec/command_integration_spec.rb} +38 -46
  100. data/{test/test_command_set.rb → spec/command_set_spec.rb} +18 -1
  101. data/{test/test_command.rb → spec/command_spec.rb} +250 -149
  102. data/spec/commands/amend_line_spec.rb +247 -0
  103. data/spec/commands/bang_spec.rb +19 -0
  104. data/spec/commands/cat_spec.rb +164 -0
  105. data/spec/commands/cd_spec.rb +250 -0
  106. data/spec/commands/disable_pry_spec.rb +25 -0
  107. data/spec/commands/edit_spec.rb +727 -0
  108. data/spec/commands/exit_all_spec.rb +34 -0
  109. data/spec/commands/exit_program_spec.rb +19 -0
  110. data/spec/commands/exit_spec.rb +34 -0
  111. data/{test/test_default_commands/test_find_method.rb → spec/commands/find_method_spec.rb} +27 -7
  112. data/spec/commands/gem_list_spec.rb +26 -0
  113. data/spec/commands/gist_spec.rb +75 -0
  114. data/{test/test_default_commands/test_help.rb → spec/commands/help_spec.rb} +8 -9
  115. data/spec/commands/hist_spec.rb +181 -0
  116. data/spec/commands/jump_to_spec.rb +15 -0
  117. data/spec/commands/ls_spec.rb +177 -0
  118. data/spec/commands/play_spec.rb +140 -0
  119. data/spec/commands/raise_up_spec.rb +56 -0
  120. data/spec/commands/save_file_spec.rb +177 -0
  121. data/spec/commands/show_doc_spec.rb +378 -0
  122. data/spec/commands/show_input_spec.rb +17 -0
  123. data/spec/commands/show_source_spec.rb +597 -0
  124. data/spec/commands/whereami_spec.rb +154 -0
  125. data/spec/completion_spec.rb +233 -0
  126. data/spec/control_d_handler_spec.rb +58 -0
  127. data/spec/editor_spec.rb +79 -0
  128. data/{test/test_exception_whitelist.rb → spec/exception_whitelist_spec.rb} +0 -0
  129. data/{test → spec/fixtures}/candidate_helper1.rb +0 -0
  130. data/{test → spec/fixtures}/candidate_helper2.rb +0 -0
  131. data/{test/test_default_commands → spec/fixtures}/example.erb +0 -0
  132. data/spec/fixtures/example_nesting.rb +33 -0
  133. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  134. data/{test → spec/fixtures}/testrc +0 -0
  135. data/{test → spec/fixtures}/testrcbad +0 -0
  136. data/spec/helper.rb +34 -0
  137. data/spec/helpers/bacon.rb +86 -0
  138. data/spec/helpers/mock_pry.rb +43 -0
  139. data/spec/helpers/table_spec.rb +83 -0
  140. data/{test/test_history_array.rb → spec/history_array_spec.rb} +21 -19
  141. data/{test/test_hooks.rb → spec/hooks_spec.rb} +0 -0
  142. data/{test/test_indent.rb → spec/indent_spec.rb} +24 -0
  143. data/{test/test_input_stack.rb → spec/input_stack_spec.rb} +4 -0
  144. data/{test/test_method.rb → spec/method_spec.rb} +65 -1
  145. data/{test/test_prompt.rb → spec/prompt_spec.rb} +0 -0
  146. data/{test/test_pry_defaults.rb → spec/pry_defaults_spec.rb} +14 -14
  147. data/{test/test_pry_history.rb → spec/pry_history_spec.rb} +15 -0
  148. data/spec/pry_output_spec.rb +95 -0
  149. data/{test/test_pry.rb → spec/pry_spec.rb} +74 -32
  150. data/{test/test_sticky_locals.rb → spec/sticky_locals_spec.rb} +27 -25
  151. data/{test/test_syntax_checking.rb → spec/syntax_checking_spec.rb} +17 -1
  152. data/{test/test_wrapped_module.rb → spec/wrapped_module_spec.rb} +92 -5
  153. metadata +239 -115
  154. data/examples/example_basic.rb +0 -15
  155. data/examples/example_command_override.rb +0 -32
  156. data/examples/example_commands.rb +0 -36
  157. data/examples/example_hooks.rb +0 -9
  158. data/examples/example_image_edit.rb +0 -67
  159. data/examples/example_input.rb +0 -7
  160. data/examples/example_input2.rb +0 -29
  161. data/examples/example_output.rb +0 -11
  162. data/examples/example_print.rb +0 -6
  163. data/examples/example_prompt.rb +0 -9
  164. data/examples/helper.rb +0 -6
  165. data/lib/pry/default_commands/cd.rb +0 -81
  166. data/lib/pry/default_commands/commands.rb +0 -62
  167. data/lib/pry/default_commands/context.rb +0 -98
  168. data/lib/pry/default_commands/easter_eggs.rb +0 -95
  169. data/lib/pry/default_commands/editing.rb +0 -420
  170. data/lib/pry/default_commands/find_method.rb +0 -169
  171. data/lib/pry/default_commands/gems.rb +0 -84
  172. data/lib/pry/default_commands/gist.rb +0 -187
  173. data/lib/pry/default_commands/help.rb +0 -127
  174. data/lib/pry/default_commands/hist.rb +0 -120
  175. data/lib/pry/default_commands/input_and_output.rb +0 -306
  176. data/lib/pry/default_commands/introspection.rb +0 -410
  177. data/lib/pry/default_commands/ls.rb +0 -272
  178. data/lib/pry/default_commands/misc.rb +0 -38
  179. data/lib/pry/default_commands/navigating_pry.rb +0 -110
  180. data/lib/pry/default_commands/whereami.rb +0 -92
  181. data/lib/pry/extended_commands/experimental.rb +0 -7
  182. data/test/helper.rb +0 -223
  183. data/test/test_completion.rb +0 -62
  184. data/test/test_control_d_handler.rb +0 -45
  185. data/test/test_default_commands/test_cd.rb +0 -321
  186. data/test/test_default_commands/test_context.rb +0 -288
  187. data/test/test_default_commands/test_documentation.rb +0 -315
  188. data/test/test_default_commands/test_gems.rb +0 -18
  189. data/test/test_default_commands/test_input.rb +0 -428
  190. data/test/test_default_commands/test_introspection.rb +0 -511
  191. data/test/test_default_commands/test_ls.rb +0 -151
  192. data/test/test_default_commands/test_shell.rb +0 -343
  193. data/test/test_default_commands/test_show_source.rb +0 -432
  194. data/test/test_pry_output.rb +0 -41
@@ -81,7 +81,7 @@ class Pry
81
81
  # # number-N regex command
82
82
  def block_command(match, description="No description.", options={}, &block)
83
83
  description, options = ["No description.", description] if description.is_a?(Hash)
84
- options = default_options(match).merge!(options)
84
+ options = Pry::Command.default_options(match).merge!(options)
85
85
 
86
86
  commands[match] = Pry::BlockCommand.subclass(match, description, options, helper_module, &block)
87
87
  end
@@ -113,7 +113,7 @@ class Pry
113
113
  #
114
114
  def create_command(match, description="No description.", options={}, &block)
115
115
  description, options = ["No description.", description] if description.is_a?(Hash)
116
- options = default_options(match).merge!(options)
116
+ options = Pry::Command.default_options(match).merge!(options)
117
117
 
118
118
  commands[match] = Pry::ClassCommand.subclass(match, description, options, helper_module, &block)
119
119
  commands[match].class_eval(&block)
@@ -148,7 +148,7 @@ class Pry
148
148
  cmd.hooks[:after] << block
149
149
  end
150
150
 
151
- def each &block
151
+ def each(&block)
152
152
  @commands.each(&block)
153
153
  end
154
154
 
@@ -196,16 +196,10 @@ class Pry
196
196
  # of the command to retrieve.
197
197
  # @return [Command] The command object matched.
198
198
  def find_command_by_match_or_listing(match_or_listing)
199
- if commands[match_or_listing]
200
- cmd = commands[match_or_listing]
201
- else
202
- _, cmd = commands.find { |match, command| command.options[:listing] == match_or_listing }
203
- end
204
-
205
- raise ArgumentError, "Cannot find a command: '#{match_or_listing}'!" if !cmd
206
- cmd
199
+ cmd = (commands[match_or_listing] ||
200
+ Pry::Helpers::BaseHelpers.find_command(match_or_listing, commands))
201
+ cmd or raise ArgumentError, "Cannot find a command: '#{match_or_listing}'!"
207
202
  end
208
- protected :find_command_by_match_or_listing
209
203
 
210
204
  # Aliases a command
211
205
  # @param [String, Regex] match The match of the alias (can be a regex).
@@ -219,7 +213,8 @@ class Pry
219
213
  # @example Pass explicit description (overriding default).
220
214
  # Pry.config.commands.alias_command "lM", "ls -M", :desc => "cutiepie"
221
215
  def alias_command(match, action, options={})
222
- original_options = find_command(action).options.dup
216
+ cmd = find_command(action) or fail "Command: `#{action}` not found"
217
+ original_options = cmd.options.dup
223
218
 
224
219
  options = original_options.merge!({
225
220
  :desc => "Alias for `#{action}`",
@@ -233,6 +228,12 @@ class Pry
233
228
  run action, *args
234
229
  end
235
230
 
231
+ c.class_eval do
232
+ define_method(:complete) do |input|
233
+ cmd.new(context).complete(input)
234
+ end
235
+ end
236
+
236
237
  c.group "Aliases"
237
238
 
238
239
  c
@@ -262,6 +263,17 @@ class Pry
262
263
  commands.delete(cmd.match)
263
264
  end
264
265
 
266
+ def deprecated_command(name_of_deprecated_command, deprecation_message, matcher=name_of_deprecated_command)
267
+ create_command name_of_deprecated_command do
268
+ match matcher
269
+ description ""
270
+
271
+ define_method(:process) do
272
+ output.puts "DEPRECATED: #{deprecation_message}"
273
+ end
274
+ end
275
+ end
276
+
265
277
  # Sets or gets the description for a command (replacing the old
266
278
  # description). Returns current description if no description
267
279
  # parameter provided.
@@ -348,19 +360,18 @@ class Pry
348
360
  command.new(context).call_safely(*args)
349
361
  end
350
362
 
351
- private
352
-
353
- def default_options(match)
354
- {
355
- :requires_gem => [],
356
- :keep_retval => false,
357
- :argument_required => false,
358
- :interpolate => true,
359
- :shellwords => true,
360
- :listing => (String === match ? match : match.inspect),
361
- :use_prefix => true,
362
- :takes_block => false
363
- }
363
+ # Generate completions for the user's search.
364
+ # @param [String] search The line to search for
365
+ # @param [Hash] context The context to create the command with
366
+ # @return [Array<String>]
367
+ def complete(search, context={})
368
+ if command = find_command(search)
369
+ command.new(context).complete(search)
370
+ else
371
+ commands.keys.select do |x|
372
+ String === x && x.start_with?(search)
373
+ end.map{ |command| command + " " } + Bond::DefaultMission.completions
374
+ end
364
375
  end
365
376
  end
366
377
 
@@ -1,29 +1,6 @@
1
- require "pry/default_commands/misc"
2
- require "pry/default_commands/help"
3
- require "pry/default_commands/gems"
4
- require "pry/default_commands/context"
5
- require "pry/default_commands/commands"
6
- require "pry/default_commands/input_and_output"
7
- require "pry/default_commands/introspection"
8
- require "pry/default_commands/editing"
9
- require "pry/default_commands/navigating_pry"
10
- require "pry/default_commands/easter_eggs"
1
+ # Default commands used by Pry.
2
+ Pry::Commands = Pry::CommandSet.new
11
3
 
12
- require "pry/extended_commands/experimental"
13
-
14
- class Pry
15
-
16
- # Default commands used by Pry.
17
- Commands = Pry::CommandSet.new do
18
- import DefaultCommands::Misc
19
- import DefaultCommands::Help
20
- import DefaultCommands::Gems
21
- import DefaultCommands::Context
22
- import DefaultCommands::NavigatingPry
23
- import DefaultCommands::Editing
24
- import DefaultCommands::InputAndOutput
25
- import DefaultCommands::Introspection
26
- import DefaultCommands::EasterEggs
27
- import DefaultCommands::Commands
28
- end
4
+ Dir[File.expand_path('../commands/*.rb', __FILE__)].each do |file|
5
+ require file
29
6
  end
@@ -0,0 +1,99 @@
1
+ class Pry
2
+ class Command::AmendLine < Pry::ClassCommand
3
+ match /amend-line(?: (-?\d+)(?:\.\.(-?\d+))?)?/
4
+ group 'Editing'
5
+ description 'Amend a line of input in multi-line mode.'
6
+ command_options :interpolate => false, :listing => 'amend-line'
7
+
8
+ banner <<-'BANNER'
9
+ Amend a line of input in multi-line mode. `amend-line N`, where the N represents
10
+ line to replace. Can also specify a range of lines using `amend-line N..M`
11
+ syntax. Passing "!" as replacement content deletes the line(s) instead.
12
+
13
+ amend-line 1 puts 'new' # replace line 1
14
+ amend-line 1..4 ! # delete lines 1..4
15
+ amend-line 3 >puts 'bye' # insert before line 3
16
+ amend-line puts 'appended' # no line number modifies immediately preceding line
17
+ BANNER
18
+
19
+ def process
20
+ raise CommandError, "No input to amend." if eval_string.empty?
21
+
22
+ eval_string.replace amended_input(eval_string)
23
+ run "fix-indent"
24
+ run "show-input"
25
+ end
26
+
27
+ private
28
+
29
+ # @param [String] string The string to amend.
30
+ # @return [String] A new string with the amendments applied to it.
31
+ def amended_input(string)
32
+ input_array = eval_string.each_line.to_a
33
+
34
+ if arg_string == "!"
35
+ delete_from_array(input_array, line_range)
36
+ elsif arg_string.start_with?(">")
37
+ insert_into_array(input_array, line_range)
38
+ else
39
+ replace_in_array(input_array, line_range)
40
+ end
41
+
42
+ input_array.join
43
+ end
44
+
45
+ def delete_from_array(array, range)
46
+ array.slice!(range)
47
+ end
48
+
49
+ def insert_into_array(array, range)
50
+ insert_slot = Array(range).first
51
+ array.insert(insert_slot, arg_string[1..-1] + "\n")
52
+ end
53
+
54
+ def replace_in_array(array, range)
55
+ array[range] = arg_string + "\n"
56
+ end
57
+
58
+ # @return [Fixnum] The number of lines currently in `eval_string` (the input buffer).
59
+ def line_count
60
+ eval_string.lines.count
61
+ end
62
+
63
+ # Returns the (one-indexed) start and end lines given by the user.
64
+ # The lines in this range will be affected by the `amend-line`.
65
+ # Returns `nil` if no lines were specified by the user.
66
+ # @return [Array<Fixnum>, nil]
67
+ def start_and_end_line_number
68
+ start_line_number, end_line_number = args
69
+ end_line_number ||= start_line_number.to_i
70
+
71
+ [start_line_number.to_i, end_line_number.to_i] if start_line_number
72
+ end
73
+
74
+ # Takes two numbers that are 1-indexed, and returns a range (or
75
+ # number) that is 0-indexed. 1-indexed means the first element is
76
+ # indentified by 1 rather than by 0 (as is the case for Ruby arrays).
77
+ # @param [Fixnum] start_line_number One-indexed number.
78
+ # @param [Fixnum] end_line_number One-indexed number.
79
+ # @return [Range] The zero-indexed range.
80
+ def zero_indexed_range_from_one_indexed_numbers(start_line_number, end_line_number)
81
+ # FIXME: one_index_number is a horrible name for this method
82
+ one_index_number(start_line_number)..one_index_number(end_line_number)
83
+ end
84
+
85
+ # The lines (or line) that will be modified by the `amend-line`.
86
+ # @return [Range, Fixnum] The lines or line.
87
+ def line_range
88
+ start_line_number, end_line_number = start_and_end_line_number
89
+ if start_line_number
90
+ zero_indexed_range_from_one_indexed_numbers(start_line_number,
91
+ end_line_number)
92
+ else
93
+ line_count - 1
94
+ end
95
+ end
96
+ end
97
+
98
+ Pry::Commands.add_command(Pry::Command::AmendLine)
99
+ end
@@ -0,0 +1,20 @@
1
+ class Pry
2
+ class Command::Bang < Pry::ClassCommand
3
+ match '!'
4
+ group 'Editing'
5
+ description 'Clear the input buffer.'
6
+ command_options :use_prefix => false
7
+
8
+ banner <<-'BANNER'
9
+ Clear the input buffer. Useful if the parsing process goes wrong and you get
10
+ stuck in the read loop.
11
+ BANNER
12
+
13
+ def process
14
+ output.puts 'Input buffer cleared!'
15
+ eval_string.replace('')
16
+ end
17
+ end
18
+
19
+ Pry::Commands.add_command(Pry::Command::Bang)
20
+ end
@@ -0,0 +1,17 @@
1
+ class Pry
2
+ class Command::BangPry < Pry::ClassCommand
3
+ match '!pry'
4
+ group 'Navigating Pry'
5
+ description 'Start a Pry session on current self.'
6
+
7
+ banner <<-'BANNER'
8
+ Start a Pry session on current self. Also works mid multi-line expression.
9
+ BANNER
10
+
11
+ def process
12
+ target.pry
13
+ end
14
+ end
15
+
16
+ Pry::Commands.add_command(Pry::Command::BangPry)
17
+ end
@@ -0,0 +1,53 @@
1
+ class Pry
2
+ class Command::Cat < Pry::ClassCommand
3
+ require 'pry/commands/cat/abstract_formatter.rb'
4
+ require 'pry/commands/cat/input_expression_formatter.rb'
5
+ require 'pry/commands/cat/exception_formatter.rb'
6
+ require 'pry/commands/cat/file_formatter.rb'
7
+
8
+ match 'cat'
9
+ group 'Input and Output'
10
+ description "Show code from a file, Pry's input buffer, or the last exception."
11
+
12
+ banner <<-'BANNER'
13
+ Usage: cat FILE
14
+ cat --ex [STACK_INDEX]
15
+ cat --in [INPUT_INDEX_OR_RANGE]
16
+
17
+ `cat` is capable of showing part or all of a source file, the context of the
18
+ last exception, or an expression from Pry's input history.
19
+
20
+ `cat --ex` defaults to showing the lines surrounding the location of the last
21
+ exception. Invoking it more than once travels up the exception's backtrace, and
22
+ providing a number shows the context of the given index of the backtrace.
23
+ BANNER
24
+
25
+ def options(opt)
26
+ opt.on :ex, "Show the context of the last exception", :optional_argument => true, :as => Integer
27
+ opt.on :i, :in, "Show one or more entries from Pry's expression history", :optional_argument => true, :as => Range, :default => -5..-1
28
+ opt.on :s, :start, "Starting line (defaults to the first line)", :optional_argument => true, :as => Integer
29
+ opt.on :e, :end, "Ending line (defaults to the last line)", :optional_argument => true, :as => Integer
30
+ opt.on :l, :'line-numbers', "Show line numbers"
31
+ opt.on :t, :type, "The file type for syntax highlighting (e.g., 'ruby' or 'python')", :argument => true, :as => Symbol
32
+ end
33
+
34
+ def process
35
+ output = case
36
+ when opts.present?(:ex)
37
+ ExceptionFormatter.new(_pry_.last_exception, _pry_, opts).format
38
+ when opts.present?(:in)
39
+ InputExpressionFormatter.new(_pry_.input_array, opts).format
40
+ else
41
+ FileFormatter.new(args.first, _pry_, opts).format
42
+ end
43
+
44
+ stagger_output(output)
45
+ end
46
+
47
+ def complete(search)
48
+ super + Bond::Rc.files(search.split(" ").last || '')
49
+ end
50
+ end
51
+
52
+ Pry::Commands.add_command(Pry::Command::Cat)
53
+ end
@@ -0,0 +1,27 @@
1
+ class Pry
2
+ class Command::Cat
3
+ class AbstractFormatter
4
+ include Pry::Helpers::CommandHelpers
5
+ include Pry::Helpers::BaseHelpers
6
+
7
+ private
8
+ def decorate(content)
9
+ content.code_type = code_type
10
+ content.between(*between_lines).
11
+ with_line_numbers(use_line_numbers?)
12
+ end
13
+
14
+ def code_type
15
+ opts[:type] || :ruby
16
+ end
17
+
18
+ def use_line_numbers?
19
+ opts.present?(:'line-numbers') || opts.present?(:ex)
20
+ end
21
+
22
+ def between_lines
23
+ [opts[:start] || 1, opts[:end] || -1]
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,78 @@
1
+ class Pry
2
+ class Command::Cat
3
+ class ExceptionFormatter < AbstractFormatter
4
+ attr_accessor :ex
5
+ attr_accessor :opts
6
+ attr_accessor :_pry_
7
+
8
+ def initialize(exception, _pry_, opts)
9
+ @ex = exception
10
+ @opts = opts
11
+ @_pry_ = _pry_
12
+ end
13
+
14
+ def format
15
+ check_for_errors
16
+ set_file_and_dir_locals(backtrace_file, _pry_, _pry_.current_context)
17
+ code = decorate(Pry::Code.from_file(backtrace_file).
18
+ between(*start_and_end_line_for_code_window).
19
+ with_marker(backtrace_line)).to_s
20
+ "#{header}#{code}"
21
+ end
22
+
23
+ private
24
+
25
+ def code_window_size
26
+ Pry.config.default_window_size || 5
27
+ end
28
+
29
+ def backtrace_level
30
+ return @backtrace_level if @backtrace_level
31
+
32
+ bl = if opts[:ex].nil?
33
+ ex.bt_index
34
+ else
35
+ ex.bt_index = absolute_index_number(opts[:ex], ex.backtrace.size)
36
+ end
37
+
38
+ increment_backtrace_level
39
+ @backtrace_level = bl
40
+ end
41
+
42
+ def increment_backtrace_level
43
+ ex.inc_bt_index
44
+ end
45
+
46
+ def backtrace_file
47
+ file = Array(ex.bt_source_location_for(backtrace_level)).first
48
+ (file && RbxPath.is_core_path?(file)) ? RbxPath.convert_path_to_full(file) : file
49
+ end
50
+
51
+ def backtrace_line
52
+ Array(ex.bt_source_location_for(backtrace_level)).last
53
+ end
54
+
55
+ def check_for_errors
56
+ raise CommandError, "No exception found." unless ex
57
+ raise CommandError, "The given backtrace level is out of bounds." unless backtrace_file
58
+ end
59
+
60
+ def start_and_end_line_for_code_window
61
+ start_line = backtrace_line - code_window_size
62
+ start_line = 1 if start_line < 1
63
+
64
+ [start_line, backtrace_line + code_window_size]
65
+ end
66
+
67
+ def header
68
+ unindent %{
69
+ #{Helpers::Text.bold 'Exception:'} #{ex.class}: #{ex.message}
70
+ --
71
+ #{Helpers::Text.bold('From:')} #{backtrace_file} @ line #{backtrace_line} @ #{Helpers::Text.bold("level: #{backtrace_level}")} of backtrace (of #{ex.backtrace.size - 1}).
72
+
73
+ }
74
+ end
75
+
76
+ end
77
+ end
78
+ end