pry 0.11.3-java → 0.12.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +139 -1
  3. data/LICENSE +1 -1
  4. data/README.md +13 -30
  5. data/bin/pry +0 -4
  6. data/lib/pry.rb +17 -47
  7. data/lib/pry/cli.rb +17 -24
  8. data/lib/pry/code.rb +6 -6
  9. data/lib/pry/code/code_file.rb +5 -4
  10. data/lib/pry/code/code_range.rb +3 -3
  11. data/lib/pry/code/loc.rb +14 -8
  12. data/lib/pry/code_object.rb +4 -4
  13. data/lib/pry/color_printer.rb +1 -0
  14. data/lib/pry/command.rb +36 -29
  15. data/lib/pry/command_set.rb +17 -52
  16. data/lib/pry/commands/amend_line.rb +3 -4
  17. data/lib/pry/commands/bang.rb +1 -1
  18. data/lib/pry/commands/cat.rb +7 -6
  19. data/lib/pry/commands/cat/exception_formatter.rb +9 -8
  20. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  21. data/lib/pry/commands/change_prompt.rb +29 -9
  22. data/lib/pry/commands/clear_screen.rb +14 -0
  23. data/lib/pry/commands/code_collector.rb +9 -9
  24. data/lib/pry/commands/easter_eggs.rb +3 -3
  25. data/lib/pry/commands/edit.rb +8 -7
  26. data/lib/pry/commands/exit.rb +2 -1
  27. data/lib/pry/commands/find_method.rb +11 -13
  28. data/lib/pry/commands/gem_cd.rb +1 -1
  29. data/lib/pry/commands/gem_install.rb +2 -2
  30. data/lib/pry/commands/gem_list.rb +2 -2
  31. data/lib/pry/commands/gem_open.rb +1 -1
  32. data/lib/pry/commands/gem_search.rb +2 -2
  33. data/lib/pry/commands/gem_stats.rb +83 -0
  34. data/lib/pry/commands/gist.rb +7 -6
  35. data/lib/pry/commands/help.rb +3 -3
  36. data/lib/pry/commands/hist.rb +9 -8
  37. data/lib/pry/commands/import_set.rb +2 -1
  38. data/lib/pry/commands/install_command.rb +7 -6
  39. data/lib/pry/commands/list_inspectors.rb +2 -2
  40. data/lib/pry/commands/ls.rb +27 -30
  41. data/lib/pry/commands/ls/constants.rb +4 -4
  42. data/lib/pry/commands/ls/formatter.rb +3 -2
  43. data/lib/pry/commands/ls/globals.rb +0 -2
  44. data/lib/pry/commands/ls/grep.rb +0 -2
  45. data/lib/pry/commands/ls/instance_vars.rb +0 -1
  46. data/lib/pry/commands/ls/local_names.rb +0 -2
  47. data/lib/pry/commands/ls/local_vars.rb +0 -2
  48. data/lib/pry/commands/ls/ls_entity.rb +0 -1
  49. data/lib/pry/commands/ls/methods.rb +0 -3
  50. data/lib/pry/commands/ls/methods_helper.rb +1 -1
  51. data/lib/pry/commands/ls/self_methods.rb +0 -1
  52. data/lib/pry/commands/play.rb +1 -2
  53. data/lib/pry/commands/pry_backtrace.rb +1 -1
  54. data/lib/pry/commands/raise_up.rb +2 -1
  55. data/lib/pry/commands/ri.rb +5 -4
  56. data/lib/pry/commands/shell_command.rb +3 -2
  57. data/lib/pry/commands/shell_mode.rb +6 -6
  58. data/lib/pry/commands/show_doc.rb +5 -7
  59. data/lib/pry/commands/show_info.rb +25 -18
  60. data/lib/pry/commands/show_source.rb +5 -2
  61. data/lib/pry/commands/stat.rb +1 -1
  62. data/lib/pry/commands/watch_expression.rb +9 -7
  63. data/lib/pry/commands/whereami.rb +4 -4
  64. data/lib/pry/commands/wtf.rb +15 -2
  65. data/lib/pry/config.rb +33 -9
  66. data/lib/pry/config/behavior.rb +229 -205
  67. data/lib/pry/config/convenience.rb +24 -21
  68. data/lib/pry/config/default.rb +153 -143
  69. data/lib/pry/config/memoization.rb +41 -37
  70. data/lib/pry/core_extensions.rb +4 -3
  71. data/lib/pry/editor.rb +5 -12
  72. data/lib/pry/exceptions.rb +0 -2
  73. data/lib/pry/helpers.rb +1 -0
  74. data/lib/pry/helpers/base_helpers.rb +133 -4
  75. data/lib/pry/helpers/command_helpers.rb +5 -4
  76. data/lib/pry/helpers/documentation_helpers.rb +2 -2
  77. data/lib/pry/helpers/options_helpers.rb +5 -5
  78. data/lib/pry/helpers/platform.rb +58 -0
  79. data/lib/pry/helpers/table.rb +20 -15
  80. data/lib/pry/helpers/text.rb +3 -4
  81. data/lib/pry/history.rb +21 -8
  82. data/lib/pry/hooks.rb +3 -3
  83. data/lib/pry/indent.rb +15 -17
  84. data/lib/pry/input_completer.rb +12 -7
  85. data/lib/pry/input_lock.rb +0 -2
  86. data/lib/pry/last_exception.rb +1 -1
  87. data/lib/pry/method.rb +37 -31
  88. data/lib/pry/method/disowned.rb +2 -1
  89. data/lib/pry/method/patcher.rb +2 -2
  90. data/lib/pry/method/weird_method_locator.rb +7 -8
  91. data/lib/pry/object_path.rb +5 -4
  92. data/lib/pry/output.rb +3 -2
  93. data/lib/pry/pager.rb +4 -3
  94. data/lib/pry/platform.rb +79 -81
  95. data/lib/pry/plugins.rb +7 -3
  96. data/lib/pry/prompt.rb +144 -25
  97. data/lib/pry/pry_class.rb +53 -29
  98. data/lib/pry/pry_instance.rb +88 -55
  99. data/lib/pry/repl.rb +33 -4
  100. data/lib/pry/repl_file_loader.rb +1 -2
  101. data/lib/pry/ring.rb +84 -0
  102. data/lib/pry/rubygem.rb +6 -6
  103. data/lib/pry/slop.rb +17 -17
  104. data/lib/pry/slop/commands.rb +3 -4
  105. data/lib/pry/slop/option.rb +19 -21
  106. data/lib/pry/terminal.rb +2 -1
  107. data/lib/pry/testable/mockable.rb +2 -2
  108. data/lib/pry/testable/pry_tester.rb +1 -1
  109. data/lib/pry/testable/utility.rb +2 -2
  110. data/lib/pry/testable/variables.rb +1 -1
  111. data/lib/pry/version.rb +1 -1
  112. data/lib/pry/wrapped_module.rb +15 -15
  113. data/lib/pry/wrapped_module/candidate.rb +2 -2
  114. metadata +19 -31
  115. data/lib/pry/commands/list_prompts.rb +0 -35
  116. data/lib/pry/commands/simple_prompt.rb +0 -22
  117. data/lib/pry/history_array.rb +0 -121
  118. data/lib/pry/rbx_path.rb +0 -22
@@ -20,7 +20,7 @@ class Pry
20
20
  #
21
21
  # @param [Object] receiver
22
22
  # @param [String] method_name
23
- def initialize(receiver, method_name, binding=nil)
23
+ def initialize(receiver, method_name)
24
24
  @receiver, @name = receiver, method_name
25
25
  @method = nil
26
26
  end
@@ -47,6 +47,7 @@ class Pry
47
47
  # Raise a more useful error message instead of trying to forward to nil.
48
48
  def method_missing(meth_name, *args, &block)
49
49
  raise "Cannot call '#{meth_name}' on an undef'd method." if method(:name).respond_to?(meth_name)
50
+
50
51
  Object.instance_method(:method_missing).bind(self).call(meth_name, *args, &block)
51
52
  end
52
53
  end
@@ -94,7 +94,7 @@ class Pry
94
94
  def wrap_for_owner(source)
95
95
  Pry.current[:pry_owner] = method.owner
96
96
  owner_source = definition_for_owner(source)
97
- visibility_fix = "#{method.visibility.to_s} #{method.name.to_sym.inspect}"
97
+ visibility_fix = "#{method.visibility} #{method.name.to_sym.inspect}"
98
98
  "Pry.current[:pry_owner].class_eval do; #{owner_source}\n#{visibility_fix}\nend"
99
99
  end
100
100
 
@@ -113,7 +113,7 @@ class Pry
113
113
  def wrap_for_nesting(source)
114
114
  nesting = Pry::Code.from_file(method.source_file).nesting_at(method.source_line)
115
115
 
116
- (nesting + [source] + nesting.map{ "end" } + [""]).join(";")
116
+ (nesting + [source] + nesting.map { "end" } + [""]).join(";")
117
117
  rescue Pry::Indent::UnparseableNestingError
118
118
  source
119
119
  end
@@ -1,6 +1,5 @@
1
1
  class Pry
2
2
  class Method
3
-
4
3
  # This class is responsible for locating the *real* `Pry::Method`
5
4
  # object captured by a binding.
6
5
  #
@@ -16,7 +15,6 @@ class Pry
16
15
  # Pry::Method and return it, or return nil if we fail.
17
16
  class WeirdMethodLocator
18
17
  class << self
19
-
20
18
  # Whether the given method object matches the associated binding.
21
19
  # If the method object does not match the binding, then it's
22
20
  # most likely not the method captured by the binding, and we
@@ -67,7 +65,7 @@ class Pry
67
65
 
68
66
  def skip_superclass_search?
69
67
  target_mod = @target.eval('self').class
70
- target_mod.ancestors.take_while {|mod| mod != target_mod }.any?
68
+ target_mod.ancestors.take_while { |mod| mod != target_mod }.any?
71
69
  end
72
70
 
73
71
  def normal_method?(method)
@@ -100,8 +98,7 @@ class Pry
100
98
  # __FILE__ and __LINE__ the binding is at, we can hope to disambiguate these cases.
101
99
  #
102
100
  # This obviously won't work if the source is unavaiable for some reason, or if both
103
- # methods have the same __FILE__ and __LINE__, or if we're in rbx where b.eval('__LINE__')
104
- # is broken.
101
+ # methods have the same __FILE__ and __LINE__.
105
102
  #
106
103
  # @return [Pry::Method, nil] The Pry::Method representing the
107
104
  # superclass method.
@@ -110,6 +107,7 @@ class Pry
110
107
  if skip_superclass_search?
111
108
  return guess
112
109
  end
110
+
113
111
  while guess
114
112
  # needs rescue if this is a Disowned method or a C method or something...
115
113
  # TODO: Fix up the exception handling so we don't need a bare rescue
@@ -122,9 +120,9 @@ class Pry
122
120
  end
123
121
  end
124
122
 
125
- # Uhoh... none of the methods in the chain had the right __FILE__ and __LINE__
126
- # This may be caused by rbx https://github.com/rubinius/rubinius/issues/953,
127
- # or other unknown circumstances (TODO: we should warn the user when this happens)
123
+ # Uhoh... none of the methods in the chain had the right `__FILE__` and
124
+ # `__LINE__` due to unknown circumstances.
125
+ # TODO: we should warn the user when this happens.
128
126
  nil
129
127
  end
130
128
 
@@ -136,6 +134,7 @@ class Pry
136
134
  # renamed method
137
135
  def find_renamed_method
138
136
  return if !valid_file?(target_file)
137
+
139
138
  alias_name = all_methods_for(target_self).find do |v|
140
139
  expanded_source_location(target_self.method(v).source_location) == renamed_method_source_location
141
140
  end
@@ -28,7 +28,7 @@ class Pry
28
28
  stack = @current_stack.dup
29
29
 
30
30
  begin
31
- next_segment = ""
31
+ next_segment = ""
32
32
 
33
33
  loop do
34
34
  # Scan for as long as we don't see a slash
@@ -74,9 +74,10 @@ class Pry
74
74
  "Exception: #{err.inspect}"
75
75
  ].join("\n")
76
76
 
77
- raise CommandError.new(msg).tap { |e|
78
- e.set_backtrace err.backtrace
79
- }
77
+ command_error = CommandError.new(msg)
78
+ command_error.set_backtrace(err.backtrace)
79
+
80
+ raise command_error
80
81
  end
81
82
  end
82
83
  end
@@ -8,8 +8,9 @@ class Pry::Output
8
8
 
9
9
  def puts(*objs)
10
10
  return print "\n" if objs.empty?
11
+
11
12
  objs.each do |obj|
12
- if ary = Array.try_convert(obj)
13
+ if (ary = Array.try_convert(obj))
13
14
  puts(*ary)
14
15
  else
15
16
  print "#{obj.to_s.chomp}\n"
@@ -35,7 +36,7 @@ class Pry::Output
35
36
  @boxed_io.__send__(name, *args, &block)
36
37
  end
37
38
 
38
- def respond_to_missing?(m, include_all=false)
39
+ def respond_to_missing?(m, include_all = false)
39
40
  @boxed_io.respond_to?(m, include_all)
40
41
  end
41
42
 
@@ -51,7 +51,7 @@ class Pry::Pager
51
51
  def best_available
52
52
  if !_pry_.config.pager
53
53
  NullPager.new(_pry_.output)
54
- elsif !SystemPager.available? || Pry::Helpers::BaseHelpers.jruby?
54
+ elsif !SystemPager.available? || Helpers::Platform.jruby?
55
55
  SimplePager.new(_pry_.output)
56
56
  else
57
57
  SystemPager.new(_pry_.output)
@@ -111,6 +111,7 @@ class Pry::Pager
111
111
  @out.print "<page break> --- Press enter to continue " \
112
112
  "( q<enter> to break ) --- <page break>\n"
113
113
  raise StopPaging if Readline.readline("").chomp == "q"
114
+
114
115
  @tracker.reset
115
116
  end
116
117
  end
@@ -138,8 +139,8 @@ class Pry::Pager
138
139
  if @system_pager.nil?
139
140
  @system_pager = begin
140
141
  pager_executable = default_pager.split(' ').first
141
- if Pry::Helpers::BaseHelpers.windows? || Pry::Helpers::BaseHelpers.windows_ansi?
142
- `where #{pager_executable}`
142
+ if Helpers::Platform.windows? || Helpers::Platform.windows_ansi?
143
+ `where /Q #{pager_executable}`
143
144
  else
144
145
  `which #{pager_executable}`
145
146
  end
@@ -1,93 +1,91 @@
1
- module Pry::Platform
2
- extend self
1
+ class Pry
2
+ module Platform
3
+ extend self
3
4
 
4
- #
5
- # @return [Boolean]
6
- # Returns true if Pry is running on Mac OSX.
7
- #
8
- # @note
9
- # Queries RbConfig::CONFIG['host_os'] with a best guess.
10
- #
11
- def mac_osx?
12
- !!(RbConfig::CONFIG['host_os'] =~ /\Adarwin/i)
13
- end
5
+ #
6
+ # @return [Boolean]
7
+ # Returns true if Pry is running on Mac OSX.
8
+ #
9
+ # @note
10
+ # Queries RbConfig::CONFIG['host_os'] with a best guess.
11
+ #
12
+ def mac_osx?
13
+ !!(RbConfig::CONFIG['host_os'] =~ /\Adarwin/i)
14
+ end
14
15
 
15
- #
16
- # @return [Boolean]
17
- # Returns true if Pry is running on Linux.
18
- #
19
- # @note
20
- # Queries RbConfig::CONFIG['host_os'] with a best guess.
21
- #
22
- def linux?
23
- !!(RbConfig::CONFIG['host_os'] =~ /linux/i)
24
- end
16
+ #
17
+ # @return [Boolean]
18
+ # Returns true if Pry is running on Linux.
19
+ #
20
+ # @note
21
+ # Queries RbConfig::CONFIG['host_os'] with a best guess.
22
+ #
23
+ def linux?
24
+ !!(RbConfig::CONFIG['host_os'] =~ /linux/i)
25
+ end
25
26
 
26
- #
27
- # @return [Boolean]
28
- # Returns true if Pry is running on Windows.
29
- #
30
- # @note
31
- # Queries RbConfig::CONFIG['host_os'] with a best guess.
32
- #
33
- def windows?
34
- !!(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
35
- end
27
+ #
28
+ # @return [Boolean]
29
+ # Returns true if Pry is running on Windows.
30
+ #
31
+ # @note
32
+ # Queries RbConfig::CONFIG['host_os'] with a best guess.
33
+ #
34
+ def windows?
35
+ !!(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
36
+ end
36
37
 
37
- #
38
- # @return [Boolean]
39
- # Returns true when Pry is running on Windows with ANSI support.
40
- #
41
- def windows_ansi?
42
- return false if not windows?
43
- !!(defined?(Win32::Console) or ENV['ANSICON'] or mri_2?)
44
- end
38
+ #
39
+ # @return [Boolean]
40
+ # Returns true when Pry is running on Windows with ANSI support.
41
+ #
42
+ def windows_ansi?
43
+ return false if not windows?
45
44
 
46
- #
47
- # @return [Boolean]
48
- # Returns true when Pry is being run from JRuby.
49
- #
50
- def jruby?
51
- RbConfig::CONFIG['ruby_install_name'] == 'jruby'
52
- end
45
+ !!(defined?(Win32::Console) or ENV['ANSICON'] or mri_2?)
46
+ end
53
47
 
54
- #
55
- # @return [Boolean]
56
- # Returns true when Pry is being run from JRuby in 1.9 mode.
57
- #
58
- def jruby_19?
59
- jruby? and RbConfig::CONFIG['ruby_version'] == '1.9'
60
- end
48
+ #
49
+ # @return [Boolean]
50
+ # Returns true when Pry is being run from JRuby.
51
+ #
52
+ def jruby?
53
+ RbConfig::CONFIG['ruby_install_name'] == 'jruby'
54
+ end
61
55
 
62
- #
63
- # @return [Boolean]
64
- # Returns true when Pry is being run from Rubinius.
65
- #
66
- def rbx?
67
- RbConfig::CONFIG['ruby_install_name'] == 'rbx'
68
- end
56
+ #
57
+ # @return [Boolean]
58
+ # Returns true when Pry is being run from JRuby in 1.9 mode.
59
+ #
60
+ def jruby_19?
61
+ jruby? and RbConfig::CONFIG['ruby_version'] == '1.9'
62
+ end
69
63
 
70
- #
71
- # @return [Boolean]
72
- # Returns true when Pry is being run from MRI (CRuby).
73
- #
74
- def mri?
75
- RbConfig::CONFIG['ruby_install_name'] == 'ruby'
76
- end
64
+ #
65
+ # @return [Boolean]
66
+ # Returns true when Pry is being run from MRI (CRuby).
67
+ #
68
+ def mri?
69
+ RbConfig::CONFIG['ruby_install_name'] == 'ruby'
70
+ end
77
71
 
78
- #
79
- # @return [Boolean]
80
- # Returns true when Pry is being run from MRI v1.9+ (CRuby).
81
- #
82
- def mri_19?
83
- !!(mri? and RUBY_VERSION =~ /\A1\.9/)
84
- end
72
+ #
73
+ # @return [Boolean]
74
+ # Returns true when Pry is being run from MRI v1.9+ (CRuby).
75
+ #
76
+ def mri_19?
77
+ !!(mri? and RUBY_VERSION =~ /\A1\.9/)
78
+ end
85
79
 
86
- #
87
- # @return [Boolean]
88
- # Returns true when Pry is being run from MRI v2+ (CRuby).
89
- #
90
- def mri_2?
91
- !!(mri? and RUBY_VERSION =~ /\A2/)
80
+ #
81
+ # @return [Boolean]
82
+ # Returns true when Pry is being run from MRI v2+ (CRuby).
83
+ #
84
+ def mri_2?
85
+ !!(mri? and RUBY_VERSION =~ /\A2/)
86
+ end
92
87
  end
88
+
89
+ # Not supported on MRI 2.2 and lower.
90
+ deprecate_constant(:Platform) if respond_to?(:deprecate_constant)
93
91
  end
@@ -8,7 +8,7 @@ class Pry
8
8
  @name = name
9
9
  end
10
10
 
11
- def method_missing(*args)
11
+ def method_missing(*_args)
12
12
  warn "Warning: The plugin '#{@name}' was not found! (no gem found)"
13
13
  end
14
14
  end
@@ -35,8 +35,12 @@ class Pry
35
35
  # Load the Command line options defined by this plugin (if they exist)
36
36
  def load_cli_options
37
37
  cli_options_file = File.join(spec.full_gem_path, "lib/#{spec.name}/cli.rb")
38
- require cli_options_file if File.exist?(cli_options_file)
38
+ return unless File.exist?(cli_options_file)
39
+
40
+ cli_options_file = File.realpath(cli_options_file) if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.4.4")
41
+ require cli_options_file
39
42
  end
43
+
40
44
  # Activate the plugin (require the gem - enables/loads the
41
45
  # plugin immediately at point of call, even if plugin is
42
46
  # disabled)
@@ -80,6 +84,7 @@ class Pry
80
84
  def locate_plugins
81
85
  gem_list.each do |gem|
82
86
  next if gem.name !~ PRY_PLUGIN_PREFIX
87
+
83
88
  plugin_name = gem.name.split('-', 2).last
84
89
  plugin = Plugin.new(plugin_name, gem.name, gem, false)
85
90
  @plugins << plugin.tap(&:enable!) if plugin.supported? && !plugin_located?(plugin)
@@ -114,5 +119,4 @@ class Pry
114
119
  Gem::Specification.respond_to?(:each) ? Gem::Specification : Gem.source_index.find_name('')
115
120
  end
116
121
  end
117
-
118
122
  end
@@ -1,26 +1,145 @@
1
- class Pry::Prompt
2
- MAP = {
3
- "default" => {
4
- value: Pry::DEFAULT_PROMPT,
5
- description: "The default Pry prompt. Includes information about the\n" \
6
- "current expression number, evaluation context, and nesting\n" \
7
- "level, plus a reminder that you're using Pry."
8
- },
9
-
10
- "simple" => {
11
- value: Pry::SIMPLE_PROMPT,
12
- description: "A simple '>>'."
13
- },
14
-
15
- "nav" => {
16
- value: Pry::NAV_PROMPT,
17
- description: "A prompt that displays the binding stack as a path and\n" \
18
- "includes information about _in_ and _out_."
19
- },
20
-
21
- "none" => {
22
- value: Pry::NO_PROMPT,
23
- description: "Wave goodbye to the Pry prompt."
24
- }
25
- }
1
+ class Pry
2
+ # Prompt represents the Pry prompt, which can be used with Readline-like
3
+ # libraries. It defines a few default prompts (default prompt, simple prompt,
4
+ # etc) and also provides an API to add custom prompts.
5
+ #
6
+ # @example
7
+ # Pry::Prompt.add(
8
+ # :ipython,
9
+ # 'IPython-like prompt', [':', '...:']
10
+ # ) do |_context, _nesting, _pry_, sep|
11
+ # sep == ':' ? "In [#{_pry_.input_ring.count}]: " : ' ...: '
12
+ # end
13
+ #
14
+ # # Produces:
15
+ # # In [3]: def foo
16
+ # # ...: puts 'foo'
17
+ # # ...: end
18
+ # # => :foo
19
+ # # In [4]:
20
+ # @since v0.11.0
21
+ # @api public
22
+ module Prompt
23
+ # @return [String]
24
+ DEFAULT_NAME = 'pry'.freeze
25
+
26
+ # @return [Array<Object>] the list of objects that are known to have a
27
+ # 1-line #inspect output suitable for prompt
28
+ SAFE_CONTEXTS = [String, Numeric, Symbol, nil, true, false].freeze
29
+
30
+ # @deprecated Use {Pry::Prompt.add} instead.
31
+ MAP = {}
32
+ deprecate_constant(:MAP) if respond_to?(:deprecate_constant)
33
+
34
+ # A Hash that holds all prompts. The keys of the Hash are prompt
35
+ # names, the values are Hash instances of the format {:description, :value}.
36
+ @prompts = {}
37
+
38
+ class << self
39
+ # Retrieves a prompt.
40
+ #
41
+ # @example
42
+ # Prompt[:my_prompt][:value]
43
+ #
44
+ # @param [Symbol] prompt_name The name of the prompt you want to access
45
+ # @return [Hash{Symbol=>Object}]
46
+ # @since v0.12.0
47
+ def [](prompt_name)
48
+ all[prompt_name.to_s]
49
+ end
50
+
51
+ # @return [Hash{Symbol=>Hash}] the duplicate of the internal prompts hash
52
+ # @note Use this for read-only operations
53
+ # @since v0.12.0
54
+ def all
55
+ @prompts.dup
56
+ end
57
+
58
+ # Adds a new prompt to the prompt hash.
59
+ #
60
+ # @param [Symbol] prompt_name
61
+ # @param [String] description
62
+ # @param [Array<String>] separators The separators to differentiate
63
+ # between prompt modes (default mode and class/method definition mode).
64
+ # The Array *must* have a size of 2.
65
+ # @yield [context, nesting, _pry_, sep]
66
+ # @yieldparam context [Object] the context where Pry is currently in
67
+ # @yieldparam nesting [Integer] whether the context is nested
68
+ # @yieldparam _pry_ [Pry] the Pry instance
69
+ # @yieldparam separator [String] separator string
70
+ # @return [nil]
71
+ # @raise [ArgumentError] if the size of `separators` is not 2
72
+ # @since v0.12.0
73
+ def add(prompt_name, description = '', separators = %w[> *])
74
+ unless separators.size == 2
75
+ raise ArgumentError, "separators size must be 2, given #{separators.size}"
76
+ end
77
+
78
+ @prompts[prompt_name.to_s] = {
79
+ description: description,
80
+ value: separators.map do |sep|
81
+ proc { |context, nesting, _pry_| yield(context, nesting, _pry_, sep) }
82
+ end
83
+ }
84
+
85
+ nil
86
+ end
87
+
88
+ private
89
+
90
+ def prompt_name(name)
91
+ return name unless name.is_a?(Pry::Config::Lazy)
92
+
93
+ name.call
94
+ end
95
+ end
96
+
97
+ add(:default, <<DESC) do |context, nesting, _pry_, sep|
98
+ The default Pry prompt. Includes information about the current expression
99
+ number, evaluation context, and nesting level, plus a reminder that you're
100
+ using Pry.
101
+ DESC
102
+ format(
103
+ "[%<in_count>s] %<name>s(%<context>s)%<nesting>s%<separator>s ",
104
+ in_count: _pry_.input_ring.count,
105
+ name: prompt_name(_pry_.config.prompt_name),
106
+ context: Pry.view_clip(context),
107
+ nesting: (nesting > 0 ? ":#{nesting}" : ''),
108
+ separator: sep
109
+ )
110
+ end
111
+
112
+ add(:simple, "A simple `>>`.\n", ['>> ', ' | ']) do |_, _, _, sep|
113
+ sep
114
+ end
115
+
116
+ add(:nav, <<DESC, %w[> *]) do |context, nesting, _pry_, sep|
117
+ A prompt that displays the binding stack as a path and includes information
118
+ about #{Helpers::Text.bold('_in_')} and #{Helpers::Text.bold('_out_')}.
119
+ DESC
120
+ tree = _pry_.binding_stack.map { |b| Pry.view_clip(b.eval('self')) }
121
+ format(
122
+ "[%<in_count>s] (%<name>s) %<tree>s: %<stack_size>s%<separator>s ",
123
+ in_count: _pry_.input_ring.count,
124
+ name: prompt_name(_pry_.config.prompt_name),
125
+ tree: tree.join(' / '),
126
+ stack_size: _pry_.binding_stack.size - 1,
127
+ separator: sep
128
+ )
129
+ end
130
+
131
+ add(:shell, <<DESC, %w[$ *]) do |context, nesting, _pry_, sep|
132
+ A prompt that displays `$PWD` as you change it.
133
+ DESC
134
+ format(
135
+ "%<name>s %<context>s:%<pwd>s %<separator>s ",
136
+ name: prompt_name(_pry_.config.prompt_name),
137
+ context: Pry.view_clip(context),
138
+ pwd: Dir.pwd,
139
+ separator: sep
140
+ )
141
+ end
142
+
143
+ add(:none, 'Wave goodbye to the Pry prompt.', Array.new(2)) { '' }
144
+ end
26
145
  end