pry 0.10.0.pre2-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +702 -0
  3. data/LICENSE +25 -0
  4. data/README.md +406 -0
  5. data/bin/pry +16 -0
  6. data/lib/pry.rb +161 -0
  7. data/lib/pry/cli.rb +220 -0
  8. data/lib/pry/code.rb +346 -0
  9. data/lib/pry/code/code_file.rb +103 -0
  10. data/lib/pry/code/code_range.rb +71 -0
  11. data/lib/pry/code/loc.rb +92 -0
  12. data/lib/pry/code_object.rb +172 -0
  13. data/lib/pry/color_printer.rb +55 -0
  14. data/lib/pry/command.rb +692 -0
  15. data/lib/pry/command_set.rb +443 -0
  16. data/lib/pry/commands.rb +6 -0
  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 +62 -0
  21. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  22. data/lib/pry/commands/cat/exception_formatter.rb +77 -0
  23. data/lib/pry/commands/cat/file_formatter.rb +67 -0
  24. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  25. data/lib/pry/commands/cd.rb +41 -0
  26. data/lib/pry/commands/change_inspector.rb +27 -0
  27. data/lib/pry/commands/change_prompt.rb +26 -0
  28. data/lib/pry/commands/code_collector.rb +165 -0
  29. data/lib/pry/commands/disable_pry.rb +27 -0
  30. data/lib/pry/commands/disabled_commands.rb +2 -0
  31. data/lib/pry/commands/easter_eggs.rb +112 -0
  32. data/lib/pry/commands/edit.rb +195 -0
  33. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  34. data/lib/pry/commands/edit/file_and_line_locator.rb +36 -0
  35. data/lib/pry/commands/exit.rb +42 -0
  36. data/lib/pry/commands/exit_all.rb +29 -0
  37. data/lib/pry/commands/exit_program.rb +23 -0
  38. data/lib/pry/commands/find_method.rb +193 -0
  39. data/lib/pry/commands/fix_indent.rb +19 -0
  40. data/lib/pry/commands/gem_cd.rb +26 -0
  41. data/lib/pry/commands/gem_install.rb +32 -0
  42. data/lib/pry/commands/gem_list.rb +33 -0
  43. data/lib/pry/commands/gem_open.rb +29 -0
  44. data/lib/pry/commands/gist.rb +101 -0
  45. data/lib/pry/commands/help.rb +164 -0
  46. data/lib/pry/commands/hist.rb +180 -0
  47. data/lib/pry/commands/import_set.rb +22 -0
  48. data/lib/pry/commands/install_command.rb +53 -0
  49. data/lib/pry/commands/jump_to.rb +29 -0
  50. data/lib/pry/commands/list_inspectors.rb +35 -0
  51. data/lib/pry/commands/list_prompts.rb +35 -0
  52. data/lib/pry/commands/ls.rb +114 -0
  53. data/lib/pry/commands/ls/constants.rb +47 -0
  54. data/lib/pry/commands/ls/formatter.rb +49 -0
  55. data/lib/pry/commands/ls/globals.rb +48 -0
  56. data/lib/pry/commands/ls/grep.rb +21 -0
  57. data/lib/pry/commands/ls/instance_vars.rb +39 -0
  58. data/lib/pry/commands/ls/interrogatable.rb +18 -0
  59. data/lib/pry/commands/ls/jruby_hacks.rb +49 -0
  60. data/lib/pry/commands/ls/local_names.rb +35 -0
  61. data/lib/pry/commands/ls/local_vars.rb +39 -0
  62. data/lib/pry/commands/ls/ls_entity.rb +70 -0
  63. data/lib/pry/commands/ls/methods.rb +57 -0
  64. data/lib/pry/commands/ls/methods_helper.rb +46 -0
  65. data/lib/pry/commands/ls/self_methods.rb +32 -0
  66. data/lib/pry/commands/nesting.rb +25 -0
  67. data/lib/pry/commands/play.rb +103 -0
  68. data/lib/pry/commands/pry_backtrace.rb +25 -0
  69. data/lib/pry/commands/pry_version.rb +17 -0
  70. data/lib/pry/commands/raise_up.rb +32 -0
  71. data/lib/pry/commands/reload_code.rb +62 -0
  72. data/lib/pry/commands/reset.rb +18 -0
  73. data/lib/pry/commands/ri.rb +60 -0
  74. data/lib/pry/commands/save_file.rb +61 -0
  75. data/lib/pry/commands/shell_command.rb +48 -0
  76. data/lib/pry/commands/shell_mode.rb +25 -0
  77. data/lib/pry/commands/show_doc.rb +83 -0
  78. data/lib/pry/commands/show_info.rb +195 -0
  79. data/lib/pry/commands/show_input.rb +17 -0
  80. data/lib/pry/commands/show_source.rb +50 -0
  81. data/lib/pry/commands/simple_prompt.rb +22 -0
  82. data/lib/pry/commands/stat.rb +40 -0
  83. data/lib/pry/commands/switch_to.rb +23 -0
  84. data/lib/pry/commands/toggle_color.rb +24 -0
  85. data/lib/pry/commands/watch_expression.rb +105 -0
  86. data/lib/pry/commands/watch_expression/expression.rb +38 -0
  87. data/lib/pry/commands/whereami.rb +190 -0
  88. data/lib/pry/commands/wtf.rb +57 -0
  89. data/lib/pry/config.rb +24 -0
  90. data/lib/pry/config/behavior.rb +139 -0
  91. data/lib/pry/config/convenience.rb +26 -0
  92. data/lib/pry/config/default.rb +165 -0
  93. data/lib/pry/core_extensions.rb +131 -0
  94. data/lib/pry/editor.rb +133 -0
  95. data/lib/pry/exceptions.rb +77 -0
  96. data/lib/pry/helpers.rb +5 -0
  97. data/lib/pry/helpers/base_helpers.rb +113 -0
  98. data/lib/pry/helpers/command_helpers.rb +156 -0
  99. data/lib/pry/helpers/documentation_helpers.rb +75 -0
  100. data/lib/pry/helpers/options_helpers.rb +27 -0
  101. data/lib/pry/helpers/table.rb +109 -0
  102. data/lib/pry/helpers/text.rb +107 -0
  103. data/lib/pry/history.rb +125 -0
  104. data/lib/pry/history_array.rb +121 -0
  105. data/lib/pry/hooks.rb +230 -0
  106. data/lib/pry/indent.rb +406 -0
  107. data/lib/pry/input_completer.rb +242 -0
  108. data/lib/pry/input_lock.rb +132 -0
  109. data/lib/pry/inspector.rb +27 -0
  110. data/lib/pry/last_exception.rb +61 -0
  111. data/lib/pry/method.rb +546 -0
  112. data/lib/pry/method/disowned.rb +53 -0
  113. data/lib/pry/method/patcher.rb +125 -0
  114. data/lib/pry/method/weird_method_locator.rb +186 -0
  115. data/lib/pry/module_candidate.rb +136 -0
  116. data/lib/pry/object_path.rb +82 -0
  117. data/lib/pry/output.rb +50 -0
  118. data/lib/pry/pager.rb +234 -0
  119. data/lib/pry/plugins.rb +103 -0
  120. data/lib/pry/prompt.rb +26 -0
  121. data/lib/pry/pry_class.rb +375 -0
  122. data/lib/pry/pry_instance.rb +654 -0
  123. data/lib/pry/rbx_path.rb +22 -0
  124. data/lib/pry/repl.rb +202 -0
  125. data/lib/pry/repl_file_loader.rb +74 -0
  126. data/lib/pry/rubygem.rb +82 -0
  127. data/lib/pry/terminal.rb +79 -0
  128. data/lib/pry/test/helper.rb +170 -0
  129. data/lib/pry/version.rb +3 -0
  130. data/lib/pry/wrapped_module.rb +373 -0
  131. metadata +248 -0
@@ -0,0 +1,20 @@
1
+ class Pry
2
+ class Command::Bang < Pry::ClassCommand
3
+ match /^\s*!\s*$/
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,62 @@
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
+ _pry_.pager.page output
45
+ end
46
+
47
+ def complete(search)
48
+ super | load_path_completions
49
+ end
50
+
51
+ def load_path_completions
52
+ $LOAD_PATH.flat_map do |path|
53
+ Dir[path + '/**/*'].map { |f|
54
+ next if File.directory?(f)
55
+ f.sub!(path + '/', '')
56
+ }
57
+ end
58
+ end
59
+ end
60
+
61
+ Pry::Commands.add_command(Pry::Command::Cat)
62
+ 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,77 @@
1
+ class Pry
2
+ class Command::Cat
3
+ class ExceptionFormatter < AbstractFormatter
4
+ attr_reader :ex
5
+ attr_reader :opts
6
+ attr_reader :_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
+ Array(ex.bt_source_location_for(backtrace_level)).first
48
+ end
49
+
50
+ def backtrace_line
51
+ Array(ex.bt_source_location_for(backtrace_level)).last
52
+ end
53
+
54
+ def check_for_errors
55
+ raise CommandError, "No exception found." unless ex
56
+ raise CommandError, "The given backtrace level is out of bounds." unless backtrace_file
57
+ end
58
+
59
+ def start_and_end_line_for_code_window
60
+ start_line = backtrace_line - code_window_size
61
+ start_line = 1 if start_line < 1
62
+
63
+ [start_line, backtrace_line + code_window_size]
64
+ end
65
+
66
+ def header
67
+ unindent %{
68
+ #{Helpers::Text.bold 'Exception:'} #{ex.class}: #{ex.message}
69
+ --
70
+ #{Helpers::Text.bold('From:')} #{backtrace_file} @ line #{backtrace_line} @ #{Helpers::Text.bold("level: #{backtrace_level}")} of backtrace (of #{ex.backtrace.size - 1}).
71
+
72
+ }
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,67 @@
1
+ class Pry
2
+ class Command::Cat
3
+ class FileFormatter < AbstractFormatter
4
+ attr_reader :file_with_embedded_line
5
+ attr_reader :opts
6
+ attr_reader :_pry_
7
+
8
+ def initialize(file_with_embedded_line, _pry_, opts)
9
+ @file_with_embedded_line = file_with_embedded_line
10
+ @opts = opts
11
+ @_pry_ = _pry_
12
+ @code_from_file = Pry::Code.from_file(file_name)
13
+ end
14
+
15
+ def format
16
+ raise CommandError, "Must provide a filename, --in, or --ex." if !file_with_embedded_line
17
+
18
+ set_file_and_dir_locals(file_name, _pry_, _pry_.current_context)
19
+ decorate(@code_from_file)
20
+ end
21
+
22
+ def file_and_line
23
+ file_name, line_num = file_with_embedded_line.split(/:(?!\/|\\)/)
24
+
25
+ [file_name, line_num ? line_num.to_i : nil]
26
+ end
27
+
28
+ private
29
+
30
+ def file_name
31
+ file_and_line.first
32
+ end
33
+
34
+ def line_number
35
+ file_and_line.last
36
+ end
37
+
38
+ def code_window_size
39
+ _pry_.config.default_window_size || 7
40
+ end
41
+
42
+ def decorate(content)
43
+ line_number ? super.around(line_number, code_window_size) : super
44
+ end
45
+
46
+ def code_type
47
+ opts[:type] || detect_code_type_from_file(file_name)
48
+ end
49
+
50
+ def detect_code_type_from_file(file_name)
51
+ code_type = @code_from_file.code_type
52
+
53
+ if code_type == :unknown
54
+ name, ext = File.basename(file_name).split('.', 2)
55
+ case name
56
+ when "Rakefile", "Gemfile"
57
+ :ruby
58
+ else
59
+ :text
60
+ end
61
+ else
62
+ code_type
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,43 @@
1
+ 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
11
+
12
+ def format
13
+ raise CommandError, "No input expressions!" if numbered_input_items.length < 1
14
+
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
20
+
21
+ content
22
+ else
23
+ decorate(Pry::Code(selected_input_items.first)).to_s
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def selected_input_items
30
+ input_expressions[normalized_expression_range] || []
31
+ end
32
+
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
+
38
+ def normalized_expression_range
39
+ absolute_index_range(opts[:i], input_expressions.length)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,41 @@
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 ||= []
24
+
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
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ Pry::Commands.add_command(Pry::Command::Cd)
41
+ end
@@ -0,0 +1,27 @@
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
8
+
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
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
21
+
22
+ private
23
+ def inspector_map
24
+ Pry::Inspector::MAP
25
+ end
26
+ Pry::Commands.add_command(self)
27
+ end
@@ -0,0 +1,26 @@
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 NAME
8
+
9
+ Change the current prompt. See list-prompts for a list of available
10
+ prompts.
11
+ BANNER
12
+
13
+ def process(prompt)
14
+ if prompt_map.key?(prompt)
15
+ _pry_.prompt = prompt_map[prompt][:value]
16
+ else
17
+ raise Pry::CommandError, "'#{prompt}' isn't a known prompt!"
18
+ end
19
+ end
20
+
21
+ private
22
+ def prompt_map
23
+ Pry::Prompt::MAP
24
+ end
25
+ Pry::Commands.add_command(self)
26
+ end
@@ -0,0 +1,165 @@
1
+ class Pry
2
+ class Command::CodeCollector
3
+ include Helpers::CommandHelpers
4
+
5
+ attr_reader :args
6
+ attr_reader :opts
7
+ attr_reader :_pry_
8
+
9
+ # The name of the explicitly given file (if any).
10
+ attr_accessor :file
11
+
12
+ class << self
13
+ attr_accessor :input_expression_ranges
14
+ attr_accessor :output_result_ranges
15
+ end
16
+
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
25
+
26
+ # Add the `--lines`, `-o`, `-i`, `-s`, `-d` options.
27
+ def self.inject_options(opt)
28
+ @input_expression_ranges = []
29
+ @output_result_ranges = []
30
+
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))
40
+ 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
+
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
+ return @content if @content
55
+ raise CommandError, "Only one of --out, --in, --doc and CODE_OBJECT may be specified." if bad_option_combination?
56
+
57
+ content = case
58
+ when opts.present?(:o)
59
+ pry_output_content
60
+ when opts.present?(:i)
61
+ pry_input_content
62
+ when opts.present?(:d)
63
+ code_object_doc
64
+ else
65
+ code_object_source_or_file
66
+ end
67
+
68
+ @content ||= restrict_to_lines(content, line_range)
69
+ end
70
+
71
+ # The code object
72
+ #
73
+ # @return [Pry::WrappedModule, Pry::Method, Pry::Command]
74
+ def code_object
75
+ Pry::CodeObject.lookup(obj_name, _pry_, :super => opts[:super])
76
+ end
77
+
78
+ # Given a string and a range, return the `range` lines of that
79
+ # string.
80
+ #
81
+ # @param [String] content
82
+ # @param [Range, Fixnum] range
83
+ # @return [String] The string restricted to the given range
84
+ def restrict_to_lines(content, range)
85
+ Array(content.lines.to_a[range]).join
86
+ end
87
+
88
+ # The selected `_pry_.output_array` as a string, as specified by
89
+ # the `-o` switch.
90
+ #
91
+ # @return [String]
92
+ def pry_output_content
93
+ pry_array_content_as_string(_pry_.output_array, self.class.output_result_ranges) do |v|
94
+ _pry_.config.gist.inspecter.call(v)
95
+ end
96
+ end
97
+
98
+ # The selected `_pry_.input_array` as a string, as specified by
99
+ # the `-i` switch.
100
+ #
101
+ # @return [String]
102
+ def pry_input_content
103
+ pry_array_content_as_string(_pry_.input_array, self.class.input_expression_ranges) { |v| v }
104
+ end
105
+
106
+ # The line range passed to `--lines`, converted to a 0-indexed range.
107
+ def line_range
108
+ opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
109
+ end
110
+
111
+ # Name of the object argument
112
+ def obj_name
113
+ @obj_name ||= args.empty? ? "" : args.join(" ")
114
+ end
115
+
116
+ private
117
+
118
+ def bad_option_combination?
119
+ [opts.present?(:in), opts.present?(:out),
120
+ !args.empty?].count(true) > 1
121
+ end
122
+
123
+ def pry_array_content_as_string(array, ranges, &block)
124
+ all = ''
125
+ ranges.each do |range|
126
+ raise CommandError, "Minimum value for range is 1, not 0." if convert_to_range(range).first == 0
127
+
128
+ ranged_array = Array(array[range]) || []
129
+ ranged_array.compact.each { |v| all << block.call(v) }
130
+ end
131
+
132
+ all
133
+ end
134
+
135
+ def code_object_doc
136
+ (code_object && code_object.doc) or could_not_locate(obj_name)
137
+ end
138
+
139
+ def code_object_source_or_file
140
+ (code_object && code_object.source) || file_content
141
+ end
142
+
143
+ def file_content
144
+ if File.exists?(obj_name)
145
+ # Set the file accessor.
146
+ self.file = obj_name
147
+ File.read(obj_name)
148
+ else
149
+ could_not_locate(obj_name)
150
+ end
151
+ end
152
+
153
+ def could_not_locate(name)
154
+ raise CommandError, "Cannot locate: #{name}!"
155
+ end
156
+
157
+ def convert_to_range(n)
158
+ if !n.is_a?(Range)
159
+ (n..n)
160
+ else
161
+ n
162
+ end
163
+ end
164
+ end
165
+ end