pry 0.12.0 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +162 -1
- data/LICENSE +1 -1
- data/README.md +331 -269
- data/bin/pry +5 -0
- data/lib/pry.rb +132 -119
- data/lib/pry/basic_object.rb +8 -4
- data/lib/pry/block_command.rb +22 -0
- data/lib/pry/class_command.rb +194 -0
- data/lib/pry/cli.rb +43 -51
- data/lib/pry/code.rb +40 -28
- data/lib/pry/code/code_file.rb +28 -24
- data/lib/pry/code/code_range.rb +4 -2
- data/lib/pry/code/loc.rb +15 -8
- data/lib/pry/code_object.rb +40 -38
- data/lib/pry/color_printer.rb +47 -46
- data/lib/pry/command.rb +166 -369
- data/lib/pry/command_set.rb +76 -73
- data/lib/pry/command_state.rb +31 -0
- data/lib/pry/commands/amend_line.rb +86 -81
- data/lib/pry/commands/bang.rb +18 -14
- data/lib/pry/commands/bang_pry.rb +15 -11
- data/lib/pry/commands/cat.rb +61 -54
- data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
- data/lib/pry/commands/cat/exception_formatter.rb +71 -60
- data/lib/pry/commands/cat/file_formatter.rb +55 -49
- data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
- data/lib/pry/commands/cd.rb +40 -35
- data/lib/pry/commands/change_inspector.rb +29 -22
- data/lib/pry/commands/change_prompt.rb +44 -39
- data/lib/pry/commands/clear_screen.rb +16 -10
- data/lib/pry/commands/code_collector.rb +148 -133
- data/lib/pry/commands/disable_pry.rb +23 -19
- data/lib/pry/commands/easter_eggs.rb +19 -30
- data/lib/pry/commands/edit.rb +184 -161
- data/lib/pry/commands/edit/exception_patcher.rb +21 -17
- data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
- data/lib/pry/commands/exit.rb +39 -35
- data/lib/pry/commands/exit_all.rb +24 -20
- data/lib/pry/commands/exit_program.rb +20 -16
- data/lib/pry/commands/find_method.rb +168 -160
- data/lib/pry/commands/fix_indent.rb +16 -12
- data/lib/pry/commands/help.rb +140 -133
- data/lib/pry/commands/hist.rb +151 -150
- data/lib/pry/commands/import_set.rb +20 -16
- data/lib/pry/commands/jump_to.rb +25 -21
- data/lib/pry/commands/list_inspectors.rb +35 -28
- data/lib/pry/commands/ls.rb +124 -102
- data/lib/pry/commands/ls/constants.rb +59 -42
- data/lib/pry/commands/ls/formatter.rb +50 -46
- data/lib/pry/commands/ls/globals.rb +38 -34
- data/lib/pry/commands/ls/grep.rb +17 -13
- data/lib/pry/commands/ls/instance_vars.rb +29 -27
- data/lib/pry/commands/ls/interrogatable.rb +18 -12
- data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
- data/lib/pry/commands/ls/local_names.rb +26 -22
- data/lib/pry/commands/ls/local_vars.rb +38 -28
- data/lib/pry/commands/ls/ls_entity.rb +47 -51
- data/lib/pry/commands/ls/methods.rb +44 -43
- data/lib/pry/commands/ls/methods_helper.rb +46 -42
- data/lib/pry/commands/ls/self_methods.rb +23 -22
- data/lib/pry/commands/nesting.rb +21 -17
- data/lib/pry/commands/play.rb +93 -82
- data/lib/pry/commands/pry_backtrace.rb +22 -17
- data/lib/pry/commands/pry_version.rb +15 -11
- data/lib/pry/commands/raise_up.rb +27 -22
- data/lib/pry/commands/reload_code.rb +60 -48
- data/lib/pry/commands/reset.rb +16 -12
- data/lib/pry/commands/ri.rb +55 -45
- data/lib/pry/commands/save_file.rb +45 -43
- data/lib/pry/commands/shell_command.rb +51 -51
- data/lib/pry/commands/shell_mode.rb +21 -17
- data/lib/pry/commands/show_doc.rb +80 -68
- data/lib/pry/commands/show_info.rb +189 -171
- data/lib/pry/commands/show_input.rb +16 -11
- data/lib/pry/commands/show_source.rb +110 -45
- data/lib/pry/commands/stat.rb +35 -31
- data/lib/pry/commands/switch_to.rb +21 -15
- data/lib/pry/commands/toggle_color.rb +20 -16
- data/lib/pry/commands/watch_expression.rb +89 -86
- data/lib/pry/commands/watch_expression/expression.rb +32 -27
- data/lib/pry/commands/whereami.rb +156 -148
- data/lib/pry/commands/wtf.rb +75 -50
- data/lib/pry/config.rb +307 -25
- data/lib/pry/config/attributable.rb +22 -0
- data/lib/pry/config/lazy_value.rb +29 -0
- data/lib/pry/config/memoized_value.rb +34 -0
- data/lib/pry/config/value.rb +24 -0
- data/lib/pry/control_d_handler.rb +28 -0
- data/lib/pry/core_extensions.rb +9 -7
- data/lib/pry/editor.rb +48 -21
- data/lib/pry/env.rb +18 -0
- data/lib/pry/exception_handler.rb +43 -0
- data/lib/pry/exceptions.rb +13 -16
- data/lib/pry/forwardable.rb +5 -1
- data/lib/pry/helpers.rb +2 -0
- data/lib/pry/helpers/base_helpers.rb +68 -197
- data/lib/pry/helpers/command_helpers.rb +50 -61
- data/lib/pry/helpers/documentation_helpers.rb +20 -13
- data/lib/pry/helpers/options_helpers.rb +14 -7
- data/lib/pry/helpers/platform.rb +7 -5
- data/lib/pry/helpers/table.rb +33 -26
- data/lib/pry/helpers/text.rb +17 -14
- data/lib/pry/history.rb +48 -56
- data/lib/pry/hooks.rb +21 -12
- data/lib/pry/indent.rb +54 -50
- data/lib/pry/input_completer.rb +248 -230
- data/lib/pry/input_lock.rb +8 -9
- data/lib/pry/inspector.rb +36 -24
- data/lib/pry/last_exception.rb +45 -45
- data/lib/pry/method.rb +141 -94
- data/lib/pry/method/disowned.rb +16 -4
- data/lib/pry/method/patcher.rb +12 -3
- data/lib/pry/method/weird_method_locator.rb +68 -44
- data/lib/pry/object_path.rb +33 -25
- data/lib/pry/output.rb +121 -35
- data/lib/pry/pager.rb +186 -180
- data/lib/pry/prompt.rb +123 -54
- data/lib/pry/pry_class.rb +61 -103
- data/lib/pry/pry_instance.rb +217 -215
- data/lib/pry/repl.rb +18 -22
- data/lib/pry/repl_file_loader.rb +27 -21
- data/lib/pry/ring.rb +11 -6
- data/lib/pry/slop.rb +574 -563
- data/lib/pry/slop/commands.rb +164 -169
- data/lib/pry/slop/option.rb +172 -168
- data/lib/pry/syntax_highlighter.rb +26 -0
- data/lib/pry/system_command_handler.rb +17 -0
- data/lib/pry/testable.rb +59 -61
- data/lib/pry/testable/evalable.rb +21 -12
- data/lib/pry/testable/mockable.rb +18 -10
- data/lib/pry/testable/pry_tester.rb +71 -56
- data/lib/pry/testable/utility.rb +29 -21
- data/lib/pry/testable/variables.rb +49 -43
- data/lib/pry/version.rb +3 -1
- data/lib/pry/warning.rb +27 -0
- data/lib/pry/wrapped_module.rb +51 -42
- data/lib/pry/wrapped_module/candidate.rb +21 -14
- metadata +35 -35
- data/lib/pry/commands.rb +0 -6
- data/lib/pry/commands/disabled_commands.rb +0 -2
- data/lib/pry/commands/gem_cd.rb +0 -26
- data/lib/pry/commands/gem_install.rb +0 -32
- data/lib/pry/commands/gem_list.rb +0 -33
- data/lib/pry/commands/gem_open.rb +0 -29
- data/lib/pry/commands/gem_readme.rb +0 -25
- data/lib/pry/commands/gem_search.rb +0 -40
- data/lib/pry/commands/gem_stats.rb +0 -83
- data/lib/pry/commands/gist.rb +0 -102
- data/lib/pry/commands/install_command.rb +0 -54
- data/lib/pry/config/behavior.rb +0 -255
- data/lib/pry/config/convenience.rb +0 -28
- data/lib/pry/config/default.rb +0 -159
- data/lib/pry/config/memoization.rb +0 -48
- data/lib/pry/platform.rb +0 -91
- data/lib/pry/plugins.rb +0 -122
- data/lib/pry/rubygem.rb +0 -84
- data/lib/pry/terminal.rb +0 -91
@@ -1,42 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class Pry
|
2
|
-
class Command
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
15
|
+
def format
|
16
|
+
raise CommandError, "No input expressions!" if numbered_input_items.empty?
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
25
|
+
content
|
26
|
+
else
|
27
|
+
decorate(Pry::Code(selected_input_items.first))
|
28
|
+
end
|
24
29
|
end
|
25
|
-
end
|
26
30
|
|
27
|
-
|
31
|
+
private
|
28
32
|
|
29
|
-
|
30
|
-
|
31
|
-
|
33
|
+
def selected_input_items
|
34
|
+
input_expressions[normalized_expression_range] || []
|
35
|
+
end
|
32
36
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
39
|
-
|
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
|
data/lib/pry/commands/cd.rb
CHANGED
@@ -1,41 +1,46 @@
|
|
1
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
44
|
+
Pry::Commands.add_command(Pry::Command::Cd)
|
45
|
+
end
|
41
46
|
end
|
@@ -1,27 +1,34 @@
|
|
1
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
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
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
-
|
13
|
+
Change the current prompt.
|
14
|
+
BANNER
|
25
15
|
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
2
|
-
class Command::CodeCollector
|
3
|
-
include Helpers::CommandHelpers
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
class Pry
|
4
|
+
class Command
|
5
|
+
class CodeCollector
|
6
|
+
include Helpers::CommandHelpers
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
attr_reader :args
|
9
|
+
attr_reader :opts
|
10
|
+
attr_reader :pry_instance
|
11
11
|
|
12
|
-
|
13
|
-
attr_accessor :
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
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
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
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
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
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
|
-
|
127
|
+
# Name of the object argument
|
128
|
+
def obj_name
|
129
|
+
@obj_name ||= args.empty? ? "" : args.join(" ")
|
130
|
+
end
|
119
131
|
|
120
|
-
|
121
|
-
[opts.present?(:in), opts.present?(:out),
|
122
|
-
!args.empty?].count(true) > 1
|
123
|
-
end
|
132
|
+
private
|
124
133
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
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
|
-
|
131
|
-
ranged_array.compact.each { |v| all << block.call(v) }
|
150
|
+
all
|
132
151
|
end
|
133
152
|
|
134
|
-
|
135
|
-
|
153
|
+
def code_object_doc
|
154
|
+
(code_object && code_object.doc) || could_not_locate(obj_name)
|
155
|
+
end
|
136
156
|
|
137
|
-
|
138
|
-
|
139
|
-
|
157
|
+
def code_object_source_or_file
|
158
|
+
(code_object && code_object.source) || file_content
|
159
|
+
end
|
140
160
|
|
141
|
-
|
142
|
-
|
143
|
-
|
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
|
-
|
146
|
-
|
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
|
-
|
156
|
-
|
157
|
-
end
|
175
|
+
def convert_to_range(range)
|
176
|
+
return range if range.is_a?(Range)
|
158
177
|
|
159
|
-
|
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
|