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

128 lines
3.9 KiB
Ruby
Raw Normal View History

2012-02-23 02:05:10 -05:00
class Pry
module DefaultCommands
Help = Pry::CommandSet.new do
create_command "help" do |cmd|
description "Show a list of commands. Type `help <foo>` for information about <foo>."
2012-02-23 02:05:10 -05:00
banner <<-BANNER
Usage: help [ COMMAND ]
With no arguments, help lists all the available commands in the current
command-set along with their description.
When given a command name as an argument, shows the help for that command.
BANNER
2012-02-23 03:42:45 -05:00
# We only want to show commands that have descriptions, so that the
# easter eggs don't show up.
def visible_commands
visible = {}
commands.each do |key, command|
visible[key] = command if command.description && !command.description.empty?
end
visible
end
2012-02-23 02:05:10 -05:00
# Get a hash of available commands grouped by the "group" name.
2012-02-23 03:42:45 -05:00
def command_groups
visible_commands.values.group_by(&:group)
2012-02-23 02:05:10 -05:00
end
def process
if args.empty?
2012-02-23 03:42:45 -05:00
display_index(command_groups)
2012-02-23 02:05:10 -05:00
else
2012-02-23 03:42:45 -05:00
display_search(args.first)
2012-02-23 02:05:10 -05:00
end
end
# Display the index view, with headings and short descriptions per command.
#
# @param Hash[String => Array[Commands]]
2012-02-23 03:42:45 -05:00
def display_index(groups)
2012-02-23 02:05:10 -05:00
help_text = []
2012-03-01 19:56:21 -05:00
groups.keys.sort_by(&method(:group_sort_key)).each do |key|
2012-02-23 03:42:45 -05:00
commands = groups[key].sort_by{ |command| command.options[:listing].to_s }
2012-02-23 02:05:10 -05:00
unless commands.empty?
2012-02-23 03:42:45 -05:00
help_text << "#{text.bold(key)}\n" + commands.map do |command|
2012-02-23 02:05:10 -05:00
" #{command.options[:listing].to_s.ljust(18)} #{command.description}"
2012-02-23 03:42:45 -05:00
end.join("\n")
2012-02-23 02:05:10 -05:00
end
end
2012-02-23 03:42:45 -05:00
stagger_output(help_text.join("\n\n"))
2012-02-23 02:05:10 -05:00
end
# Display help for an individual command or group.
#
# @param String The string to search for.
2012-02-23 03:42:45 -05:00
def display_search(search)
2012-02-23 02:05:10 -05:00
if command = command_set.find_command_for_help(search)
2012-02-23 03:42:45 -05:00
display_command(command)
2012-02-23 02:05:10 -05:00
else
2012-02-23 03:42:45 -05:00
groups = search_hash(search, command_groups)
2012-02-23 02:05:10 -05:00
2012-02-23 03:42:45 -05:00
if groups.size > 0
display_index(groups)
return
end
filtered = search_hash(search, visible_commands)
raise CommandError, "No help found for '#{args.first}'" if filtered.empty?
if filtered.size == 1
display_command(filtered.values.first)
2012-02-23 02:05:10 -05:00
else
2012-02-23 03:42:45 -05:00
display_index({"'#{search}' commands" => filtered.values})
2012-02-23 02:05:10 -05:00
end
end
end
2012-02-23 03:42:45 -05:00
# Display help for an individual command.
#
# @param [Pry::Command]
def display_command(command)
stagger_output command.new.help
end
# Find a subset of a hash that matches the user's search term.
#
# If there's an exact match a Hash of one element will be returned,
# otherwise a sub-Hash with every key that matches the search will
# be returned.
#
# @param [String] the search term
# @param [Hash] the hash to search
def search_hash(search, hash)
matching = {}
hash.each_pair do |key, value|
next unless key.is_a?(String)
if normalize(key) == normalize(search)
return {key => value}
elsif normalize(key).start_with?(normalize(search))
matching[key] = value
end
end
matching
end
2012-02-23 02:05:10 -05:00
# Clean search terms to make it easier to search group names
#
# @param String
# @return String
def normalize(key)
key.downcase.gsub(/pry\W+/, '')
end
2012-03-01 19:56:21 -05:00
def group_sort_key(group_name)
[%w(Help Context Editing Introspection Input_and_output Navigating_pry Gems Basic Commands).index(group_name.gsub(' ', '_')) || 99, group_name]
end
2012-02-23 02:05:10 -05:00
end
end
end
end