Class: Pry::Command::Help

Inherits:
Pry::ClassCommand show all
Defined in:
lib/pry/commands/help.rb

Constant Summary

Constants inherited from Pry::Command

VOID_VALUE

Constants included from Helpers::DocumentationHelpers

Helpers::DocumentationHelpers::YARD_TAGS

Constants included from Helpers::Text

Helpers::Text::COLORS

Instance Attribute Summary

Attributes inherited from Pry::ClassCommand

#args, #opts

Attributes inherited from Pry::Command

#arg_string, #captures, #command_block, #command_set, #context, #eval_string, #hooks, #output, #pry_instance, #target

Instance Method Summary collapse

Methods inherited from Pry::ClassCommand

#call, #complete, doc, #help, inherited, #options, #setup, #slop, source, source_file, source_line, source_location, source_object, #subcommands

Methods inherited from Pry::Command

#_pry_, #after_hooks, banner, #before_hooks, #block, #call_safely, #call_with_hooks, #check_for_command_collision, command_name, #command_name, #command_options, command_regex, #commands, #complete, convert_to_regex, default_options, #description, doc, #find_hooks, group, #initialize, inspect, #interpolate_string, #match, match_score, matches?, name, #name, #normalize_method_args, options, #pass_block, #process_line, #run, #source, source, source_file, source_line, #state, state, subclass, #target_self, #tokenize, #use_unpatched_symbol, #void

Methods included from Pry::CodeObject::Helpers

#c_method?, #c_module?, #command?, #module_with_yard_docs?, #real_method_object?

Methods included from Helpers::DocumentationHelpers

#get_comment_content, get_comment_content, process_comment_markup, #process_comment_markup, #process_rdoc, process_rdoc, #process_yardoc, process_yardoc, process_yardoc_tag, #process_yardoc_tag, #strip_comments_from_c_code, strip_comments_from_c_code, #strip_leading_whitespace, strip_leading_whitespace

Methods included from Helpers::Text

#bold, #default, #indent, #no_color, #no_pager, #strip_color, #with_line_numbers

Methods included from Helpers::CommandHelpers

#absolute_index_number, #absolute_index_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #unindent

Methods included from Helpers::OptionsHelpers

method_object, #method_object, method_options, #method_options

Methods included from Helpers::BaseHelpers

#colorize_code, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Constructor Details

This class inherits a constructor from Pry::Command

Instance Method Details

#command_groupsObject

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


29
30
31
# File 'lib/pry/commands/help.rb', line 29

def command_groups
  visible_commands.values.group_by(&:group)
end

#display_command(command) ⇒ Object

Display help for an individual command.

Parameters:


125
126
127
# File 'lib/pry/commands/help.rb', line 125

def display_command(command)
  pry_instance.pager.page command.new.help
end

#display_filtered_commands(search) ⇒ Object

Display help for a searched item, filtered by group

Parameters:

  • search (String)

    The string to search for.

Raises:


111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object

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

Parameters:

  • search (String)

    The string to search for.


98
99
100
101
102
103
104
105
106
# 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) ⇒ Object

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

Parameters:

  • groups (Hash<String, Array<Commands>>)

44
45
46
47
48
49
50
51
52
53
54
# 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

#display_search(search) ⇒ Object

Display help for an individual command or group.

Parameters:

  • search (String)

    The string to search for.


86
87
88
89
90
91
92
# File 'lib/pry/commands/help.rb', line 86

def display_search(search)
  if (command = command_set.find_command_for_help(search))
    display_command(command)
  else
    display_filtered_search_results(search)
  end
end

#group_sort_key(group_name) ⇒ Object


159
160
161
162
163
164
165
166
# 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) ⇒ String

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

Parameters:

  • name (String)

    The group name.

  • commands (Array<Pry::Command>)

Returns:

  • (String)

    The generated help string.


62
63
64
65
66
67
# 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) ⇒ String

Clean search terms to make it easier to search group names

Parameters:

  • key (String)

Returns:

  • (String)

155
156
157
# File 'lib/pry/commands/help.rb', line 155

def normalize(key)
  key.downcase.gsub(/pry\W+/, '')
end

#processObject


33
34
35
36
37
38
39
# 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) ⇒ Object

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.

Parameters:

  • search (String)

    the search term

  • hash (Hash)

    the hash to search


137
138
139
140
141
142
143
144
145
146
147
148
149
# 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) ⇒ Array<Pry::Command>

Sort an array of commands by their listing name.

Parameters:

Returns:

  • (Array<Pry::Command>)

    commands sorted by listing name.


79
80
81
# 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) ⇒ Array<String>

Returns An array of sorted group names.

Parameters:

  • groups (Hash)

Returns:

  • (Array<String>)

    An array of sorted group names.


71
72
73
# File 'lib/pry/commands/help.rb', line 71

def sorted_group_names(groups)
  groups.keys.sort_by(&method(:group_sort_key))
end

#visible_commandsObject

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


20
21
22
23
24
25
26
# 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