1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
class Pry
HOME_RC_FILE =
if ENV.key?('PRYRC')
ENV['PRYRC']
elsif File.exist?(File.expand_path('~/.pryrc'))
'~/.pryrc'
elsif ENV.key?('XDG_CONFIG_HOME') && ENV['XDG_CONFIG_HOME'] != ''
# See XDG Base Directory Specification at
# https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
ENV['XDG_CONFIG_HOME'] + '/pry/pryrc'
else
'~/.config/pry/pryrc'
end
LOCAL_RC_FILE = "./.pryrc".freeze
class << self
extend Pry::Forwardable
attr_accessor :custom_completions
attr_accessor :current_line
attr_accessor :line_buffer
attr_accessor :eval_path
attr_accessor :cli
attr_accessor :quiet
attr_accessor :last_internal_error
attr_accessor :config
attr_writer :history
def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
extend Pry::Config::Convenience
config_shortcut(*Pry::Config.shortcuts)
def prompt=(value)
config.prompt = value
end
def prompt
config.prompt
end
def history
return @history if @history
@history =
if defined?(Pry.config.input::HISTORY)
History.new(history: Pry.config.input::HISTORY)
else
History.new
end
end
#
# @example
# Pry.configure do |config|
# config.eager_load! # optional
# config.input = # ..
# config.foo = 2
# end
#
# @yield [config]
# Yields a block with {Pry.config} as its argument.
#
def configure
yield config
end
end
#
# @return [main]
# returns the special instance of Object, "main".
#
def self.main
@main ||= TOPLEVEL_BINDING.eval "self"
end
#
# @return [Pry::Config]
# Returns a value store for an instance of Pry running on the current thread.
#
def self.current
Thread.current[:__pry__] ||= Pry::Config.from_hash({})
end
# Load the given file in the context of `Pry.toplevel_binding`
# @param [String] file The unexpanded file path.
def self.load_file_at_toplevel(file)
toplevel_binding.eval(File.read(file), file)
rescue RescuableException => e
puts "Error loading #{file}: #{e}\n#{e.backtrace.first}"
end
# Load HOME_RC_FILE and LOCAL_RC_FILE if appropriate
# This method can also be used to reload the files if they have changed.
def self.load_rc_files
rc_files_to_load.each do |file|
critical_section do
load_file_at_toplevel(file)
end
end
end
# Load the local RC file (./.pryrc)
def self.rc_files_to_load
files = []
files << HOME_RC_FILE if Pry.config.should_load_rc
files << LOCAL_RC_FILE if Pry.config.should_load_local_rc
files.map { |file| real_path_to(file) }.compact.uniq
end
# Expand a file to its canonical name (following symlinks as appropriate)
def self.real_path_to(file)
Pathname.new(File.expand_path(file)).realpath.to_s
rescue Errno::ENOENT, Errno::EACCES
nil
end
# Load any Ruby files specified with the -r flag on the command line.
def self.load_requires
Pry.config.requires.each do |file|
require file
end
end
# Trap interrupts on jruby, and make them behave like MRI so we can
# catch them.
def self.load_traps
trap('INT') { raise Interrupt }
end
def self.load_win32console
require 'win32console'
# The mswin and mingw versions of pry require win32console, so this should
# only fail on jruby (where win32console doesn't work).
# Instead we'll recommend ansicon, which does.
rescue LoadError
warn <<-WARNING if Pry.config.windows_console_warning
For a better Pry experience on Windows, please use ansicon:
https://github.com/adoxa/ansicon
If you use an alternative to ansicon and don't want to see this warning again,
you can add "Pry.config.windows_console_warning = false" to your pryrc.
WARNING
end
# Do basic setup for initial session including: loading pryrc, plugins,
# requires, and history.
def self.initial_session_setup
return unless initial_session?
@initial_session = false
# note these have to be loaded here rather than in pry_instance as
# we only want them loaded once per entire Pry lifetime.
load_rc_files
end
def self.final_session_setup
return if @session_finalized
@session_finalized = true
load_plugins if Pry.config.should_load_plugins
load_requires if Pry.config.should_load_requires
load_history if Pry.config.history.should_load
load_traps if Pry.config.should_trap_interrupts
load_win32console if Helpers::Platform.windows? && !Helpers::Platform.windows_ansi?
end
# Start a Pry REPL.
# This method also loads `pryrc` as necessary the first time it is invoked.
# @param [Object, Binding] target The receiver of the Pry session
# @param [Hash] options
# @option options (see Pry#initialize)
# @example
# Pry.start(Object.new, :input => MyInput.new)
def self.start(target = nil, options = {})
return if ENV['DISABLE_PRY']
raise 'You have FAIL_PRY set to true, which results in Pry calls failing' if ENV['FAIL_PRY']
options = options.to_hash
if in_critical_section?
output.puts "ERROR: Pry started inside Pry."
output.puts "This can happen if you have a binding.pry inside a #to_s or #inspect function."
return
end
options[:target] = Pry.binding_for(target || toplevel_binding)
initial_session_setup
final_session_setup
# Unless we were given a backtrace, save the current one
if options[:backtrace].nil?
options[:backtrace] = caller
# If Pry was started via `binding.pry`, elide that from the backtrace
options[:backtrace].shift if options[:backtrace].first =~ /pry.*core_extensions.*pry/
end
driver = options[:driver] || Pry::REPL
# Enter the matrix
driver.start(options)
rescue Pry::TooSafeException
puts "ERROR: Pry cannot work with $SAFE > 0"
raise
end
# Execute the file through the REPL loop, non-interactively.
# @param [String] file_name File name to load through the REPL.
def self.load_file_through_repl(file_name)
require "pry/repl_file_loader"
REPLFileLoader.new(file_name).load
end
#
# An inspector that clips the output to `max_length` chars.
# In case of > `max_length` chars the `#<Object...> notation is used.
#
# @param [Object] obj
# The object to view.
#
# @param [Hash] options
# @option options [Integer] :max_length (60)
# The maximum number of chars before clipping occurs.
#
# @option options [Boolean] :id (false)
# Boolean to indicate whether or not a hex reprsentation of the object ID
# is attached to the return value when the length of inspect is greater than
# value of `:max_length`.
#
# @return [String]
# The string representation of `obj`.
#
def self.view_clip(obj, options = {})
max = options.fetch :max_length, 60
id = options.fetch :id, false
if obj.is_a?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max
obj.name.to_s
elsif Pry.main == obj
# special-case to support jruby.
# fixed as of https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039
# we can drop in the future.
obj.to_s
elsif Pry.config.prompt_safe_contexts.any? { |v| v === obj } && obj.inspect.length <= max
obj.inspect
else
id ? format("#<#{obj.class}:0x%x>", (obj.object_id << 1)) : "#<#{obj.class}>"
end
rescue RescuableException
"unknown"
end
# Load Readline history if required.
def self.load_history
Pry.history.load
end
# @return [Boolean] Whether this is the first time a Pry session has
# been started since loading the Pry class.
def self.initial_session?
@initial_session
end
# Run a Pry command from outside a session. The commands available are
# those referenced by `Pry.config.commands` (the default command set).
# @param [String] command_string The Pry command (including arguments,
# if any).
# @param [Hash] options Optional named parameters.
# @return [nil]
# @option options [Object, Binding] :target The object to run the
# command under. Defaults to `TOPLEVEL_BINDING` (main).
# @option options [Boolean] :show_output Whether to show command
# output. Defaults to true.
# @example Run at top-level with no output.
# Pry.run_command "ls"
# @example Run under Pry class, returning only public methods.
# Pry.run_command "ls -m", :target => Pry
# @example Display command output.
# Pry.run_command "ls -av", :show_output => true
def self.run_command(command_string, options = {})
options = {
target: TOPLEVEL_BINDING,
show_output: true,
output: Pry.config.output,
commands: Pry.config.commands
}.merge!(options)
# :context for compatibility with <= 0.9.11.4
target = options[:context] || options[:target]
output = options[:show_output] ? options[:output] : StringIO.new
pry = Pry.new(output: output, target: target, commands: options[:commands])
pry.eval command_string
nil
end
def self.default_editor_for_platform
return ENV['VISUAL'] if ENV['VISUAL'] && !ENV['VISUAL'].empty?
return ENV['EDITOR'] if ENV['EDITOR'] && !ENV['EDITOR'].empty?
return 'notepad' if Helpers::Platform.windows?
%w[editor nano vi].detect do |editor|
system("which #{editor} > /dev/null 2>&1")
end
end
def self.auto_resize!
Pry.config.input # by default, load Readline
if !defined?(Readline) || Pry.config.input != Readline
warn "Sorry, you must be using Readline for Pry.auto_resize! to work."
return
end
if Readline::VERSION =~ /edit/i
warn <<-EOT
Readline version #{Readline::VERSION} detected - will not auto_resize! correctly.
For the fix, use GNU Readline instead:
https://github.com/guard/guard/wiki/Add-Readline-support-to-Ruby-on-Mac-OS-X
EOT
return
end
trap :WINCH do
begin
Readline.set_screen_size(*Terminal.size!)
rescue StandardError => e
warn "\nPry.auto_resize!'s Readline.set_screen_size failed: #{e}"
end
begin
Readline.refresh_line
rescue StandardError => e
warn "\nPry.auto_resize!'s Readline.refresh_line failed: #{e}"
end
end
end
# Set all the configurable options back to their default values
def self.reset_defaults
@initial_session = true
@session_finalized = nil
self.config = Pry::Config.new Pry::Config.defaults
self.cli = false
self.current_line = 1
self.line_buffer = [""]
self.eval_path = "(pry)"
end
# Basic initialization.
def self.init
@plugin_manager ||= PluginManager.new
reset_defaults
locate_plugins
end
# Return a `Binding` object for `target` or return `target` if it is
# already a `Binding`.
# In the case where `target` is top-level then return `TOPLEVEL_BINDING`
# @param [Object] target The object to get a `Binding` object for.
# @return [Binding] The `Binding` object.
def self.binding_for(target)
if Binding === target
target
else
if Pry.main == target
TOPLEVEL_BINDING
else
target.__binding__
end
end
end
def self.toplevel_binding
unless defined?(@toplevel_binding) && @toplevel_binding
# Grab a copy of the TOPLEVEL_BINDING without any local variables.
# This binding has a default definee of Object, and new methods are
# private (just as in TOPLEVEL_BINDING).
TOPLEVEL_BINDING.eval <<-RUBY
def self.__pry__
binding
end
Pry.toplevel_binding = __pry__
class << self; undef __pry__; end
RUBY
end
@toplevel_binding.eval('private')
@toplevel_binding
end
def self.toplevel_binding=(binding)
@toplevel_binding = binding
end
def self.in_critical_section?
Thread.current[:pry_critical_section] ||= 0
Thread.current[:pry_critical_section] > 0
end
def self.critical_section
Thread.current[:pry_critical_section] ||= 0
Thread.current[:pry_critical_section] += 1
yield
ensure
Thread.current[:pry_critical_section] -= 1
end
# Wraps a block in a named block called `Pry::Config::Lazy`. This is used for
# dynamic config values, which are calculated every time
# {Pry::Config::Lazy#call} is called.
#
# @example
# # pryrc
# Pry.config.prompt_name = Pry.lazy { rand(100) }
#
# # Session
# [1] 96(main)>
# [2] 19(main)>
# [3] 80(main)>
#
# @return [#call]
def self.lazy(&block)
Pry::Config::Lazy.new(&block)
end
end
Pry.init
|