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

Instance Attribute Summary

Attributes inherited from Pry::ClassCommand

#args, #opts

Attributes inherited from Pry::Command

#_pry_, #arg_string, #captures, #command_block, #command_set, #context, #eval_string, #output, #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, #subcommands

Methods inherited from Pry::Command

banner, #block, #call_safely, #check_for_command_collision, #command_name, command_name, #command_options, command_regex, #commands, #complete, convert_to_regex, default_options, #dependencies_met?, #description, doc, group, hooks, #initialize, inspect, #interpolate_string, #match, match_score, matches?, #name, name, options, #process_line, #run, #source, source, source_file, source_line, #source_location, source_location, #state, subclass, #target_self, #text, #tokenize, #void

Methods included from Pry::CodeObject::Helpers

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

Methods included from Helpers::DocumentationHelpers

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

Methods included from Helpers::CommandHelpers

absolute_index_number, absolute_index_range, command_error, 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_options

Methods included from Helpers::BaseHelpers

colorize_code, command_dependencies_met?, find_command, heading, highlight, jruby?, jruby_19?, mri?, mri_19?, mri_20?, mri_21?, mri_2?, not_a_real_file?, rbx?, #safe_send, safe_send, silence_warnings, stagger_output, use_ansi_codes?, windows?, windows_ansi?

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.


26
27
28
# File 'lib/pry/commands/help.rb', line 26

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

#display_command(command) ⇒ Object

Display help for an individual command.

Parameters:


123
124
125
# File 'lib/pry/commands/help.rb', line 123

def display_command(command)
  _pry_.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:


109
110
111
112
113
114
115
116
117
118
# File 'lib/pry/commands/help.rb', line 109

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.


96
97
98
99
100
101
102
103
104
# File 'lib/pry/commands/help.rb', line 96

def display_filtered_search_results(search)
  groups = search_hash(search, command_groups)

  if groups.size > 0
    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>>)

41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pry/commands/help.rb', line 41

def display_index(groups)
  help_text = []

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

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

  _pry_.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.


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

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


158
159
160
# File 'lib/pry/commands/help.rb', line 158

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

#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.


61
62
63
64
65
# File 'lib/pry/commands/help.rb', line 61

def help_text_for_commands(name, commands)
  "#{text.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)

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

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

#processObject


30
31
32
33
34
35
36
# File 'lib/pry/commands/help.rb', line 30

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


135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/pry/commands/help.rb', line 135

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

#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.


77
78
79
# File 'lib/pry/commands/help.rb', line 77

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.


69
70
71
# File 'lib/pry/commands/help.rb', line 69

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.


17
18
19
20
21
22
23
# File 'lib/pry/commands/help.rb', line 17

def visible_commands
  visible = {}
  commands.each do |key, command|
    visible[key] = command if command.description && !command.description.empty?
  end
  visible
end