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

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
@@ -104,10 +104,8 @@ class Pry
104
104
  # TODO: Fix up the exception handling so we don't need a bare rescue
105
105
  if normal_method?(guess)
106
106
  return guess
107
- elsif guess != guess.super
108
- guess = guess.super
109
107
  else
110
- break
108
+ guess = guess.super
111
109
  end
112
110
  end
113
111
 
@@ -166,7 +164,7 @@ class Pry
166
164
  end
167
165
 
168
166
  def valid_file?(file)
169
- (File.exist?(file) && !File.directory?(file)) || Pry.eval_path == file
167
+ (File.exists?(file) && !File.directory?(file)) || Pry.eval_path == file
170
168
  end
171
169
 
172
170
  def lines_for_file(file)
@@ -23,10 +23,9 @@ class Pry
23
23
  # Methods to delegate to associated `Pry::WrappedModule
24
24
  # instance`.
25
25
  private_delegates = [:lines_for_file, :method_candidates,
26
- :yard_docs?]
26
+ :yard_docs?, :number_of_candidates]
27
27
 
28
- public_delegates = [:wrapped, :module?, :class?, :name, :nonblank_name,
29
- :number_of_candidates]
28
+ public_delegates = [:wrapped, :module?, :class?, :name, :nonblank_name]
30
29
 
31
30
  def_delegators :@wrapper, *(private_delegates + public_delegates)
32
31
  private(*private_delegates)
@@ -112,13 +111,13 @@ class Pry
112
111
  # @return [Array] The source location of the base method used to
113
112
  # calculate the source location of the candidate.
114
113
  def first_method_source_location
115
- @first_method_source_location ||= method_candidates[@rank].first.source_location
114
+ @first_method_source_location ||= adjusted_source_location(method_candidates[@rank].first.source_location)
116
115
  end
117
116
 
118
117
  # @return [Array] The source location of the last method in this
119
118
  # candidate's module definition.
120
119
  def last_method_source_location
121
- @end_method_source_location ||= method_candidates[@rank].last.source_location
120
+ @end_method_source_location ||= adjusted_source_location(method_candidates[@rank].last.source_location)
122
121
  end
123
122
 
124
123
  # Return the number of lines between the start of the class definition
@@ -131,6 +130,16 @@ class Pry
131
130
 
132
131
  end_method_line - line
133
132
  end
133
+
134
+ def adjusted_source_location(sl)
135
+ file, line = sl
136
+
137
+ if file && RbxPath.is_core_path?(file)
138
+ file = RbxPath.convert_path_to_full(file)
139
+ end
140
+
141
+ [file, line]
142
+ end
134
143
  end
135
144
  end
136
145
  end
@@ -1,133 +1,64 @@
1
1
  require 'pry/terminal'
2
-
3
- # A pager is an `IO`-like object that accepts text and either prints it
4
- # immediately, prints it one page at a time, or streams it to an external
5
- # program to print one page at a time.
6
2
  class Pry::Pager
7
- class StopPaging < StandardError
8
- end
9
-
10
- attr_reader :_pry_
11
-
12
- def initialize(_pry_)
13
- @_pry_ = _pry_
14
- end
15
-
16
- # Send the given text through the best available pager (if `Pry.config.pager` is
17
- # enabled).
18
- # If you want to send text through in chunks as you generate it, use `open` to
19
- # get a writable object instead.
20
- # @param [String] text A piece of text to run through a pager.
21
- # @param [IO] output (`$stdout`) An object to send output to.
22
- def page(text)
23
- open do |pager|
24
- pager << text
25
- end
26
- end
27
-
28
- # Yields a pager object (`NullPager`, `SimplePager`, or `SystemPager`). All
29
- # pagers accept output with `#puts`, `#print`, `#write`, and `#<<`.
30
- # @param [IO] output (`$stdout`) An object to send output to.
31
- def open
32
- pager = best_available
33
- yield pager
34
- rescue StopPaging
35
- ensure
36
- pager.close if pager
37
- end
38
-
39
- private
40
-
41
- def enabled?; !!@enabled; end
42
-
43
- def output; @output; end
44
-
45
- # Return an instance of the "best" available pager class -- `SystemPager` if
46
- # possible, `SimplePager` if `SystemPager` isn't available, and `NullPager`
47
- # if the user has disabled paging. All pagers accept output with `#puts`,
48
- # `#print`, `#write`, and `#<<`. You must call `#close` when you're done
49
- # writing output to a pager, and you must rescue `Pry::Pager::StopPaging`.
50
- # These requirements can be avoided by using `.open` instead.
51
- # @param [#<<] output ($stdout) An object to send output to.
52
- def best_available
53
- if !_pry_.config.pager
54
- NullPager.new(_pry_.output)
55
- elsif !SystemPager.available? || Pry::Helpers::BaseHelpers.jruby?
56
- SimplePager.new(_pry_.output)
3
+ # @param [String] text
4
+ # A piece of text to run through a pager.
5
+ # @param [Symbol?] pager
6
+ # `:simple` -- Use the pure ruby pager.
7
+ # `:system` -- Use the system pager (less) or the environment variable
8
+ # $PAGER if set.
9
+ # `nil` -- Infer what pager to use from the environment. What this
10
+ # really means is that JRuby and systems that do not have
11
+ # access to 'less' will run through the pure ruby pager.
12
+ def self.page(text, pager = nil)
13
+ case pager
14
+ when nil
15
+ no_pager = !SystemPager.available?
16
+ if no_pager || Pry::Helpers::BaseHelpers.jruby?
17
+ SimplePager.new(text).page
18
+ else
19
+ SystemPager.new(text).page
20
+ end
21
+ when :simple
22
+ SimplePager.new(text).page
23
+ when :system
24
+ SystemPager.new(text).page
57
25
  else
58
- SystemPager.new(_pry_.output)
26
+ raise "'#{pager}' is not a recognized pager."
59
27
  end
60
28
  end
61
29
 
62
- # `NullPager` is a "pager" that actually just prints all output as it comes
63
- # in. Used when `Pry.config.pager` is false.
64
- class NullPager
65
- def initialize(out)
66
- @out = out
67
- end
68
-
69
- def puts(str)
70
- print "#{str.chomp}\n"
71
- end
72
-
73
- def print(str)
74
- write str
75
- end
76
- alias << print
77
-
78
- def write(str)
79
- @out.write str
80
- end
81
-
82
- def close
83
- end
84
-
85
- private
86
-
87
- def height
88
- @height ||= Pry::Terminal.height!
89
- end
90
-
91
- def width
92
- @width ||= Pry::Terminal.width!
93
- end
30
+ def self.page_size
31
+ @page_size ||= Pry::Terminal.height!
94
32
  end
95
33
 
96
- # `SimplePager` is a straightforward pure-Ruby pager. We use it on JRuby and
97
- # when we can't find a usable external pager.
98
- class SimplePager < NullPager
99
- def initialize(*)
100
- super
101
- @tracker = PageTracker.new(height - 3, width)
102
- end
103
-
104
- def write(str)
105
- str.lines.each do |line|
106
- @out.print line
107
- @tracker.record line
34
+ def initialize(text)
35
+ @text = text
36
+ end
108
37
 
109
- if @tracker.page?
110
- @out.print "\n"
111
- @out.print "\e[0m"
112
- @out.print "<page break> --- Press enter to continue " \
113
- "( q<enter> to break ) --- <page break>\n"
114
- raise StopPaging if Readline.readline("").chomp == "q"
115
- @tracker.reset
38
+ class SimplePager < Pry::Pager
39
+ def page
40
+ # The pager size minus the number of lines used by the simple pager info bar.
41
+ page_size = Pry::Pager.page_size - 3
42
+ text_array = @text.lines.to_a
43
+
44
+ text_array.each_slice(page_size) do |chunk|
45
+ puts chunk.join
46
+ break if chunk.size < page_size
47
+ if text_array.size > page_size
48
+ puts "\n<page break> --- Press enter to continue ( q<enter> to break ) --- <page break>"
49
+ break if $stdin.gets.chomp == "q"
116
50
  end
117
51
  end
118
52
  end
119
53
  end
120
54
 
121
- # `SystemPager` buffers output until we're pretty sure it's at least a page
122
- # long, then invokes an external pager and starts streaming output to it. If
123
- # `#close` is called before then, it just prints out the buffered content.
124
- class SystemPager < NullPager
55
+ class SystemPager < Pry::Pager
125
56
  def self.default_pager
126
57
  pager = ENV["PAGER"] || ""
127
58
 
128
59
  # Default to less, and make sure less is being passed the correct options
129
- if pager.strip.empty? or pager =~ /^less\b/
130
- pager = "less -R -F -X"
60
+ if pager.strip.empty? or pager =~ /^less\s*/
61
+ pager = "less -R -S -F -X"
131
62
  end
132
63
 
133
64
  pager
@@ -137,8 +68,7 @@ class Pry::Pager
137
68
  if @system_pager.nil?
138
69
  @system_pager = begin
139
70
  pager_executable = default_pager.split(' ').first
140
- `which #{pager_executable}`
141
- $?.success?
71
+ `which #{ pager_executable }`
142
72
  rescue
143
73
  false
144
74
  end
@@ -149,88 +79,13 @@ class Pry::Pager
149
79
 
150
80
  def initialize(*)
151
81
  super
152
- @tracker = PageTracker.new(height, width)
153
- @buffer = ""
154
- end
155
-
156
- def write(str)
157
- if invoked_pager?
158
- write_to_pager str
159
- else
160
- @tracker.record str
161
- @buffer << str
162
-
163
- if @tracker.page?
164
- write_to_pager @buffer
165
- end
166
- end
167
- rescue Errno::EPIPE
168
- raise StopPaging
82
+ @pager = SystemPager.default_pager
169
83
  end
170
84
 
171
- def close
172
- if invoked_pager?
173
- pager.close
174
- else
175
- @out.puts @buffer
85
+ def page
86
+ IO.popen(@pager, 'w') do |io|
87
+ io.write @text
176
88
  end
177
89
  end
178
-
179
- private
180
-
181
- def write_to_pager(text)
182
- pager.write @out.decolorize_maybe(text)
183
- end
184
-
185
- def invoked_pager?
186
- @pager
187
- end
188
-
189
- def pager
190
- @pager ||= IO.popen(self.class.default_pager, 'w')
191
- end
192
- end
193
-
194
- # `PageTracker` tracks output to determine whether it's likely to take up a
195
- # whole page. This doesn't need to be super precise, but we can use it for
196
- # `SimplePager` and to avoid invoking the system pager unnecessarily.
197
- #
198
- # One simplifying assumption is that we don't need `#page?` to return `true`
199
- # on the basis of an incomplete line. Long lines should be counted as
200
- # multiple lines, but we don't have to transition from `false` to `true`
201
- # until we see a newline.
202
- class PageTracker
203
- def initialize(rows, cols)
204
- @rows, @cols = rows, cols
205
- reset
206
- end
207
-
208
- def record(str)
209
- str.lines.each do |line|
210
- if line.end_with? "\n"
211
- @row += ((@col + line_length(line) - 1) / @cols) + 1
212
- @col = 0
213
- else
214
- @col += line_length(line)
215
- end
216
- end
217
- end
218
-
219
- def page?
220
- @row >= @rows
221
- end
222
-
223
- def reset
224
- @row = 0
225
- @col = 0
226
- end
227
-
228
- private
229
-
230
- # Approximation of the printable length of a given line, without the
231
- # newline and without ANSI color codes.
232
- def line_length(line)
233
- line.chomp.gsub(/\e\[[\d;]*m/, '').length
234
- end
235
90
  end
236
91
  end
@@ -35,7 +35,7 @@ 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
+ require cli_options_file if File.exists?(cli_options_file)
39
39
  end
40
40
  # Activate the plugin (require the gem - enables/loads the
41
41
  # plugin immediately at point of call, even if plugin is
@@ -43,7 +43,7 @@ class Pry
43
43
  # Does not reload plugin if it's already active.
44
44
  def activate!
45
45
  # Create the configuration object for the plugin.
46
- Pry.config.send("#{gem_name.gsub('-', '_')}=", Pry::Config.from_hash({}))
46
+ Pry.config.send("#{gem_name.gsub('-', '_')}=", OpenStruct.new)
47
47
 
48
48
  begin
49
49
  require gem_name if !active?
@@ -1,57 +1,70 @@
1
+ require 'ostruct'
2
+ require 'forwardable'
1
3
  require 'pry/config'
4
+
2
5
  class Pry
3
6
 
4
- HOME_RC_FILE = ENV["PRYRC"] || "~/.pryrc"
7
+ # The RC Files to load.
8
+ HOME_RC_FILE = "~/.pryrc"
5
9
  LOCAL_RC_FILE = "./.pryrc"
6
10
 
11
+ # @return [Hash] Pry's `Thread.current` hash
12
+ def self.current
13
+ Thread.current[:__pry__] ||= {}
14
+ end
15
+
16
+ # class accessors
7
17
  class << self
8
18
  extend Forwardable
19
+
20
+ # convenience method
21
+ def self.delegate_accessors(delagatee, *names)
22
+ def_delegators delagatee, *names
23
+ def_delegators delagatee, *names.map { |v| "#{v}=" }
24
+ end
25
+
26
+ # Get/Set the Proc that defines extra Readline completions (on top
27
+ # of the ones defined for IRB).
28
+ # @return [Proc] The Proc that defines extra Readline completions (on top
29
+ # @example Add file names to completion list
30
+ # Pry.custom_completions = proc { Dir.entries('.') }
9
31
  attr_accessor :custom_completions
32
+
33
+ # @return [Fixnum] The current input line.
10
34
  attr_accessor :current_line
35
+
36
+ # @return [Array] The Array of evaluated expressions.
11
37
  attr_accessor :line_buffer
38
+
39
+ # @return [String] The __FILE__ for the `eval()`. Should be "(pry)"
40
+ # by default.
12
41
  attr_accessor :eval_path
13
- attr_accessor :cli
14
- attr_accessor :quiet
15
- attr_accessor :last_internal_error
16
- attr_accessor :config
17
- attr_writer :history
18
42
 
19
- def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
43
+ # @return [OpenStruct] Return Pry's config object.
44
+ attr_accessor :config
20
45
 
21
- extend Pry::Config::Convenience
22
- config_shortcut(*Pry::Config.shortcuts)
46
+ # @return [History] Return Pry's line history object.
47
+ attr_accessor :history
23
48
 
24
- def prompt=(value)
25
- config.prompt = value
26
- end
49
+ # @return [Boolean] Whether Pry was activated from the command line.
50
+ attr_accessor :cli
27
51
 
28
- def prompt
29
- config.prompt
30
- end
52
+ # @return [Boolean] Whether Pry sessions are quiet by default.
53
+ attr_accessor :quiet
31
54
 
32
- def history
33
- @history ||= History.new
34
- end
35
- end
55
+ # @return [Exception, nil] The last pry internal error.
56
+ # (a CommandError in most cases)
57
+ attr_accessor :last_internal_error
36
58
 
37
- #
38
- # @return [main]
39
- # returns the special instance of Object, "main".
40
- #
41
- def self.main
42
- @main ||= TOPLEVEL_BINDING.eval "self"
43
- end
59
+ # plugin forwardables
60
+ def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
44
61
 
45
- #
46
- # @return [Pry::Config]
47
- # Returns a value store for an instance of Pry running on the current thread.
48
- #
49
- def self.current
50
- Thread.current[:__pry__] ||= Pry::Config.from_hash({}, nil)
62
+ delegate_accessors :@config, :input, :output, :commands, :prompt, :print, :exception_handler,
63
+ :hooks, :color, :pager, :editor, :memory_size, :extra_sticky_locals
51
64
  end
52
65
 
53
66
  # Load the given file in the context of `Pry.toplevel_binding`
54
- # @param [String] file The unexpanded file path.
67
+ # @param [String] file_name The unexpanded file path.
55
68
  def self.load_file_at_toplevel(file)
56
69
  toplevel_binding.eval(File.read(file), file)
57
70
  rescue RescuableException => e
@@ -62,9 +75,7 @@ class Pry
62
75
  # This method can also be used to reload the files if they have changed.
63
76
  def self.load_rc_files
64
77
  rc_files_to_load.each do |file|
65
- critical_section do
66
- load_file_at_toplevel(file)
67
- end
78
+ load_file_at_toplevel(file)
68
79
  end
69
80
  end
70
81
 
@@ -98,28 +109,12 @@ class Pry
98
109
  trap('INT'){ raise Interrupt }
99
110
  end
100
111
 
101
- def self.load_win32console
102
- begin
103
- require 'win32console'
104
- # The mswin and mingw versions of pry require win32console, so this should
105
- # only fail on jruby (where win32console doesn't work).
106
- # Instead we'll recommend ansicon, which does.
107
- rescue LoadError
108
- warn <<-WARNING if Pry.config.windows_console_warning
109
- For a better Pry experience on Windows, please use ansicon:
110
- https://github.com/adoxa/ansicon
111
- If you use an alternative to ansicon and don't want to see this warning again,
112
- you can add "Pry.config.windows_console_warning = false" to your .pryrc.
113
- WARNING
114
- end
115
- end
116
-
117
112
  # Do basic setup for initial session.
118
113
  # Including: loading .pryrc, loading plugins, loading requires, and
119
114
  # loading history.
120
115
  def self.initial_session_setup
121
- return unless initial_session?
122
- @initial_session = false
116
+
117
+ return if !initial_session?
123
118
 
124
119
  # note these have to be loaded here rather than in pry_instance as
125
120
  # we only want them loaded once per entire Pry lifetime.
@@ -128,7 +123,8 @@ you can add "Pry.config.windows_console_warning = false" to your .pryrc.
128
123
  load_requires if Pry.config.should_load_requires
129
124
  load_history if Pry.config.history.should_load
130
125
  load_traps if Pry.config.should_trap_interrupts
131
- load_win32console if Pry::Helpers::BaseHelpers.windows? && !Pry::Helpers::BaseHelpers.windows_ansi?
126
+
127
+ @initial_session = false
132
128
  end
133
129
 
134
130
  # Start a Pry REPL.
@@ -141,7 +137,6 @@ you can add "Pry.config.windows_console_warning = false" to your .pryrc.
141
137
  # Pry.start(Object.new, :input => MyInput.new)
142
138
  def self.start(target=nil, options={})
143
139
  return if ENV['DISABLE_PRY']
144
- options = options.to_hash
145
140
 
146
141
  if in_critical_section?
147
142
  output.puts "ERROR: Pry started inside Pry."
@@ -150,7 +145,7 @@ you can add "Pry.config.windows_console_warning = false" to your .pryrc.
150
145
  end
151
146
 
152
147
  options[:target] = Pry.binding_for(target || toplevel_binding)
153
- options[:hooks] = Pry::Hooks.from_hash options.delete(:hooks) if options.key?(:hooks)
148
+
154
149
  initial_session_setup
155
150
 
156
151
  # Unless we were given a backtrace, save the current one
@@ -179,40 +174,23 @@ you can add "Pry.config.windows_console_warning = false" to your .pryrc.
179
174
  REPLFileLoader.new(file_name).load
180
175
  end
181
176
 
182
- #
183
177
  # An inspector that clips the output to `max_length` chars.
184
178
  # In case of > `max_length` chars the `#<Object...> notation is used.
185
- #
186
- # @param [Object] obj
187
- # The object to view.
188
- #
189
- # @param [Hash] options
190
- # @option options [Integer] :max_length (60)
191
- # The maximum number of chars before clipping occurs.
192
- #
193
- # @option options [Boolean] :id (false)
194
- # Boolean to indicate whether or not a hex reprsentation of the object ID
195
- # is attached to the return value when the length of inspect is greater than
196
- # value of `:max_length`.
197
- #
198
- # @return [String]
199
- # The string representation of `obj`.
200
- #
201
- def self.view_clip(obj, options = {})
202
- max = options.fetch :max_length, 60
203
- id = options.fetch :id, false
204
- if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max
179
+ # @param obj The object to view.
180
+ # @param max_length The maximum number of chars before clipping occurs.
181
+ # @return [String] The string representation of `obj`.
182
+ def self.view_clip(obj, max_length = 60)
183
+ if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length
205
184
  obj.name.to_s
206
- elsif Pry.main == obj
207
- # special-case to support jruby.
208
- # fixed as of https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039
209
- # we can drop in the future.
185
+ elsif TOPLEVEL_BINDING.eval('self') == obj
186
+ # special case for 'main' object :)
210
187
  obj.to_s
211
- elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max
188
+ elsif [String, Numeric, Symbol, nil, true, false].any? { |v| v === obj } && obj.inspect.length <= max_length
212
189
  obj.inspect
213
190
  else
214
- id == true ? "#<#{obj.class}:0x%x>" % (obj.object_id << 1) : "#<#{obj.class}>"
191
+ "#<#{obj.class}>"#:%x>"# % (obj.object_id << 1)
215
192
  end
193
+
216
194
  rescue RescuableException
217
195
  "unknown"
218
196
  end
@@ -229,7 +207,7 @@ you can add "Pry.config.windows_console_warning = false" to your .pryrc.
229
207
  end
230
208
 
231
209
  # Run a Pry command from outside a session. The commands available are
232
- # those referenced by `Pry.config.commands` (the default command set).
210
+ # those referenced by `Pry.commands` (the default command set).
233
211
  # @param [String] command_string The Pry command (including arguments,
234
212
  # if any).
235
213
  # @param [Hash] options Optional named parameters.
@@ -248,8 +226,8 @@ you can add "Pry.config.windows_console_warning = false" to your .pryrc.
248
226
  options = {
249
227
  :target => TOPLEVEL_BINDING,
250
228
  :show_output => true,
251
- :output => Pry.config.output,
252
- :commands => Pry.config.commands
229
+ :output => Pry.output,
230
+ :commands => Pry.commands
253
231
  }.merge!(options)
254
232
 
255
233
  # :context for compatibility with <= 0.9.11.4
@@ -263,6 +241,7 @@ you can add "Pry.config.windows_console_warning = false" to your .pryrc.
263
241
  def self.default_editor_for_platform
264
242
  return ENV['VISUAL'] if ENV['VISUAL'] and not ENV['VISUAL'].empty?
265
243
  return ENV['EDITOR'] if ENV['EDITOR'] and not ENV['EDITOR'].empty?
244
+
266
245
  if Helpers::BaseHelpers.windows?
267
246
  'notepad'
268
247
  else
@@ -273,22 +252,15 @@ you can add "Pry.config.windows_console_warning = false" to your .pryrc.
273
252
  end
274
253
 
275
254
  def self.auto_resize!
276
- Pry.config.input # by default, load Readline
277
-
278
- if !defined?(Readline) || Pry.config.input != Readline
279
- warn "Sorry, you must be using Readline for Pry.auto_resize! to work."
280
- return
281
- end
282
-
283
- if Readline::VERSION =~ /edit/i
255
+ ver = Readline::VERSION
256
+ if ver[/edit/i]
284
257
  warn <<-EOT
285
- Readline version #{Readline::VERSION} detected - will not auto_resize! correctly.
258
+ Readline version #{ver} detected - will not auto_resize! correctly.
286
259
  For the fix, use GNU Readline instead:
287
260
  https://github.com/guard/guard/wiki/Add-proper-Readline-support-to-Ruby-on-Mac-OS-X
288
261
  EOT
289
262
  return
290
263
  end
291
-
292
264
  trap :WINCH do
293
265
  begin
294
266
  Readline.set_screen_size(*Terminal.size!)
@@ -303,19 +275,136 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
303
275
  end
304
276
  end
305
277
 
278
+ def self.set_config_defaults
279
+ config.input = Readline
280
+ config.output = $stdout
281
+ config.commands = Pry::Commands
282
+ config.prompt_name = DEFAULT_PROMPT_NAME
283
+ config.prompt = DEFAULT_PROMPT
284
+ config.print = DEFAULT_PRINT
285
+ config.exception_handler = DEFAULT_EXCEPTION_HANDLER
286
+ config.exception_whitelist = DEFAULT_EXCEPTION_WHITELIST
287
+ config.default_window_size = 5
288
+ config.hooks = DEFAULT_HOOKS
289
+ config.color = Helpers::BaseHelpers.use_ansi_codes?
290
+ config.pager = true
291
+ config.system = DEFAULT_SYSTEM
292
+ config.editor = default_editor_for_platform
293
+ config.should_load_rc = true
294
+ config.should_load_local_rc = true
295
+ config.should_trap_interrupts = Helpers::BaseHelpers.jruby?
296
+ config.disable_auto_reload = false
297
+ config.command_prefix = ""
298
+ config.auto_indent = Helpers::BaseHelpers.use_ansi_codes?
299
+ config.correct_indent = true
300
+ config.collision_warning = false
301
+ config.output_prefix = "=> "
302
+
303
+ if defined?(Bond) && Readline::VERSION !~ /editline/i
304
+ config.completer = Pry::BondCompleter.start
305
+ else
306
+ config.completer = Pry::InputCompleter.start
307
+ end
308
+
309
+ config.gist ||= OpenStruct.new
310
+ config.gist.inspecter = proc(&:pretty_inspect)
311
+
312
+ config.should_load_plugins = true
313
+
314
+ config.requires ||= []
315
+ config.should_load_requires = true
316
+
317
+ config.history ||= OpenStruct.new
318
+ config.history.should_save = true
319
+ config.history.should_load = true
320
+ config.history.file = File.expand_path("~/.pry_history") rescue nil
321
+
322
+ if config.history.file.nil?
323
+ config.should_load_rc = false
324
+ config.history.should_save = false
325
+ config.history.should_load = false
326
+ end
327
+
328
+ config.control_d_handler = DEFAULT_CONTROL_D_HANDLER
329
+
330
+ config.memory_size = 100
331
+
332
+ config.extra_sticky_locals = {}
333
+
334
+ config.ls ||= OpenStruct.new({
335
+ :heading_color => :bright_blue,
336
+
337
+ :public_method_color => :default,
338
+ :private_method_color => :blue,
339
+ :protected_method_color => :blue,
340
+ :method_missing_color => :bright_red,
341
+
342
+ :local_var_color => :yellow,
343
+ :pry_var_color => :default, # e.g. _, _pry_, _file_
344
+
345
+ :instance_var_color => :blue, # e.g. @foo
346
+ :class_var_color => :bright_blue, # e.g. @@foo
347
+
348
+ :global_var_color => :default, # e.g. $CODERAY_DEBUG, $eventmachine_library
349
+ :builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID
350
+ :pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO
351
+
352
+ :constant_color => :default, # e.g. VERSION, ARGF
353
+ :class_constant_color => :blue, # e.g. Object, Kernel
354
+ :exception_constant_color => :magenta, # e.g. Exception, RuntimeError
355
+ :unloaded_constant_color => :yellow, # Any constant that is still in .autoload? state
356
+
357
+ # What should separate items listed by ls? (TODO: we should allow a columnar layout)
358
+ :separator => " ",
359
+
360
+ # Any methods defined on these classes, or modules included into these classes, will not
361
+ # be shown by ls unless the -v flag is used.
362
+ # A user of Rails may wih to add ActiveRecord::Base to the list.
363
+ # add the following to your .pryrc:
364
+ # Pry.config.ls.ceiling << ActiveRecord::Base if defined? ActiveRecordBase
365
+ :ceiling => [Object, Module, Class]
366
+ })
367
+ end
368
+
306
369
  # Set all the configurable options back to their default values
307
370
  def self.reset_defaults
371
+ set_config_defaults
372
+
308
373
  @initial_session = true
309
- self.config = Pry::Config.new Pry::Config::Default.new
374
+
375
+ self.custom_completions = DEFAULT_CUSTOM_COMPLETIONS
310
376
  self.cli = false
311
377
  self.current_line = 1
312
378
  self.line_buffer = [""]
313
379
  self.eval_path = "(pry)"
380
+
381
+ fix_coderay_colors
382
+ end
383
+
384
+ # To avoid mass-confusion, we change the default colour of "white" to
385
+ # "blue" enabling global legibility
386
+ def self.fix_coderay_colors
387
+ to_fix = if (CodeRay::Encoders::Terminal::TOKEN_COLORS rescue nil)
388
+ # CodeRay 1.0.0
389
+ CodeRay::Encoders::Terminal::TOKEN_COLORS
390
+ else
391
+ # CodeRay 0.9
392
+ begin
393
+ require 'coderay/encoders/term'
394
+ CodeRay::Encoders::Term::TOKEN_COLORS
395
+ rescue
396
+ end
397
+ end
398
+
399
+ to_fix[:comment] = "0;34" if to_fix
314
400
  end
315
401
 
316
402
  # Basic initialization.
317
403
  def self.init
318
404
  @plugin_manager ||= PluginManager.new
405
+ self.config ||= Config.new
406
+ self.history ||= History.new
407
+
319
408
  reset_defaults
320
409
  locate_plugins
321
410
  end
@@ -329,7 +418,7 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
329
418
  if Binding === target
330
419
  target
331
420
  else
332
- if Pry.main == target
421
+ if TOPLEVEL_BINDING.eval('self') == target
333
422
  TOPLEVEL_BINDING
334
423
  else
335
424
  target.__binding__
@@ -359,16 +448,16 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
359
448
  end
360
449
 
361
450
  def self.in_critical_section?
362
- Thread.current[:pry_critical_section] ||= 0
363
- Thread.current[:pry_critical_section] > 0
451
+ @critical_section ||= 0
452
+ @critical_section > 0
364
453
  end
365
454
 
366
455
  def self.critical_section(&block)
367
- Thread.current[:pry_critical_section] ||= 0
368
- Thread.current[:pry_critical_section] += 1
456
+ @critical_section ||= 0
457
+ @critical_section += 1
369
458
  yield
370
459
  ensure
371
- Thread.current[:pry_critical_section] -= 1
460
+ @critical_section -= 1
372
461
  end
373
462
  end
374
463