1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

find-method no longer hardcodes ansi (using pry helpers now)

This commit is contained in:
John Mair 2012-04-03 02:22:06 +12:00
parent 578c7d3ebf
commit 0fd7b48e6d

View file

@ -1,19 +1,17 @@
class Pry
module DefaultCommands
FindMethod = Pry::CommandSet.new do
create_command "find-method" do
create_command "find-method" do
group "Context"
description "Recursively search for a method within a Class/Module or the current namespace. find-method [-n | -c] METHOD [NAMESPACE]"
def options(opti)
opti.on :n, :name, "Search for a method by name"
opti.on :c, :content, "Search for a method based on content in Regex form"
end
def process
return if args.size < 1
pattern = ::Regexp.new args[0]
@ -22,8 +20,8 @@ class Pry
if !klass.is_a?(Module)
klass = klass.class
end
else
klass = eval target_self.pretty_inspect
else
klass = (target_self.is_a?(Module)) ? target_self : target_self.class
end
if opts.name?
to_put = name_search(pattern, klass)
@ -34,27 +32,29 @@ class Pry
end
1
if to_put.flatten == []
puts "\e[32;1mNo Methods Matched\e[0m"
puts text.bold("No Methods Matched")
else
puts "\e[1mMethods Matched\e[0m"
puts text.bold("Methods Matched")
puts "--"
puts to_put
end
end
private
def puts(item)
output.puts item
end
def content_search(pattern, klass, current=[])
def content_search(pattern, klass, current=[], the_methods=[])
return unless(klass.is_a? Module)
return if current.include? klass
current << klass
meths = []
(Pry::Method.all_from_class(klass) + Pry::Method.all_from_obj(klass)).uniq.each do |meth|
next if the_methods.include? meth.name
the_methods << meth.name
begin
if meth.source =~ pattern && !meth.alias?
header = "#{klass}##{meth.name}: "
@ -67,40 +67,41 @@ class Pry
end
end
klass.constants.each do |klazz|
meths += ((res = content_search(pattern, klass.const_get(klazz), current)) ? res : [])
meths += ((res = content_search(pattern, klass.const_get(klazz), current, the_methods)) ? res : [])
end
return meths.uniq.flatten
end
def name_search(regex, klass, current=[])
def name_search(regex, klass, current=[], the_methods=[])
return unless(klass.is_a? Module)
return if current.include? klass
current << klass
header = "\e[1;34;4m#{klass.name}\e[0;24m:"
header = text.bold("#{klass.name}:")
meths = []
(Pry::Method.all_from_class(klass) + Pry::Method.all_from_obj(klass)).uniq.each do |x|
next if the_methods.include? x.name
the_methods << x.name
if x.name =~ regex
meths << " #{x.name}"
meths << " #{x.name}"
begin
if x.alias?
meths[-1] += "#A|#{x.original_name}" if x.original_name
meths[-1] += "#A|#{x.original_name}" if x.original_name
end
rescue Exception
end
end
end
max = meths.map(&:length).max
meths.map! do |x|
if x =~ /#{"#A"}/
x = x.sub!("#A|", ((' ' * ((max - x.length) + 3)) + "\e[33m(Alias of ")) + ")\e[0m"
x = x.sub!("#A|", ((' ' * ((max - x.length) + 3)) + text.bold("(Alias of "))) + text.bold(")")
end
x
end
meths.unshift header if meths.size > 0
klass.constants.each do |x|
begin
meths << ((res = name_search(regex, klass.const_get(x), current)) ? res : [])
meths << ((res = name_search(regex, klass.const_get(x), current, the_methods)) ? res : [])
rescue Exception
next
end
@ -111,4 +112,3 @@ class Pry
end
end
end