pry 0.10.2-i386-mswin32 → 1.0.0.pre1-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (189) hide show
  1. data/.document +2 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +21 -0
  4. data/.yardopts +3 -0
  5. data/CHANGELOG +503 -0
  6. data/CONTRIBUTORS +55 -0
  7. data/Gemfile +9 -0
  8. data/Guardfile +62 -0
  9. data/LICENSE +2 -2
  10. data/{README.md → README.markdown} +31 -37
  11. data/Rakefile +144 -0
  12. data/TODO +117 -0
  13. data/lib/pry.rb +146 -33
  14. data/lib/pry/cli.rb +13 -35
  15. data/lib/pry/code.rb +63 -24
  16. data/lib/pry/code/loc.rb +2 -2
  17. data/lib/pry/code_object.rb +21 -40
  18. data/lib/pry/command.rb +6 -9
  19. data/lib/pry/command_set.rb +37 -80
  20. data/lib/pry/commands.rb +1 -1
  21. data/lib/pry/commands/amend_line.rb +1 -1
  22. data/lib/pry/commands/bang.rb +1 -1
  23. data/lib/pry/commands/cat.rb +2 -11
  24. data/lib/pry/commands/cat/abstract_formatter.rb +1 -1
  25. data/lib/pry/commands/cat/exception_formatter.rb +7 -6
  26. data/lib/pry/commands/cat/file_formatter.rb +32 -15
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  28. data/lib/pry/commands/cd.rb +3 -14
  29. data/lib/pry/commands/code_collector.rb +4 -4
  30. data/lib/pry/commands/easter_eggs.rb +3 -3
  31. data/lib/pry/commands/edit.rb +22 -10
  32. data/lib/pry/commands/edit/exception_patcher.rb +1 -1
  33. data/lib/pry/commands/edit/file_and_line_locator.rb +2 -0
  34. data/lib/pry/{method/patcher.rb → commands/edit/method_patcher.rb} +37 -40
  35. data/lib/pry/commands/find_method.rb +22 -16
  36. data/lib/pry/commands/gem_install.rb +2 -5
  37. data/lib/pry/commands/gem_open.rb +1 -1
  38. data/lib/pry/commands/gist.rb +11 -10
  39. data/lib/pry/commands/help.rb +14 -14
  40. data/lib/pry/commands/hist.rb +5 -24
  41. data/lib/pry/commands/ls.rb +287 -56
  42. data/lib/pry/commands/play.rb +10 -44
  43. data/lib/pry/commands/pry_backtrace.rb +2 -1
  44. data/lib/pry/commands/raise_up.rb +1 -1
  45. data/lib/pry/commands/reload_code.rb +15 -31
  46. data/lib/pry/commands/ri.rb +3 -7
  47. data/lib/pry/commands/shell_command.rb +12 -17
  48. data/lib/pry/commands/shell_mode.rb +2 -2
  49. data/lib/pry/commands/show_doc.rb +0 -5
  50. data/lib/pry/commands/show_info.rb +10 -11
  51. data/lib/pry/commands/show_source.rb +3 -15
  52. data/lib/pry/commands/simple_prompt.rb +1 -1
  53. data/lib/pry/commands/toggle_color.rb +4 -8
  54. data/lib/pry/commands/whereami.rb +10 -18
  55. data/lib/pry/completion.rb +293 -0
  56. data/lib/pry/config.rb +233 -20
  57. data/lib/pry/core_extensions.rb +19 -29
  58. data/lib/pry/custom_completions.rb +6 -0
  59. data/lib/pry/editor.rb +103 -109
  60. data/lib/pry/helpers/base_helpers.rb +109 -22
  61. data/lib/pry/helpers/command_helpers.rb +8 -10
  62. data/lib/pry/helpers/documentation_helpers.rb +2 -1
  63. data/lib/pry/helpers/text.rb +5 -4
  64. data/lib/pry/history.rb +10 -21
  65. data/lib/pry/history_array.rb +0 -5
  66. data/lib/pry/hooks.rb +29 -9
  67. data/lib/pry/indent.rb +10 -5
  68. data/lib/pry/method.rb +86 -81
  69. data/lib/pry/method/weird_method_locator.rb +2 -4
  70. data/lib/pry/module_candidate.rb +14 -5
  71. data/lib/pry/pager.rb +48 -193
  72. data/lib/pry/plugins.rb +2 -2
  73. data/lib/pry/pry_class.rb +193 -104
  74. data/lib/pry/pry_instance.rb +154 -152
  75. data/lib/pry/rbx_method.rb +13 -0
  76. data/lib/pry/rbx_path.rb +1 -1
  77. data/lib/pry/repl.rb +14 -17
  78. data/lib/pry/repl_file_loader.rb +3 -8
  79. data/lib/pry/rubygem.rb +3 -3
  80. data/lib/pry/terminal.rb +3 -4
  81. data/lib/pry/test/helper.rb +11 -6
  82. data/lib/pry/version.rb +1 -1
  83. data/lib/pry/wrapped_module.rb +56 -49
  84. data/man/pry.1 +195 -0
  85. data/man/pry.1.html +204 -0
  86. data/man/pry.1.ronn +141 -0
  87. data/pry.gemspec +31 -0
  88. data/spec/Procfile +3 -0
  89. data/spec/cli_spec.rb +78 -0
  90. data/spec/code_object_spec.rb +277 -0
  91. data/spec/code_spec.rb +219 -0
  92. data/spec/command_helpers_spec.rb +29 -0
  93. data/spec/command_integration_spec.rb +562 -0
  94. data/spec/command_set_spec.rb +627 -0
  95. data/spec/command_spec.rb +821 -0
  96. data/spec/commands/amend_line_spec.rb +247 -0
  97. data/spec/commands/bang_spec.rb +18 -0
  98. data/spec/commands/cat_spec.rb +164 -0
  99. data/spec/commands/cd_spec.rb +250 -0
  100. data/spec/commands/disable_pry_spec.rb +25 -0
  101. data/spec/commands/edit_spec.rb +725 -0
  102. data/spec/commands/exit_all_spec.rb +27 -0
  103. data/spec/commands/exit_program_spec.rb +19 -0
  104. data/spec/commands/exit_spec.rb +28 -0
  105. data/spec/commands/find_method_spec.rb +70 -0
  106. data/spec/commands/gem_list_spec.rb +26 -0
  107. data/spec/commands/gist_spec.rb +79 -0
  108. data/spec/commands/help_spec.rb +56 -0
  109. data/spec/commands/hist_spec.rb +172 -0
  110. data/spec/commands/jump_to_spec.rb +15 -0
  111. data/spec/commands/ls_spec.rb +189 -0
  112. data/spec/commands/play_spec.rb +136 -0
  113. data/spec/commands/raise_up_spec.rb +56 -0
  114. data/spec/commands/save_file_spec.rb +177 -0
  115. data/spec/commands/show_doc_spec.rb +488 -0
  116. data/spec/commands/show_input_spec.rb +17 -0
  117. data/spec/commands/show_source_spec.rb +760 -0
  118. data/spec/commands/whereami_spec.rb +203 -0
  119. data/spec/completion_spec.rb +221 -0
  120. data/spec/control_d_handler_spec.rb +62 -0
  121. data/spec/documentation_helper_spec.rb +73 -0
  122. data/spec/editor_spec.rb +79 -0
  123. data/spec/exception_whitelist_spec.rb +21 -0
  124. data/spec/fixtures/candidate_helper1.rb +11 -0
  125. data/spec/fixtures/candidate_helper2.rb +8 -0
  126. data/spec/fixtures/example.erb +5 -0
  127. data/spec/fixtures/example_nesting.rb +33 -0
  128. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  129. data/spec/fixtures/testlinkrc +2 -0
  130. data/spec/fixtures/testrc +2 -0
  131. data/spec/fixtures/testrcbad +2 -0
  132. data/spec/fixtures/whereami_helper.rb +6 -0
  133. data/spec/helper.rb +35 -0
  134. data/spec/helpers/bacon.rb +86 -0
  135. data/spec/helpers/mock_pry.rb +44 -0
  136. data/spec/helpers/repl_tester.rb +112 -0
  137. data/spec/helpers/table_spec.rb +105 -0
  138. data/spec/history_array_spec.rb +67 -0
  139. data/spec/hooks_spec.rb +522 -0
  140. data/spec/indent_spec.rb +301 -0
  141. data/spec/method_spec.rb +482 -0
  142. data/spec/prompt_spec.rb +61 -0
  143. data/spec/pry_defaults_spec.rb +420 -0
  144. data/spec/pry_history_spec.rb +69 -0
  145. data/spec/pry_output_spec.rb +95 -0
  146. data/spec/pry_repl_spec.rb +86 -0
  147. data/spec/pry_spec.rb +394 -0
  148. data/spec/pryrc_spec.rb +97 -0
  149. data/spec/run_command_spec.rb +25 -0
  150. data/spec/sticky_locals_spec.rb +147 -0
  151. data/spec/syntax_checking_spec.rb +81 -0
  152. data/spec/wrapped_module_spec.rb +261 -0
  153. data/wiki/Customizing-pry.md +397 -0
  154. data/wiki/Home.md +4 -0
  155. metadata +272 -61
  156. checksums.yaml +0 -7
  157. data/CHANGELOG.md +0 -714
  158. data/lib/pry/code/code_file.rb +0 -103
  159. data/lib/pry/color_printer.rb +0 -55
  160. data/lib/pry/commands/change_inspector.rb +0 -27
  161. data/lib/pry/commands/change_prompt.rb +0 -26
  162. data/lib/pry/commands/list_inspectors.rb +0 -35
  163. data/lib/pry/commands/list_prompts.rb +0 -35
  164. data/lib/pry/commands/ls/constants.rb +0 -47
  165. data/lib/pry/commands/ls/formatter.rb +0 -49
  166. data/lib/pry/commands/ls/globals.rb +0 -48
  167. data/lib/pry/commands/ls/grep.rb +0 -21
  168. data/lib/pry/commands/ls/instance_vars.rb +0 -39
  169. data/lib/pry/commands/ls/interrogatable.rb +0 -18
  170. data/lib/pry/commands/ls/jruby_hacks.rb +0 -49
  171. data/lib/pry/commands/ls/local_names.rb +0 -35
  172. data/lib/pry/commands/ls/local_vars.rb +0 -39
  173. data/lib/pry/commands/ls/ls_entity.rb +0 -70
  174. data/lib/pry/commands/ls/methods.rb +0 -57
  175. data/lib/pry/commands/ls/methods_helper.rb +0 -46
  176. data/lib/pry/commands/ls/self_methods.rb +0 -32
  177. data/lib/pry/commands/watch_expression.rb +0 -105
  178. data/lib/pry/commands/watch_expression/expression.rb +0 -38
  179. data/lib/pry/config/behavior.rb +0 -139
  180. data/lib/pry/config/convenience.rb +0 -25
  181. data/lib/pry/config/default.rb +0 -161
  182. data/lib/pry/exceptions.rb +0 -78
  183. data/lib/pry/input_completer.rb +0 -242
  184. data/lib/pry/input_lock.rb +0 -132
  185. data/lib/pry/inspector.rb +0 -27
  186. data/lib/pry/last_exception.rb +0 -61
  187. data/lib/pry/object_path.rb +0 -82
  188. data/lib/pry/output.rb +0 -50
  189. data/lib/pry/prompt.rb +0 -26
@@ -1,39 +0,0 @@
1
- require 'pry/commands/ls/interrogatable'
2
-
3
- class Pry
4
- class Command::Ls < Pry::ClassCommand
5
- class InstanceVars < Pry::Command::Ls::Formatter
6
- include Pry::Command::Ls::Interrogatable
7
-
8
- def initialize(interrogatee, no_user_opts, opts, _pry_)
9
- super(_pry_)
10
- @interrogatee = interrogatee
11
- @no_user_opts = no_user_opts
12
- @default_switch = opts[:ivars]
13
- end
14
-
15
- def correct_opts?
16
- super || @no_user_opts
17
- end
18
-
19
- def output_self
20
- ivars = if Object === @interrogatee
21
- Pry::Method.safe_send(@interrogatee, :instance_variables)
22
- else
23
- [] #TODO: BasicObject support
24
- end
25
- kvars = Pry::Method.safe_send(interrogatee_mod, :class_variables)
26
- ivars_out = output_section('instance variables', format(:instance_var, ivars))
27
- kvars_out = output_section('class variables', format(:class_var, kvars))
28
- ivars_out + kvars_out
29
- end
30
-
31
- private
32
-
33
- def format(type, vars)
34
- vars.sort_by { |var| var.to_s.downcase }.map { |var| color(type, var) }
35
- end
36
-
37
- end
38
- end
39
- end
@@ -1,18 +0,0 @@
1
- module Pry::Command::Ls::Interrogatable
2
-
3
- private
4
-
5
- def interrogating_a_module?
6
- Module === @interrogatee
7
- end
8
-
9
- def interrogatee_mod
10
- if interrogating_a_module?
11
- @interrogatee
12
- else
13
- singleton = Pry::Method.singleton_class_of(@interrogatee)
14
- singleton.ancestors.grep(::Class).reject { |c| c == singleton }.first
15
- end
16
- end
17
-
18
- end
@@ -1,49 +0,0 @@
1
- module Pry::Command::Ls::JRubyHacks
2
-
3
- private
4
-
5
- # JRuby creates lots of aliases for methods imported from java in an attempt
6
- # to make life easier for ruby programmers. (e.g. getFooBar becomes
7
- # get_foo_bar and foo_bar, and maybe foo_bar? if it returns a Boolean). The
8
- # full transformations are in the assignAliases method of:
9
- # https://github.com/jruby/jruby/blob/master/src/org/jruby/javasupport/JavaClass.java
10
- #
11
- # This has the unfortunate side-effect of making the output of ls even more
12
- # incredibly verbose than it normally would be for these objects; and so we
13
- # filter out all but the nicest of these aliases here.
14
- #
15
- # TODO: This is a little bit vague, better heuristics could be used.
16
- # JRuby also has a lot of scala-specific logic, which we don't copy.
17
- def trim_jruby_aliases(methods)
18
- grouped = methods.group_by do |m|
19
- m.name.sub(/\A(is|get|set)(?=[A-Z_])/, '').gsub(/[_?=]/, '').downcase
20
- end
21
-
22
- grouped.map do |key, values|
23
- values = values.sort_by do |m|
24
- rubbishness(m.name)
25
- end
26
-
27
- found = []
28
- values.select do |x|
29
- (!found.any? { |y| x == y }) && found << x
30
- end
31
- end.flatten(1)
32
- end
33
-
34
- # When removing jruby aliases, we want to keep the alias that is
35
- # "least rubbish" according to this metric.
36
- def rubbishness(name)
37
- name.each_char.map { |x|
38
- case x
39
- when /[A-Z]/
40
- 1
41
- when '?', '=', '!'
42
- -2
43
- else
44
- 0
45
- end
46
- }.inject(&:+) + (name.size / 100.0)
47
- end
48
-
49
- end
@@ -1,35 +0,0 @@
1
- class Pry
2
- class Command::Ls < Pry::ClassCommand
3
- class LocalNames < Pry::Command::Ls::Formatter
4
-
5
- def initialize(no_user_opts, args, _pry_)
6
- super(_pry_)
7
- @no_user_opts = no_user_opts
8
- @args = args
9
- @sticky_locals = _pry_.sticky_locals
10
- end
11
-
12
- def correct_opts?
13
- super || (@no_user_opts && @args.empty?)
14
- end
15
-
16
- def output_self
17
- local_vars = grep.regexp[@target.eval('local_variables')]
18
- output_section('locals', format(local_vars))
19
- end
20
-
21
- private
22
-
23
- def format(locals)
24
- locals.sort_by(&:downcase).map do |name|
25
- if @sticky_locals.include?(name.to_sym)
26
- color(:pry_var, name)
27
- else
28
- color(:local_var, name)
29
- end
30
- end
31
- end
32
-
33
- end
34
- end
35
- end
@@ -1,39 +0,0 @@
1
- class Pry
2
- class Command::Ls < Pry::ClassCommand
3
- class LocalVars < Pry::Command::Ls::Formatter
4
-
5
- def initialize(opts, _pry_)
6
- super(_pry_)
7
- @default_switch = opts[:locals]
8
- @sticky_locals = _pry_.sticky_locals
9
- end
10
-
11
- def output_self
12
- name_value_pairs = @target.eval('local_variables').reject { |e|
13
- @sticky_locals.keys.include?(e.to_sym)
14
- }.map { |name|
15
- [name, (@target.eval(name.to_s))]
16
- }
17
- format(name_value_pairs).join('')
18
- end
19
-
20
- private
21
-
22
- def format(name_value_pairs)
23
- name_value_pairs.sort_by { |name, value|
24
- value.to_s.size
25
- }.reverse.map { |name, value|
26
- colorized_assignment_style(name, format_value(value))
27
- }
28
- end
29
-
30
- def colorized_assignment_style(lhs, rhs, desired_width = 7)
31
- colorized_lhs = color(:local_var, lhs)
32
- color_escape_padding = colorized_lhs.size - lhs.size
33
- pad = desired_width + color_escape_padding
34
- "%-#{pad}s = %s" % [color(:local_var, colorized_lhs), rhs]
35
- end
36
-
37
- end
38
- end
39
- end
@@ -1,70 +0,0 @@
1
- require 'pry/commands/ls/grep'
2
- require 'pry/commands/ls/formatter'
3
- require 'pry/commands/ls/globals'
4
- require 'pry/commands/ls/constants'
5
- require 'pry/commands/ls/methods'
6
- require 'pry/commands/ls/self_methods'
7
- require 'pry/commands/ls/instance_vars'
8
- require 'pry/commands/ls/local_names'
9
- require 'pry/commands/ls/local_vars'
10
-
11
- class Pry
12
- class Command::Ls < Pry::ClassCommand
13
-
14
- class LsEntity
15
- attr_reader :_pry_
16
-
17
- def initialize(opts)
18
- @interrogatee = opts[:interrogatee]
19
- @no_user_opts = opts[:no_user_opts]
20
- @opts = opts[:opts]
21
- @args = opts[:args]
22
- @grep = Grep.new(Regexp.new(opts[:opts][:G] || '.'))
23
- @_pry_ = opts.delete(:_pry_)
24
- end
25
-
26
- def entities_table
27
- entities.map(&:write_out).reject { |o| !o }.join('')
28
- end
29
-
30
- private
31
-
32
- def grep(entity)
33
- entity.tap { |o| o.grep = @grep }
34
- end
35
-
36
- def globals
37
- grep Globals.new(@opts, _pry_)
38
- end
39
-
40
- def constants
41
- grep Constants.new(@interrogatee, @no_user_opts, @opts, _pry_)
42
- end
43
-
44
- def methods
45
- grep(Methods.new(@interrogatee, @no_user_opts, @opts, _pry_))
46
- end
47
-
48
- def self_methods
49
- grep SelfMethods.new(@interrogatee, @no_user_opts, @opts, _pry_)
50
- end
51
-
52
- def instance_vars
53
- grep InstanceVars.new(@interrogatee, @no_user_opts, @opts, _pry_)
54
- end
55
-
56
- def local_names
57
- grep LocalNames.new(@no_user_opts, @args, _pry_)
58
- end
59
-
60
- def local_vars
61
- LocalVars.new(@opts, _pry_)
62
- end
63
-
64
- def entities
65
- [globals, constants, methods, self_methods, instance_vars, local_names,
66
- local_vars]
67
- end
68
- end
69
- end
70
- end
@@ -1,57 +0,0 @@
1
- require 'pry/commands/ls/methods_helper'
2
- require 'pry/commands/ls/interrogatable'
3
-
4
- class Pry
5
- class Command::Ls < Pry::ClassCommand
6
- class Methods < Pry::Command::Ls::Formatter
7
-
8
- include Pry::Command::Ls::Interrogatable
9
- include Pry::Command::Ls::MethodsHelper
10
-
11
- def initialize(interrogatee, no_user_opts, opts, _pry_)
12
- super(_pry_)
13
- @interrogatee = interrogatee
14
- @no_user_opts = no_user_opts
15
- @default_switch = opts[:methods]
16
- @instance_methods_switch = opts['instance-methods']
17
- @ppp_switch = opts[:ppp]
18
- @jruby_switch = opts['all-java']
19
- @quiet_switch = opts[:quiet]
20
- @verbose_switch = opts[:verbose]
21
- end
22
-
23
- def output_self
24
- methods = all_methods.group_by(&:owner)
25
- # Reverse the resolution order so that the most useful information
26
- # appears right by the prompt.
27
- resolution_order.take_while(&below_ceiling).reverse.map do |klass|
28
- methods_here = (methods[klass] || []).select { |m| grep.regexp[m.name] }
29
- heading = "#{ Pry::WrappedModule.new(klass).method_prefix }methods"
30
- output_section(heading, format(methods_here))
31
- end.join('')
32
- end
33
-
34
- private
35
-
36
- def correct_opts?
37
- super || @instance_methods_switch || @ppp_switch || @no_user_opts
38
- end
39
-
40
-
41
- # Get a lambda that can be used with `take_while` to prevent over-eager
42
- # traversal of the Object's ancestry graph.
43
- def below_ceiling
44
- ceiling = if @quiet_switch
45
- [Pry::Method.safe_send(interrogatee_mod, :ancestors)[1]] +
46
- _pry_.config.ls.ceiling
47
- elsif @verbose_switch
48
- []
49
- else
50
- _pry_.config.ls.ceiling.dup
51
- end
52
- lambda { |klass| !ceiling.include?(klass) }
53
- end
54
-
55
- end
56
- end
57
- end
@@ -1,46 +0,0 @@
1
- require 'pry/commands/ls/jruby_hacks'
2
-
3
- module Pry::Command::Ls::MethodsHelper
4
-
5
- include Pry::Command::Ls::JRubyHacks
6
-
7
- private
8
-
9
- # Get all the methods that we'll want to output.
10
- def all_methods(instance_methods = false)
11
- methods = if instance_methods || @instance_methods_switch
12
- Pry::Method.all_from_class(@interrogatee)
13
- else
14
- Pry::Method.all_from_obj(@interrogatee)
15
- end
16
-
17
- if Pry::Helpers::BaseHelpers.jruby? && !@jruby_switch
18
- methods = trim_jruby_aliases(methods)
19
- end
20
-
21
- methods.select { |method| @ppp_switch || method.visibility == :public }
22
- end
23
-
24
- def resolution_order
25
- if @instance_methods_switch
26
- Pry::Method.instance_resolution_order(@interrogatee)
27
- else
28
- Pry::Method.resolution_order(@interrogatee)
29
- end
30
- end
31
-
32
- def format(methods)
33
- methods.sort_by(&:name).map do |method|
34
- if method.name == 'method_missing'
35
- color(:method_missing, 'method_missing')
36
- elsif method.visibility == :private
37
- color(:private_method, method.name)
38
- elsif method.visibility == :protected
39
- color(:protected_method, method.name)
40
- else
41
- color(:public_method, method.name)
42
- end
43
- end
44
- end
45
-
46
- end
@@ -1,32 +0,0 @@
1
- require 'pry/commands/ls/interrogatable'
2
- require 'pry/commands/ls/methods_helper'
3
-
4
- class Pry
5
- class Command::Ls < Pry::ClassCommand
6
- class SelfMethods < Pry::Command::Ls::Formatter
7
- include Pry::Command::Ls::Interrogatable
8
- include Pry::Command::Ls::MethodsHelper
9
-
10
- def initialize(interrogatee, no_user_opts, opts, _pry_)
11
- super(_pry_)
12
- @interrogatee = interrogatee
13
- @no_user_opts = no_user_opts
14
- end
15
-
16
- def output_self
17
- methods = all_methods(true).select do |m|
18
- m.owner == @interrogatee && grep.regexp[m.name]
19
- end
20
- heading = "#{ Pry::WrappedModule.new(@interrogatee).method_prefix }methods"
21
- output_section(heading, format(methods))
22
- end
23
-
24
- private
25
-
26
- def correct_opts?
27
- @no_user_opts && interrogating_a_module?
28
- end
29
-
30
- end
31
- end
32
- end
@@ -1,105 +0,0 @@
1
- class Pry
2
- class Command::WatchExpression < Pry::ClassCommand
3
- require 'pry/commands/watch_expression/expression.rb'
4
-
5
- match 'watch'
6
- group 'Context'
7
- description 'Watch the value of an expression and print a notification whenever it changes.'
8
- command_options :use_prefix => false
9
-
10
- banner <<-'BANNER'
11
- Usage: watch [EXPRESSION]
12
- watch
13
- watch --delete [INDEX]
14
-
15
- watch [EXPRESSION] adds an expression to the list of those being watched.
16
- It will be re-evaluated every time you hit enter in pry. If its value has
17
- changed, the new value will be printed to the console.
18
-
19
- This is useful if you are step-through debugging and want to see how
20
- something changes over time. It's also useful if you're trying to write
21
- a method inside pry and want to check that it gives the right answers
22
- every time you redefine it.
23
-
24
- watch on its own displays all the currently watched expressions and their
25
- values, and watch --delete [INDEX] allows you to delete expressions from
26
- the list being watched.
27
- BANNER
28
-
29
- def options(opt)
30
- opt.on :d, :delete,
31
- "Delete the watch expression with the given index. If no index is given; clear all watch expressions.",
32
- :optional_argument => true, :as => Integer
33
- opt.on :l, :list,
34
- "Show all current watch expressions and their values. Calling watch with no expressions or options will also show the watch expressions."
35
- end
36
-
37
- def process
38
- case
39
- when opts.present?(:delete)
40
- delete opts[:delete]
41
- when opts.present?(:list) || args.empty?
42
- list
43
- else
44
- add_hook
45
- add_expression(args)
46
- end
47
- end
48
-
49
- private
50
-
51
- def expressions
52
- _pry_.config.watch_expressions ||= []
53
- end
54
-
55
- def delete(index)
56
- if index
57
- output.puts "Deleting watch expression ##{index}: #{expressions[index-1]}"
58
- expressions.delete_at(index-1)
59
- else
60
- output.puts "Deleting all watched expressions"
61
- expressions.clear
62
- end
63
- end
64
-
65
- def list
66
- if expressions.empty?
67
- output.puts "No watched expressions"
68
- else
69
- _pry_.pager.open do |pager|
70
- pager.puts "Listing all watched expressions:"
71
- pager.puts ""
72
- expressions.each_with_index do |expr, index|
73
- pager.print text.with_line_numbers(expr.to_s, index+1)
74
- end
75
- pager.puts ""
76
- end
77
- end
78
- end
79
-
80
- def eval_and_print_changed(output)
81
- expressions.each do |expr|
82
- expr.eval!
83
- if expr.changed?
84
- output.puts "#{text.blue "watch"}: #{expr.to_s}"
85
- end
86
- end
87
- end
88
-
89
- def add_expression(arguments)
90
- expressions << Expression.new(_pry_, target, arg_string)
91
- output.puts "Watching #{Code.new(arg_string).highlighted}"
92
- end
93
-
94
- def add_hook
95
- hook = [:after_eval, :watch_expression]
96
- unless _pry_.hooks.hook_exists?(*hook)
97
- _pry_.hooks.add_hook(*hook) do |_, _pry_|
98
- eval_and_print_changed _pry_.output
99
- end
100
- end
101
- end
102
- end
103
-
104
- Pry::Commands.add_command(Pry::Command::WatchExpression)
105
- end