2011-04-13 12:48:50 +12:00
|
|
|
class Pry
|
2011-04-25 22:58:06 +02:00
|
|
|
module Helpers
|
2011-04-13 12:48:50 +12:00
|
|
|
|
2011-04-25 22:58:06 +02:00
|
|
|
module BaseHelpers
|
2011-08-09 00:35:22 +12:00
|
|
|
|
2011-05-05 00:53:04 +12:00
|
|
|
module_function
|
|
|
|
|
|
|
|
def silence_warnings
|
|
|
|
old_verbose = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
$VERBOSE = old_verbose
|
|
|
|
end
|
|
|
|
end
|
2011-04-18 22:31:39 +01:00
|
|
|
|
2012-11-02 14:26:01 +01:00
|
|
|
# Acts like send but ignores any methods defined below Object or Class in the
|
|
|
|
# inheritance hierarchy.
|
|
|
|
# This is required to introspect methods on objects like Net::HTTP::Get that
|
|
|
|
# have overridden the `method` method.
|
|
|
|
def safe_send(obj, method, *args, &block)
|
|
|
|
(Module === obj ? Module : Object).instance_method(method).bind(obj).call(*args, &block)
|
|
|
|
end
|
|
|
|
public :safe_send
|
|
|
|
|
2012-12-25 20:42:47 +02:00
|
|
|
def find_command(name, set = Pry::Commands)
|
|
|
|
command_match = set.find do |_, command|
|
2012-12-25 21:24:02 +02:00
|
|
|
(listing = command.options[:listing]) == name && listing != nil
|
2012-12-25 20:24:18 +02:00
|
|
|
end
|
|
|
|
command_match.last if command_match
|
2011-05-28 04:11:06 +12:00
|
|
|
end
|
2011-04-18 22:31:39 +01:00
|
|
|
|
2012-02-09 15:55:51 +13:00
|
|
|
def not_a_real_file?(file)
|
2015-01-08 17:03:30 -05:00
|
|
|
file =~ /^(\(.*\))$|^<.*>$/ || file =~ /__unknown__/ || file == "" || file == "-e"
|
2012-02-09 15:55:51 +13:00
|
|
|
end
|
|
|
|
|
2011-04-13 12:48:50 +12:00
|
|
|
def command_dependencies_met?(options)
|
|
|
|
return true if !options[:requires_gem]
|
|
|
|
Array(options[:requires_gem]).all? do |g|
|
2013-01-09 00:24:53 +02:00
|
|
|
Rubygem.installed?(g)
|
2011-04-13 12:48:50 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-09 12:03:24 -07:00
|
|
|
def use_ansi_codes?
|
2012-08-12 00:38:32 -04:00
|
|
|
windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
|
2011-10-09 12:03:24 -07:00
|
|
|
end
|
|
|
|
|
2011-06-11 22:44:30 +12:00
|
|
|
def colorize_code(code)
|
2014-05-01 01:14:33 -07:00
|
|
|
CodeRay.scan(code, :ruby).term
|
2011-06-11 22:44:30 +12:00
|
|
|
end
|
|
|
|
|
2011-04-22 01:12:32 -04:00
|
|
|
def highlight(string, regexp, highlight_color=:bright_yellow)
|
2011-08-16 17:00:18 +12:00
|
|
|
string.gsub(regexp) { |match| "<#{highlight_color}>#{match}</#{highlight_color}>" }
|
2011-04-22 01:12:32 -04:00
|
|
|
end
|
|
|
|
|
2011-04-13 17:16:12 +12:00
|
|
|
# formatting
|
|
|
|
def heading(text)
|
|
|
|
text = "#{text}\n--"
|
2014-05-01 01:14:33 -07:00
|
|
|
"\e[1m#{text}\e[0m"
|
2011-04-13 17:16:12 +12:00
|
|
|
end
|
|
|
|
|
2011-12-26 18:48:59 -06:00
|
|
|
# have fun on the Windows platform.
|
|
|
|
def windows?
|
|
|
|
RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
|
|
|
|
end
|
|
|
|
|
2012-06-23 15:06:32 -07:00
|
|
|
# are we able to use ansi on windows?
|
|
|
|
def windows_ansi?
|
2014-04-28 23:12:56 -07:00
|
|
|
defined?(Win32::Console) || ENV['ANSICON'] || (windows? && mri_2?)
|
2012-06-23 15:06:32 -07:00
|
|
|
end
|
|
|
|
|
2011-08-23 13:41:27 +12:00
|
|
|
def jruby?
|
2011-12-27 23:04:30 +00:00
|
|
|
RbConfig::CONFIG['ruby_install_name'] == 'jruby'
|
2011-09-01 15:47:29 +12:00
|
|
|
end
|
|
|
|
|
2013-02-04 17:57:00 +02:00
|
|
|
def jruby_19?
|
2013-09-06 06:22:34 -07:00
|
|
|
jruby? && RbConfig::CONFIG['ruby_version'] == '1.9'
|
2013-02-04 17:57:00 +02:00
|
|
|
end
|
|
|
|
|
2011-09-01 15:47:29 +12:00
|
|
|
def rbx?
|
2011-12-27 23:04:30 +00:00
|
|
|
RbConfig::CONFIG['ruby_install_name'] == 'rbx'
|
2011-08-23 13:41:27 +12:00
|
|
|
end
|
|
|
|
|
2013-09-06 06:22:34 -07:00
|
|
|
def mri?
|
|
|
|
RbConfig::CONFIG['ruby_install_name'] == 'ruby'
|
|
|
|
end
|
|
|
|
|
2012-04-13 14:55:30 +12:00
|
|
|
def mri_19?
|
2014-04-28 23:12:56 -07:00
|
|
|
mri? && RUBY_VERSION =~ /^1\.9/
|
|
|
|
end
|
|
|
|
|
|
|
|
def mri_2?
|
|
|
|
mri? && RUBY_VERSION =~ /^2/
|
2012-04-13 14:55:30 +12:00
|
|
|
end
|
|
|
|
|
2014-04-29 00:03:15 -07:00
|
|
|
# Send the given text through the best available pager (if Pry.config.pager is
|
2013-11-02 18:40:56 -07:00
|
|
|
# enabled). Infers where to send the output if used as a mixin.
|
2014-04-30 02:08:29 -07:00
|
|
|
# DEPRECATED.
|
2012-02-11 01:53:13 -06:00
|
|
|
def stagger_output(text, out = nil)
|
2017-10-13 23:12:34 +02:00
|
|
|
if defined?(_pry_) && _pry_
|
2015-02-16 20:20:14 +01:00
|
|
|
_pry_.pager.page text
|
|
|
|
else
|
|
|
|
Pry.new.pager.page text
|
|
|
|
end
|
2011-04-13 17:16:12 +12:00
|
|
|
end
|
2011-04-13 12:48:50 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|