class Pry::Command::Help

def command_groups

Get a hash of available commands grouped by the "group" name.
def command_groups
  visible_commands.values.group_by(&:group)
end

def display_command(command)

Parameters:
  • command (Pry::Command) --
def display_command(command)
  pry_instance.pager.page command.new.help
end

def display_filtered_commands(search)

Parameters:
  • search (String) -- The string to search for.
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

def display_filtered_search_results(search)

Parameters:
  • search (String) -- The string to search for.
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

def display_index(groups)

Parameters:
  • groups (Hash>) --
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

def display_search(search)

Parameters:
  • search (String) -- The string to search for.
def display_search(search)
  if (command = command_set.find_command_for_help(search))
    display_command(command)
  else
    display_filtered_search_results(search)
  end
end

def group_sort_key(group_name)

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

def help_text_for_commands(name, commands)

Returns:
  • (String) - The generated help string.

Parameters:
  • commands (Array) --
  • name (String) -- The group name.
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

def normalize(key)

Returns:
  • (String) -

Parameters:
  • key (String) --
def normalize(key)
  key.downcase.gsub(/pry\W+/, '')
end

def process

def process
  if args.empty?
    display_index(command_groups)
  else
    display_search(args.first)
  end
end

def search_hash(search, hash)

Parameters:
  • hash (Hash) -- the hash to search
  • search (String) -- the search term
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

def sorted_commands(commands)

Returns:
  • (Array) - commands sorted by listing name.

Parameters:
  • commands (Array) -- The commands to sort
def sorted_commands(commands)
  commands.sort_by { |command| command.options[:listing].to_s }
end

def sorted_group_names(groups)

Returns:
  • (Array) - An array of sorted group names.

Parameters:
  • groups (Hash) --
def sorted_group_names(groups)
  groups.keys.sort_by(&method(:group_sort_key))
end

def visible_commands

easter eggs don't show up.
We only want to show commands that have descriptions, so that the
def visible_commands
  visible = {}
  commands.each do |key, command|
    visible[key] = command if command.description && !command.description.empty?
  end
  visible
end