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

Add completion for gem-open and gem-cd

This commit is contained in:
Conrad Irwin 2012-11-14 01:50:27 -08:00
parent 0968c5ddce
commit b18a52f9a5
4 changed files with 31 additions and 5 deletions

View file

@ -14,5 +14,9 @@ class Pry
Dir.chdir(gem_spec(gem).full_gem_path)
output.puts(Dir.pwd)
end
def complete(str)
gem_complete(str)
end
end
end

View file

@ -12,11 +12,7 @@ class Pry
def process(pattern=nil)
pattern = Regexp.compile(pattern || '')
gems = if Gem::Specification.respond_to?(:each)
Gem::Specification.select{|spec| spec.name =~ pattern }.group_by(&:name)
else
Gem.source_index.gems.values.group_by(&:name).select { |gemname, specs| gemname =~ pattern }
end
gems = gem_list(pattern).group_by(&:name)
gems.each do |gem, specs|
specs.sort! do |a,b|

View file

@ -16,5 +16,9 @@ class Pry
invoke_editor(".", 0, false)
end
end
def complete(str)
gem_complete(str)
end
end
end

View file

@ -276,6 +276,28 @@ class Pry
spec or raise CommandError, "Gem `#{gem}` not found"
end
# List gems matching a pattern
# @param [Regexp] pattern
# @return [Array<Gem::Specification>]
def gem_list(pattern=/.*/)
if Gem::Specification.respond_to?(:each)
Gem::Specification.select{|spec| spec.name =~ pattern }
else
Gem.source_index.gems.values.select{|spec| spec.name =~ pattern }
end
end
# Completion function for gem-cd and gem-open
# @param [String] so_far what the user's typed so far
# @return [Array<String>] completions
def gem_complete(so_far)
if so_far =~ / ([^ ]*)\z/
gem_list(%r{\A#{$2}}).map(&:name)
else
gem_list.map(&:name)
end
end
end
end
end