pry 0.12.2 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +110 -1
  3. data/LICENSE +1 -1
  4. data/README.md +331 -269
  5. data/bin/pry +5 -0
  6. data/lib/pry.rb +133 -119
  7. data/lib/pry/basic_object.rb +8 -4
  8. data/lib/pry/block_command.rb +22 -0
  9. data/lib/pry/class_command.rb +194 -0
  10. data/lib/pry/cli.rb +40 -31
  11. data/lib/pry/code.rb +39 -27
  12. data/lib/pry/code/code_file.rb +28 -24
  13. data/lib/pry/code/code_range.rb +4 -2
  14. data/lib/pry/code/loc.rb +15 -8
  15. data/lib/pry/code_object.rb +40 -38
  16. data/lib/pry/color_printer.rb +47 -46
  17. data/lib/pry/command.rb +166 -369
  18. data/lib/pry/command_set.rb +76 -73
  19. data/lib/pry/command_state.rb +31 -0
  20. data/lib/pry/commands/amend_line.rb +86 -81
  21. data/lib/pry/commands/bang.rb +18 -14
  22. data/lib/pry/commands/bang_pry.rb +15 -11
  23. data/lib/pry/commands/cat.rb +61 -54
  24. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  25. data/lib/pry/commands/cat/exception_formatter.rb +71 -60
  26. data/lib/pry/commands/cat/file_formatter.rb +55 -49
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  28. data/lib/pry/commands/cd.rb +40 -35
  29. data/lib/pry/commands/change_inspector.rb +29 -22
  30. data/lib/pry/commands/change_prompt.rb +44 -39
  31. data/lib/pry/commands/clear_screen.rb +16 -10
  32. data/lib/pry/commands/code_collector.rb +148 -133
  33. data/lib/pry/commands/disable_pry.rb +23 -19
  34. data/lib/pry/commands/easter_eggs.rb +19 -30
  35. data/lib/pry/commands/edit.rb +184 -161
  36. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  37. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  38. data/lib/pry/commands/exit.rb +39 -35
  39. data/lib/pry/commands/exit_all.rb +24 -20
  40. data/lib/pry/commands/exit_program.rb +20 -16
  41. data/lib/pry/commands/find_method.rb +168 -160
  42. data/lib/pry/commands/fix_indent.rb +16 -12
  43. data/lib/pry/commands/help.rb +140 -133
  44. data/lib/pry/commands/hist.rb +151 -150
  45. data/lib/pry/commands/import_set.rb +20 -16
  46. data/lib/pry/commands/jump_to.rb +25 -21
  47. data/lib/pry/commands/list_inspectors.rb +35 -28
  48. data/lib/pry/commands/ls.rb +124 -102
  49. data/lib/pry/commands/ls/constants.rb +59 -42
  50. data/lib/pry/commands/ls/formatter.rb +50 -46
  51. data/lib/pry/commands/ls/globals.rb +38 -34
  52. data/lib/pry/commands/ls/grep.rb +17 -13
  53. data/lib/pry/commands/ls/instance_vars.rb +29 -27
  54. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  55. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  56. data/lib/pry/commands/ls/local_names.rb +26 -22
  57. data/lib/pry/commands/ls/local_vars.rb +38 -28
  58. data/lib/pry/commands/ls/ls_entity.rb +47 -51
  59. data/lib/pry/commands/ls/methods.rb +44 -43
  60. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  61. data/lib/pry/commands/ls/self_methods.rb +23 -22
  62. data/lib/pry/commands/nesting.rb +21 -17
  63. data/lib/pry/commands/play.rb +93 -82
  64. data/lib/pry/commands/pry_backtrace.rb +24 -17
  65. data/lib/pry/commands/pry_version.rb +15 -11
  66. data/lib/pry/commands/raise_up.rb +27 -22
  67. data/lib/pry/commands/reload_code.rb +60 -48
  68. data/lib/pry/commands/reset.rb +16 -12
  69. data/lib/pry/commands/ri.rb +55 -45
  70. data/lib/pry/commands/save_file.rb +45 -43
  71. data/lib/pry/commands/shell_command.rb +51 -51
  72. data/lib/pry/commands/shell_mode.rb +21 -17
  73. data/lib/pry/commands/show_doc.rb +81 -68
  74. data/lib/pry/commands/show_info.rb +189 -171
  75. data/lib/pry/commands/show_input.rb +16 -11
  76. data/lib/pry/commands/show_source.rb +109 -45
  77. data/lib/pry/commands/stat.rb +35 -31
  78. data/lib/pry/commands/switch_to.rb +21 -15
  79. data/lib/pry/commands/toggle_color.rb +20 -16
  80. data/lib/pry/commands/watch_expression.rb +89 -86
  81. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  82. data/lib/pry/commands/whereami.rb +156 -148
  83. data/lib/pry/commands/wtf.rb +75 -50
  84. data/lib/pry/config.rb +311 -25
  85. data/lib/pry/config/attributable.rb +22 -0
  86. data/lib/pry/config/lazy_value.rb +29 -0
  87. data/lib/pry/config/memoized_value.rb +34 -0
  88. data/lib/pry/config/value.rb +24 -0
  89. data/lib/pry/control_d_handler.rb +28 -0
  90. data/lib/pry/core_extensions.rb +9 -7
  91. data/lib/pry/editor.rb +48 -21
  92. data/lib/pry/env.rb +18 -0
  93. data/lib/pry/exception_handler.rb +43 -0
  94. data/lib/pry/exceptions.rb +13 -16
  95. data/lib/pry/forwardable.rb +5 -1
  96. data/lib/pry/helpers.rb +2 -0
  97. data/lib/pry/helpers/base_helpers.rb +68 -197
  98. data/lib/pry/helpers/command_helpers.rb +50 -61
  99. data/lib/pry/helpers/documentation_helpers.rb +20 -13
  100. data/lib/pry/helpers/options_helpers.rb +14 -7
  101. data/lib/pry/helpers/platform.rb +7 -5
  102. data/lib/pry/helpers/table.rb +33 -26
  103. data/lib/pry/helpers/text.rb +17 -14
  104. data/lib/pry/history.rb +48 -56
  105. data/lib/pry/hooks.rb +21 -12
  106. data/lib/pry/indent.rb +54 -50
  107. data/lib/pry/input_completer.rb +248 -230
  108. data/lib/pry/input_lock.rb +8 -9
  109. data/lib/pry/inspector.rb +36 -24
  110. data/lib/pry/last_exception.rb +45 -45
  111. data/lib/pry/method.rb +141 -94
  112. data/lib/pry/method/disowned.rb +16 -4
  113. data/lib/pry/method/patcher.rb +12 -3
  114. data/lib/pry/method/weird_method_locator.rb +68 -44
  115. data/lib/pry/object_path.rb +33 -25
  116. data/lib/pry/output.rb +121 -35
  117. data/lib/pry/pager.rb +41 -42
  118. data/lib/pry/plugins.rb +25 -8
  119. data/lib/pry/prompt.rb +123 -54
  120. data/lib/pry/pry_class.rb +61 -98
  121. data/lib/pry/pry_instance.rb +217 -215
  122. data/lib/pry/repl.rb +18 -22
  123. data/lib/pry/repl_file_loader.rb +27 -21
  124. data/lib/pry/ring.rb +11 -6
  125. data/lib/pry/slop.rb +574 -563
  126. data/lib/pry/slop/commands.rb +164 -169
  127. data/lib/pry/slop/option.rb +172 -168
  128. data/lib/pry/syntax_highlighter.rb +26 -0
  129. data/lib/pry/system_command_handler.rb +17 -0
  130. data/lib/pry/testable.rb +59 -61
  131. data/lib/pry/testable/evalable.rb +21 -12
  132. data/lib/pry/testable/mockable.rb +18 -10
  133. data/lib/pry/testable/pry_tester.rb +71 -56
  134. data/lib/pry/testable/utility.rb +29 -21
  135. data/lib/pry/testable/variables.rb +49 -43
  136. data/lib/pry/version.rb +3 -1
  137. data/lib/pry/warning.rb +27 -0
  138. data/lib/pry/wrapped_module.rb +51 -42
  139. data/lib/pry/wrapped_module/candidate.rb +21 -14
  140. metadata +31 -30
  141. data/lib/pry/commands.rb +0 -6
  142. data/lib/pry/commands/disabled_commands.rb +0 -2
  143. data/lib/pry/commands/gem_cd.rb +0 -26
  144. data/lib/pry/commands/gem_install.rb +0 -32
  145. data/lib/pry/commands/gem_list.rb +0 -33
  146. data/lib/pry/commands/gem_open.rb +0 -29
  147. data/lib/pry/commands/gem_readme.rb +0 -25
  148. data/lib/pry/commands/gem_search.rb +0 -40
  149. data/lib/pry/commands/gem_stats.rb +0 -83
  150. data/lib/pry/commands/gist.rb +0 -102
  151. data/lib/pry/commands/install_command.rb +0 -54
  152. data/lib/pry/config/behavior.rb +0 -255
  153. data/lib/pry/config/convenience.rb +0 -28
  154. data/lib/pry/config/default.rb +0 -159
  155. data/lib/pry/config/memoization.rb +0 -48
  156. data/lib/pry/platform.rb +0 -91
  157. data/lib/pry/rubygem.rb +0 -84
  158. data/lib/pry/terminal.rb +0 -91
@@ -1,42 +1,47 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::Cat
3
- class InputExpressionFormatter < AbstractFormatter
4
- attr_accessor :input_expressions
5
- attr_accessor :opts
6
-
7
- def initialize(input_expressions, opts)
8
- @input_expressions = input_expressions
9
- @opts = opts
10
- end
4
+ class Command
5
+ class Cat
6
+ class InputExpressionFormatter < AbstractFormatter
7
+ attr_accessor :input_expressions
8
+ attr_accessor :opts
9
+
10
+ def initialize(input_expressions, opts)
11
+ @input_expressions = input_expressions
12
+ @opts = opts
13
+ end
11
14
 
12
- def format
13
- raise CommandError, "No input expressions!" if numbered_input_items.length < 1
15
+ def format
16
+ raise CommandError, "No input expressions!" if numbered_input_items.empty?
14
17
 
15
- if numbered_input_items.length > 1
16
- content = ""
17
- numbered_input_items.each do |i, s|
18
- content << "#{Helpers::Text.bold(i.to_s)}:\n" << decorate(Pry::Code(s).with_indentation(2)).to_s
19
- end
18
+ if numbered_input_items.length > 1
19
+ content = ''
20
+ numbered_input_items.each do |i, s|
21
+ content += "#{Helpers::Text.bold(i.to_s)}:\n"
22
+ content += decorate(Pry::Code(s).with_indentation(2)).to_s
23
+ end
20
24
 
21
- content
22
- else
23
- decorate(Pry::Code(selected_input_items.first))
25
+ content
26
+ else
27
+ decorate(Pry::Code(selected_input_items.first))
28
+ end
24
29
  end
25
- end
26
30
 
27
- private
31
+ private
28
32
 
29
- def selected_input_items
30
- input_expressions[normalized_expression_range] || []
31
- end
33
+ def selected_input_items
34
+ input_expressions[normalized_expression_range] || []
35
+ end
32
36
 
33
- def numbered_input_items
34
- @numbered_input_items ||= normalized_expression_range.zip(selected_input_items).
35
- reject { |_, s| s.nil? || s == "" }
36
- end
37
+ def numbered_input_items
38
+ @numbered_input_items ||= normalized_expression_range.zip(selected_input_items)
39
+ .reject { |_, s| s.nil? || s == "" }
40
+ end
37
41
 
38
- def normalized_expression_range
39
- absolute_index_range(opts[:i], input_expressions.count)
42
+ def normalized_expression_range
43
+ absolute_index_range(opts[:i], input_expressions.count)
44
+ end
40
45
  end
41
46
  end
42
47
  end
@@ -1,41 +1,46 @@
1
- class Pry
2
- class Command::Cd < Pry::ClassCommand
3
- match 'cd'
4
- group 'Context'
5
- description 'Move into a new context (object or scope).'
6
-
7
- banner <<-'BANNER'
8
- Usage: cd [OPTIONS] [--help]
9
-
10
- Move into new context (object or scope). As in UNIX shells use `cd ..` to go
11
- back, `cd /` to return to Pry top-level and `cd -` to toggle between last two
12
- scopes. Complex syntax (e.g `cd ../@x/@y`) also supported.
13
-
14
- cd @x
15
- cd ..
16
- cd /
17
- cd -
18
-
19
- https://github.com/pry/pry/wiki/State-navigation#wiki-Changing_scope
20
- BANNER
21
-
22
- def process
23
- state.old_stack ||= []
1
+ # frozen_string_literal: true
24
2
 
25
- if arg_string.strip == "-"
26
- unless state.old_stack.empty?
27
- _pry_.binding_stack, state.old_stack = state.old_stack, _pry_.binding_stack
28
- end
29
- else
30
- stack = ObjectPath.new(arg_string, _pry_.binding_stack).resolve
31
-
32
- if stack && stack != _pry_.binding_stack
33
- state.old_stack = _pry_.binding_stack
34
- _pry_.binding_stack = stack
3
+ class Pry
4
+ class Command
5
+ class Cd < Pry::ClassCommand
6
+ match 'cd'
7
+ group 'Context'
8
+ description 'Move into a new context (object or scope).'
9
+
10
+ banner <<-'BANNER'
11
+ Usage: cd [OPTIONS] [--help]
12
+
13
+ Move into new context (object or scope). As in UNIX shells use `cd ..` to go
14
+ back, `cd /` to return to Pry top-level and `cd -` to toggle between last two
15
+ scopes. Complex syntax (e.g `cd ../@x/@y`) also supported.
16
+
17
+ cd @x
18
+ cd ..
19
+ cd /
20
+ cd -
21
+
22
+ https://github.com/pry/pry/wiki/State-navigation#wiki-Changing_scope
23
+ BANNER
24
+
25
+ def process
26
+ state.old_stack ||= []
27
+
28
+ if arg_string.strip == "-"
29
+ unless state.old_stack.empty?
30
+ pry_instance.binding_stack, state.old_stack =
31
+ state.old_stack, pry_instance.binding_stack
32
+ end
33
+ else
34
+ stack = ObjectPath.new(arg_string, pry_instance.binding_stack).resolve
35
+
36
+ if stack && stack != pry_instance.binding_stack
37
+ state.old_stack = pry_instance.binding_stack
38
+ pry_instance.binding_stack = stack
39
+ end
35
40
  end
36
41
  end
37
42
  end
38
- end
39
43
 
40
- Pry::Commands.add_command(Pry::Command::Cd)
44
+ Pry::Commands.add_command(Pry::Command::Cd)
45
+ end
41
46
  end
@@ -1,27 +1,34 @@
1
- class Pry::Command::ChangeInspector < Pry::ClassCommand
2
- match 'change-inspector'
3
- group 'Input and Output'
4
- description 'Change the current inspector proc.'
5
- command_options argument_required: true
6
- banner <<-BANNER
7
- Usage: change-inspector NAME
1
+ # frozen_string_literal: true
8
2
 
9
- Change the proc used to print return values. See list-inspectors for a list
10
- of available procs and a short description of what each one does.
11
- BANNER
3
+ class Pry
4
+ class Command
5
+ class ChangeInspector < Pry::ClassCommand
6
+ match 'change-inspector'
7
+ group 'Input and Output'
8
+ description 'Change the current inspector proc.'
9
+ command_options argument_required: true
10
+ banner <<-BANNER
11
+ Usage: change-inspector NAME
12
12
 
13
- def process(inspector)
14
- if inspector_map.key?(inspector)
15
- _pry_.print = inspector_map[inspector][:value]
16
- output.puts "Switched to the '#{inspector}' inspector!"
17
- else
18
- raise Pry::CommandError, "'#{inspector}' isn't a known inspector!"
19
- end
20
- end
13
+ Change the proc used to print return values. See list-inspectors for a list
14
+ of available procs and a short description of what each one does.
15
+ BANNER
16
+
17
+ def process(inspector)
18
+ unless inspector_map.key?(inspector)
19
+ raise Pry::CommandError, "'#{inspector}' isn't a known inspector!"
20
+ end
21
21
 
22
- private
23
- def inspector_map
24
- Pry::Inspector::MAP
22
+ pry_instance.print = inspector_map[inspector][:value]
23
+ output.puts "Switched to the '#{inspector}' inspector!"
24
+ end
25
+
26
+ private
27
+
28
+ def inspector_map
29
+ Pry::Inspector::MAP
30
+ end
31
+ Pry::Commands.add_command(self)
32
+ end
25
33
  end
26
- Pry::Commands.add_command(self)
27
34
  end
@@ -1,46 +1,51 @@
1
- class Pry::Command::ChangePrompt < Pry::ClassCommand
2
- match 'change-prompt'
3
- group 'Input and Output'
4
- description 'Change the current prompt.'
5
- command_options argument_required: true
6
- banner <<-BANNER
7
- Usage: change-prompt [OPTIONS] [NAME]
8
-
9
- Change the current prompt.
10
- BANNER
11
-
12
- def options(opt)
13
- opt.on(:l, :list, 'List the available prompts')
14
- end
1
+ # frozen_string_literal: true
15
2
 
16
- def process(prompt)
17
- if opts.present?(:l)
18
- list_prompts
19
- else
20
- change_prompt(prompt)
21
- end
22
- end
3
+ class Pry
4
+ class Command
5
+ class ChangePrompt < Pry::ClassCommand
6
+ match 'change-prompt'
7
+ group 'Input and Output'
8
+ description 'Change the current prompt.'
9
+ command_options argument_required: true
10
+ banner <<-BANNER
11
+ Usage: change-prompt [OPTIONS] [NAME]
23
12
 
24
- private
13
+ Change the current prompt.
14
+ BANNER
25
15
 
26
- def list_prompts
27
- prompts = Pry::Prompt.all.map do |name, prompt|
28
- "#{bold(name)}#{red(' (selected)') if _pry_.prompt == prompt[:value]}\n" +
29
- prompt[:description]
30
- end
31
- output.puts(prompts.join("\n"))
32
- end
16
+ def options(opt)
17
+ opt.on(:l, :list, 'List the available prompts')
18
+ end
33
19
 
34
- def change_prompt(prompt)
35
- if Pry::Prompt.all.key?(prompt)
36
- _pry_.prompt = Pry::Prompt.all[prompt][:value]
37
- else
38
- raise(Pry::CommandError, <<MSG)
39
- '#{prompt}' isn't a known prompt. Run `change-prompt --list` to see
40
- the list of known prompts.
41
- MSG
20
+ def process(prompt)
21
+ if opts.present?(:l)
22
+ list_prompts
23
+ else
24
+ change_prompt(prompt)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def list_prompts
31
+ prompts = Pry::Prompt.all.map do |name, prompt|
32
+ "#{bold(name)}#{red(' (selected)') if pry_instance.prompt == prompt}\n" +
33
+ prompt.description
34
+ end
35
+ output.puts(prompts.join("\n" * 2))
36
+ end
37
+
38
+ def change_prompt(prompt)
39
+ if Pry::Prompt[prompt]
40
+ pry_instance.prompt = Pry::Prompt[prompt]
41
+ else
42
+ raise Pry::CommandError,
43
+ "'#{prompt}' isn't a known prompt. Run `change-prompt --list` " \
44
+ "to see the list of known prompts."
45
+ end
46
+ end
47
+
48
+ Pry::Commands.add_command(self)
42
49
  end
43
50
  end
44
-
45
- Pry::Commands.add_command(self)
46
51
  end
@@ -1,14 +1,20 @@
1
- class Pry::Command::ClearScreen < Pry::ClassCommand
2
- match 'clear-screen'
3
- group 'Input and Output'
4
- description 'Clear the contents of the screen/window Pry is running in.'
1
+ # frozen_string_literal: true
5
2
 
6
- def process
7
- if Helpers::Platform.windows?
8
- _pry_.config.system.call(_pry_.output, 'cls', _pry_)
9
- else
10
- _pry_.config.system.call(_pry_.output, 'clear', _pry_)
3
+ class Pry
4
+ class Command
5
+ class ClearScreen < Pry::ClassCommand
6
+ match 'clear-screen'
7
+ group 'Input and Output'
8
+ description 'Clear the contents of the screen/window Pry is running in.'
9
+
10
+ def process
11
+ if Pry::Helpers::Platform.windows?
12
+ pry_instance.config.system.call(pry_instance.output, 'cls', pry_instance)
13
+ else
14
+ pry_instance.config.system.call(pry_instance.output, 'clear', pry_instance)
15
+ end
16
+ end
17
+ Pry::Commands.add_command(self)
11
18
  end
12
19
  end
13
- Pry::Commands.add_command(self)
14
20
  end
@@ -1,166 +1,181 @@
1
- class Pry
2
- class Command::CodeCollector
3
- include Helpers::CommandHelpers
1
+ # frozen_string_literal: true
4
2
 
5
- attr_reader :args
6
- attr_reader :opts
7
- attr_reader :_pry_
3
+ class Pry
4
+ class Command
5
+ class CodeCollector
6
+ include Helpers::CommandHelpers
8
7
 
9
- # The name of the explicitly given file (if any).
10
- attr_accessor :file
8
+ attr_reader :args
9
+ attr_reader :opts
10
+ attr_reader :pry_instance
11
11
 
12
- class << self
13
- attr_accessor :input_expression_ranges
14
- attr_accessor :output_result_ranges
15
- end
12
+ # The name of the explicitly given file (if any).
13
+ attr_accessor :file
16
14
 
17
- @input_expression_ranges = []
18
- @output_result_ranges = []
19
-
20
- def initialize(args, opts, _pry_)
21
- @args = args
22
- @opts = opts
23
- @_pry_ = _pry_
24
- end
15
+ class << self
16
+ attr_accessor :input_expression_ranges
17
+ attr_accessor :output_result_ranges
18
+ end
25
19
 
26
- # Add the `--lines`, `-o`, `-i`, `-s`, `-d` options.
27
- def self.inject_options(opt)
28
20
  @input_expression_ranges = []
29
21
  @output_result_ranges = []
30
22
 
31
- opt.on :l, :lines, "Restrict to a subset of lines. Takes a line number or range",
32
- optional_argument: true, as: Range, default: 1..-1
33
- opt.on :o, :out, "Select lines from Pry's output result history. Takes an index or range",
34
- optional_argument: true, as: Range, default: -5..-1 do |r|
35
- output_result_ranges << (r || (-5..-1))
36
- end
37
- opt.on :i, :in, "Select lines from Pry's input expression history. Takes an index or range",
38
- optional_argument: true, as: Range, default: -5..-1 do |r|
39
- input_expression_ranges << (r || (-5..-1))
23
+ def initialize(args, opts, pry_instance)
24
+ @args = args
25
+ @opts = opts
26
+ @pry_instance = pry_instance
40
27
  end
41
- opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors",
42
- as: :count
43
- opt.on :d, :doc, "Select lines from the code object's documentation"
44
- end
45
28
 
46
- # The content (i.e code/docs) for the selected object.
47
- # If the user provided a bare code object, it returns the source.
48
- # If the user provided the `-i` or `-o` switches, it returns the
49
- # selected input/output lines joined as a string. If the user used
50
- # `-d CODE_OBJECT` it returns the docs for that code object.
51
- #
52
- # @return [String]
53
- def content
54
- @content ||=
55
- begin
56
- raise CommandError, "Only one of --out, --in, --doc and CODE_OBJECT may be specified." if bad_option_combination?
57
-
58
- content = case
59
- when opts.present?(:o)
60
- pry_output_content
61
- when opts.present?(:i)
62
- pry_input_content
63
- when opts.present?(:d)
64
- code_object_doc
65
- else
66
- code_object_source_or_file
67
- end
68
-
69
- restrict_to_lines(content, line_range)
29
+ # Add the `--lines`, `-o`, `-i`, `-s`, `-d` options.
30
+ def self.inject_options(opt)
31
+ @input_expression_ranges = []
32
+ @output_result_ranges = []
33
+
34
+ opt.on :l, :lines, "Restrict to a subset of lines. Takes a line number " \
35
+ "or range",
36
+ optional_argument: true, as: Range, default: 1..-1
37
+ opt.on :o, :out, "Select lines from Pry's output result history. " \
38
+ "Takes an index or range",
39
+ optional_argument: true, as: Range, default: -5..-1 do |r|
40
+ output_result_ranges << (r || (-5..-1))
70
41
  end
71
- end
42
+ opt.on :i, :in, "Select lines from Pry's input expression history. " \
43
+ "Takes an index or range",
44
+ optional_argument: true, as: Range, default: -5..-1 do |r|
45
+ input_expression_ranges << (r || (-5..-1))
46
+ end
47
+ opt.on :s, :super, "Select the 'super' method. Can be repeated to " \
48
+ "traverse the ancestors",
49
+ as: :count
50
+ opt.on :d, :doc, "Select lines from the code object's documentation"
51
+ end
72
52
 
73
- # The code object
74
- #
75
- # @return [Pry::WrappedModule, Pry::Method, Pry::Command]
76
- def code_object
77
- Pry::CodeObject.lookup(obj_name, _pry_, super: opts[:super])
78
- end
53
+ # The content (i.e code/docs) for the selected object.
54
+ # If the user provided a bare code object, it returns the source.
55
+ # If the user provided the `-i` or `-o` switches, it returns the
56
+ # selected input/output lines joined as a string. If the user used
57
+ # `-d CODE_OBJECT` it returns the docs for that code object.
58
+ #
59
+ # @return [String]
60
+ def content
61
+ @content ||=
62
+ begin
63
+ if bad_option_combination?
64
+ raise CommandError,
65
+ "Only one of --out, --in, --doc and CODE_OBJECT may " \
66
+ "be specified."
67
+ end
68
+
69
+ content = if opts.present?(:o)
70
+ pry_output_content
71
+ elsif opts.present?(:i)
72
+ pry_input_content
73
+ elsif opts.present?(:d)
74
+ code_object_doc
75
+ else
76
+ code_object_source_or_file
77
+ end
78
+
79
+ restrict_to_lines(content, line_range)
80
+ end
81
+ end
79
82
 
80
- # Given a string and a range, return the `range` lines of that
81
- # string.
82
- #
83
- # @param [String] content
84
- # @param [Range, Fixnum] range
85
- # @return [String] The string restricted to the given range
86
- def restrict_to_lines(content, range)
87
- Array(content.lines.to_a[range]).join
88
- end
83
+ # The code object
84
+ #
85
+ # @return [Pry::WrappedModule, Pry::Method, Pry::Command]
86
+ def code_object
87
+ Pry::CodeObject.lookup(obj_name, pry_instance, super: opts[:super])
88
+ end
89
89
 
90
- # The selected `_pry_.output_ring` as a string, as specified by
91
- # the `-o` switch.
92
- #
93
- # @return [String]
94
- def pry_output_content
95
- pry_array_content_as_string(_pry_.output_ring, self.class.output_result_ranges) do |v|
96
- _pry_.config.gist.inspecter.call(v)
90
+ # Given a string and a range, return the `range` lines of that
91
+ # string.
92
+ #
93
+ # @param [String] content
94
+ # @param [Range, Fixnum] range
95
+ # @return [String] The string restricted to the given range
96
+ def restrict_to_lines(content, range)
97
+ Array(content.lines.to_a[range]).join
97
98
  end
98
- end
99
99
 
100
- # The selected `_pry_.input_ring` as a string, as specified by
101
- # the `-i` switch.
102
- #
103
- # @return [String]
104
- def pry_input_content
105
- pry_array_content_as_string(_pry_.input_ring, self.class.input_expression_ranges) { |v| v }
106
- end
100
+ # The selected `pry_instance.output_ring` as a string, as specified by
101
+ # the `-o` switch.
102
+ #
103
+ # @return [String]
104
+ def pry_output_content
105
+ pry_array_content_as_string(
106
+ pry_instance.output_ring,
107
+ self.class.output_result_ranges,
108
+ &:pretty_inspect
109
+ )
110
+ end
107
111
 
108
- # The line range passed to `--lines`, converted to a 0-indexed range.
109
- def line_range
110
- opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
111
- end
112
+ # The selected `pry_instance.input_ring` as a string, as specified by
113
+ # the `-i` switch.
114
+ #
115
+ # @return [String]
116
+ def pry_input_content
117
+ pry_array_content_as_string(
118
+ pry_instance.input_ring, self.class.input_expression_ranges
119
+ ) { |v| v }
120
+ end
112
121
 
113
- # Name of the object argument
114
- def obj_name
115
- @obj_name ||= args.empty? ? "" : args.join(" ")
116
- end
122
+ # The line range passed to `--lines`, converted to a 0-indexed range.
123
+ def line_range
124
+ opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
125
+ end
117
126
 
118
- private
127
+ # Name of the object argument
128
+ def obj_name
129
+ @obj_name ||= args.empty? ? "" : args.join(" ")
130
+ end
119
131
 
120
- def bad_option_combination?
121
- [opts.present?(:in), opts.present?(:out),
122
- !args.empty?].count(true) > 1
123
- end
132
+ private
124
133
 
125
- def pry_array_content_as_string(array, ranges, &block)
126
- all = ''
127
- ranges.each do |range|
128
- raise CommandError, "Minimum value for range is 1, not 0." if convert_to_range(range).first == 0
134
+ def bad_option_combination?
135
+ [opts.present?(:in), opts.present?(:out),
136
+ !args.empty?].count(true) > 1
137
+ end
138
+
139
+ def pry_array_content_as_string(array, ranges)
140
+ all = ''
141
+ ranges.each do |range|
142
+ if convert_to_range(range).first == 0
143
+ raise CommandError, "Minimum value for range is 1, not 0."
144
+ end
145
+
146
+ ranged_array = Array(array[range]) || []
147
+ ranged_array.compact.each { |v| all += yield(v) }
148
+ end
129
149
 
130
- ranged_array = Array(array[range]) || []
131
- ranged_array.compact.each { |v| all << block.call(v) }
150
+ all
132
151
  end
133
152
 
134
- all
135
- end
153
+ def code_object_doc
154
+ (code_object && code_object.doc) || could_not_locate(obj_name)
155
+ end
136
156
 
137
- def code_object_doc
138
- (code_object && code_object.doc) or could_not_locate(obj_name)
139
- end
157
+ def code_object_source_or_file
158
+ (code_object && code_object.source) || file_content
159
+ end
140
160
 
141
- def code_object_source_or_file
142
- (code_object && code_object.source) || file_content
143
- end
161
+ def file_content
162
+ if File.exist?(obj_name)
163
+ # Set the file accessor.
164
+ self.file = obj_name
165
+ File.read(obj_name)
166
+ else
167
+ could_not_locate(obj_name)
168
+ end
169
+ end
144
170
 
145
- def file_content
146
- if File.exist?(obj_name)
147
- # Set the file accessor.
148
- self.file = obj_name
149
- File.read(obj_name)
150
- else
151
- could_not_locate(obj_name)
171
+ def could_not_locate(name)
172
+ raise CommandError, "Cannot locate: #{name}!"
152
173
  end
153
- end
154
174
 
155
- def could_not_locate(name)
156
- raise CommandError, "Cannot locate: #{name}!"
157
- end
175
+ def convert_to_range(range)
176
+ return range if range.is_a?(Range)
158
177
 
159
- def convert_to_range(n)
160
- if !n.is_a?(Range)
161
- (n..n)
162
- else
163
- n
178
+ (range..range)
164
179
  end
165
180
  end
166
181
  end