2011-04-25 16:58:06 -04:00
|
|
|
class Pry
|
|
|
|
module Helpers
|
|
|
|
|
|
|
|
module CommandHelpers
|
2011-10-15 03:03:41 -04:00
|
|
|
include OptionsHelpers
|
2011-04-25 16:58:06 -04:00
|
|
|
|
|
|
|
module_function
|
|
|
|
|
2011-09-07 22:17:41 -04:00
|
|
|
# Open a temp file and yield it to the block, closing it after
|
|
|
|
# @return [String] The path of the temp file
|
2012-01-15 01:06:24 -05:00
|
|
|
def temp_file(ext='.rb')
|
|
|
|
file = Tempfile.new(['pry', ext])
|
2011-09-07 22:17:41 -04:00
|
|
|
yield file
|
|
|
|
ensure
|
2012-01-15 01:06:24 -05:00
|
|
|
file.close(true) if file
|
2011-09-07 22:17:41 -04:00
|
|
|
end
|
|
|
|
|
2012-01-08 01:01:15 -05:00
|
|
|
def render_output(str, opts={})
|
|
|
|
if opts[:flood]
|
|
|
|
output.puts str
|
|
|
|
else
|
|
|
|
stagger_output str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-27 23:32:45 -04:00
|
|
|
def internal_binding?(target)
|
2012-09-23 23:54:35 -04:00
|
|
|
m = target.eval("::Kernel.__method__").to_s
|
2012-07-01 03:29:17 -04:00
|
|
|
# class_eval is here because of http://jira.codehaus.org/browse/JRUBY-6753
|
2012-07-14 08:00:50 -04:00
|
|
|
["__binding__", "__pry__", "class_eval"].include?(m)
|
2012-06-27 23:32:45 -04:00
|
|
|
end
|
|
|
|
|
2011-10-01 19:57:39 -04:00
|
|
|
def get_method_or_raise(name, target, opts={}, omit_help=false)
|
2011-10-15 04:30:20 -04:00
|
|
|
meth = Pry::Method.from_str(name, target, opts)
|
|
|
|
|
|
|
|
if name && !meth
|
2012-07-17 21:55:18 -04:00
|
|
|
command_error("The method '#{name}' could not be found.", omit_help, MethodNotFound)
|
2011-09-25 02:53:08 -04:00
|
|
|
end
|
2011-10-15 04:30:20 -04:00
|
|
|
|
|
|
|
(opts[:super] || 0).times do
|
|
|
|
if meth.super
|
|
|
|
meth = meth.super
|
|
|
|
else
|
2012-07-17 21:55:18 -04:00
|
|
|
command_error("'#{meth.name_with_owner}' has no super method.", omit_help, MethodNotFound)
|
2011-10-15 04:30:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-27 23:32:45 -04:00
|
|
|
if !meth || (!name && internal_binding?(target))
|
2012-07-17 21:55:18 -04:00
|
|
|
command_error("No method name given, and context is not a method.", omit_help, MethodNotFound)
|
2012-06-27 23:32:45 -04:00
|
|
|
end
|
|
|
|
|
2011-10-15 04:30:20 -04:00
|
|
|
set_file_and_dir_locals(meth.source_file)
|
|
|
|
meth
|
2011-09-25 02:53:08 -04:00
|
|
|
end
|
|
|
|
|
2012-01-16 02:45:08 -05:00
|
|
|
def command_error(message, omit_help, klass=CommandError)
|
2011-10-15 04:09:26 -04:00
|
|
|
message += " Type `#{command_name} --help` for help." unless omit_help
|
2012-01-16 02:45:08 -05:00
|
|
|
raise klass, message
|
2011-10-15 04:09:26 -04:00
|
|
|
end
|
|
|
|
|
2011-09-10 15:17:22 -04:00
|
|
|
# Remove any common leading whitespace from every line in `text`.
|
|
|
|
#
|
|
|
|
# This can be used to make a HEREDOC line up with the left margin, without
|
|
|
|
# sacrificing the indentation level of the source code.
|
|
|
|
#
|
|
|
|
# e.g.
|
|
|
|
# opt.banner unindent <<-USAGE
|
|
|
|
# Lorem ipsum dolor sit amet, consectetur adipisicing elit,
|
|
|
|
# sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
|
|
|
# "Ut enim ad minim veniam."
|
|
|
|
# USAGE
|
|
|
|
#
|
2012-06-27 01:30:00 -04:00
|
|
|
# Heavily based on textwrap.dedent from Python, which is:
|
2011-09-10 15:17:22 -04:00
|
|
|
# Copyright (C) 1999-2001 Gregory P. Ward.
|
|
|
|
# Copyright (C) 2002, 2003 Python Software Foundation.
|
|
|
|
# Written by Greg Ward <gward@python.net>
|
|
|
|
#
|
|
|
|
# Licensed under <http://docs.python.org/license.html>
|
|
|
|
# From <http://hg.python.org/cpython/file/6b9f0a6efaeb/Lib/textwrap.py>
|
|
|
|
#
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String] text The text from which to remove indentation
|
|
|
|
# @return [String] The text with indentation stripped.
|
2012-08-18 20:12:34 -04:00
|
|
|
def unindent(text, left_padding = 0)
|
2011-09-10 15:17:22 -04:00
|
|
|
# Empty blank lines
|
|
|
|
text = text.sub(/^[ \t]+$/, '')
|
|
|
|
|
|
|
|
# Find the longest common whitespace to all indented lines
|
2013-02-01 04:24:01 -05:00
|
|
|
# Ignore lines containing just -- or ++ as these seem to be used by
|
|
|
|
# comment authors as delimeters.
|
|
|
|
margin = text.scan(/^[ \t]*(?!--\n|\+\+\n)(?=[^ \t\n])/).inject do |current_margin, next_indent|
|
2011-09-10 15:17:22 -04:00
|
|
|
if next_indent.start_with?(current_margin)
|
|
|
|
current_margin
|
|
|
|
elsif current_margin.start_with?(next_indent)
|
|
|
|
next_indent
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-18 20:12:34 -04:00
|
|
|
text.gsub(/^#{margin}/, ' ' * left_padding)
|
2011-09-10 15:17:22 -04:00
|
|
|
end
|
|
|
|
|
2012-01-23 03:12:00 -05:00
|
|
|
# Restrict a string to the given range of lines (1-indexed)
|
|
|
|
# @param [String] content The string.
|
|
|
|
# @param [Range, Fixnum] lines The line(s) to restrict it to.
|
|
|
|
# @return [String] The resulting string.
|
|
|
|
def restrict_to_lines(content, lines)
|
|
|
|
line_range = one_index_range_or_number(lines)
|
|
|
|
Array(content.lines.to_a[line_range]).join
|
|
|
|
end
|
|
|
|
|
2011-12-28 00:22:20 -05:00
|
|
|
def one_index_number(line_number)
|
|
|
|
if line_number > 0
|
|
|
|
line_number - 1
|
|
|
|
else
|
|
|
|
line_number
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# convert a 1-index range to a 0-indexed one
|
|
|
|
def one_index_range(range)
|
|
|
|
Range.new(one_index_number(range.begin), one_index_number(range.end))
|
|
|
|
end
|
|
|
|
|
|
|
|
def one_index_range_or_number(range_or_number)
|
|
|
|
case range_or_number
|
|
|
|
when Range
|
|
|
|
one_index_range(range_or_number)
|
|
|
|
else
|
|
|
|
one_index_number(range_or_number)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-06 02:59:49 -05:00
|
|
|
def absolute_index_number(line_number, array_length)
|
|
|
|
if line_number >= 0
|
|
|
|
line_number
|
|
|
|
else
|
|
|
|
[array_length + line_number, 0].max
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def absolute_index_range(range_or_number, array_length)
|
|
|
|
case range_or_number
|
|
|
|
when Range
|
|
|
|
a = absolute_index_number(range_or_number.begin, array_length)
|
|
|
|
b = absolute_index_number(range_or_number.end, array_length)
|
|
|
|
else
|
|
|
|
a = b = absolute_index_number(range_or_number, array_length)
|
|
|
|
end
|
|
|
|
|
|
|
|
Range.new(a, b)
|
|
|
|
end
|
2012-11-14 03:35:12 -05:00
|
|
|
end
|
2011-04-25 16:58:06 -04:00
|
|
|
end
|
|
|
|
end
|