class Pry::Command::Help

Public Instance Methods

command_groups() click to toggle source

Get a hash of available commands grouped by the “group” name.

# File lib/pry/commands/help.rb, line 29
def command_groups
  visible_commands.values.group_by(&:group)
end
display_command(command) click to toggle source

Display help for an individual command.

@param [Pry::Command] command

# File lib/pry/commands/help.rb, line 125
def display_command(command)
  pry_instance.pager.page command.new.help
end
display_filtered_commands(search) click to toggle source

Display help for a searched item, filtered by group

@param [String] search The string to search for.

# File lib/pry/commands/help.rb, line 111
def display_filtered_commands(search)
  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)
  else
    display_index("'#{search}' commands" => filtered.values)
  end
end
display_filtered_search_results(search) click to toggle source

Display help for a searched item, filtered first by group and if that fails, filtered by command name.

@param [String] search The string to search for.

# File lib/pry/commands/help.rb, line 98
def display_filtered_search_results(search)
  groups = search_hash(search, command_groups)

  if !groups.empty?
    display_index(groups)
  else
    display_filtered_commands(search)
  end
end
display_index(groups) click to toggle source

Display the index view, with headings and short descriptions per command.

@param [Hash<String, Array<Commands>>] groups

# File lib/pry/commands/help.rb, line 44
def display_index(groups)
  help_text = []

  sorted_group_names(groups).each do |group_name|
    commands = sorted_commands(groups[group_name])

    help_text << help_text_for_commands(group_name, commands) if commands.any?
  end

  pry_instance.pager.page help_text.join("\n\n")
end
group_sort_key(group_name) click to toggle source
# File lib/pry/commands/help.rb, line 159
def group_sort_key(group_name)
  [
    %w[
      Help Context Editing Introspection Input_and_output Navigating_pry
      Gems Basic Commands
    ].index(group_name.tr(' ', '_')) || 99, group_name
  ]
end
help_text_for_commands(name, commands) click to toggle source

Given a group name and an array of commands, return the help string for those commands.

@param [String] name The group name. @param [Array<Pry::Command>] commands @return [String] The generated help string.

# File lib/pry/commands/help.rb, line 62
def help_text_for_commands(name, commands)
  "#{bold(name.capitalize)}\n" + commands.map do |command|
    "  #{command.options[:listing].to_s.ljust(18)} " \
    "#{command.description.capitalize}"
  end.join("\n")
end
normalize(key) click to toggle source

Clean search terms to make it easier to search group names

@param [String] key @return [String]

# File lib/pry/commands/help.rb, line 155
def normalize(key)
  key.downcase.gsub(/pry\W+/, '')
end
process() click to toggle source
# File lib/pry/commands/help.rb, line 33
def process
  if args.empty?
    display_index(command_groups)
  else
    display_search(args.first)
  end
end
search_hash(search, hash) click to toggle source

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] search the search term @param [Hash] hash the hash to search

# File lib/pry/commands/help.rb, line 137
def search_hash(search, hash)
  matching = {}

  hash.each_pair do |key, value|
    next unless key.is_a?(String)
    return { key => value } if normalize(key) == normalize(search)
    next unless normalize(key).start_with?(normalize(search))

    matching[key] = value
  end

  matching
end
sorted_commands(commands) click to toggle source

Sort an array of commands by their `listing` name.

@param [Array<Pry::Command>] commands The commands to sort @return [Array<Pry::Command>] commands sorted by listing name.

# File lib/pry/commands/help.rb, line 79
def sorted_commands(commands)
  commands.sort_by { |command| command.options[:listing].to_s }
end
sorted_group_names(groups) click to toggle source

@param [Hash] groups @return [Array<String>] An array of sorted group names.

# File lib/pry/commands/help.rb, line 71
def sorted_group_names(groups)
  groups.keys.sort_by(&method(:group_sort_key))
end
visible_commands() click to toggle source

We only want to show commands that have descriptions, so that the easter eggs don't show up.

# File lib/pry/commands/help.rb, line 20
def visible_commands
  visible = {}
  commands.each do |key, command|
    visible[key] = command if command.description && !command.description.empty?
  end
  visible
end