Class: Pry::Command::Help
- Inherits:
-
Pry::ClassCommand
- Object
- Pry::Command
- Pry::ClassCommand
- Pry::Command::Help
- Defined in:
- lib/pry/commands/help.rb
Constant Summary
Constants inherited from Pry::Command
Constants included from Helpers::DocumentationHelpers
Helpers::DocumentationHelpers::YARD_TAGS
Constants included from Helpers::Text
Instance Attribute Summary
Attributes inherited from Pry::ClassCommand
Attributes inherited from Pry::Command
#arg_string, #captures, #command_block, #command_set, #context, #eval_string, #hooks, #output, #pry_instance, #target
Instance Method Summary collapse
-
#command_groups ⇒ Object
Get a hash of available commands grouped by the “group” name.
-
#display_command(command) ⇒ Object
Display help for an individual command.
-
#display_filtered_commands(search) ⇒ Object
Display help for a searched item, filtered by group.
-
#display_filtered_search_results(search) ⇒ Object
Display help for a searched item, filtered first by group and if that fails, filtered by command name.
-
#display_index(groups) ⇒ Object
Display the index view, with headings and short descriptions per command.
-
#display_search(search) ⇒ Object
Display help for an individual command or group.
- #group_sort_key(group_name) ⇒ Object
-
#help_text_for_commands(name, commands) ⇒ String
Given a group name and an array of commands, return the help string for those commands.
-
#normalize(key) ⇒ String
Clean search terms to make it easier to search group names.
- #process ⇒ Object
-
#search_hash(search, hash) ⇒ Object
Find a subset of a hash that matches the user’s search term.
-
#sorted_commands(commands) ⇒ Array<Pry::Command>
Sort an array of commands by their ‘listing` name.
-
#sorted_group_names(groups) ⇒ Array<String>
An array of sorted group names.
-
#visible_commands ⇒ Object
We only want to show commands that have descriptions, so that the easter eggs don’t show up.
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
#_pry_, banner, #block, #check_for_command_collision, command_name, #command_name, #command_options, command_regex, #commands, #complete, convert_to_regex, default_options, #description, doc, group, #initialize, inspect, #interpolate_string, #match, match_score, matches?, name, #name, options, #process_line, #run, source, #source, source_file, source_line, state, #state, subclass, #target_self, #tokenize, #void
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 Pry::CodeObject::Helpers
#c_method?, #c_module?, #command?, #module_with_yard_docs?, #real_method_object?
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
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_groups ⇒ Object
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.
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
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.
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.
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.
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.
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.[: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
155 156 157 |
# File 'lib/pry/commands/help.rb', line 155 def normalize(key) key.downcase.gsub(/pry\W+/, '') end |
#process ⇒ Object
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.
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.
79 80 81 |
# File 'lib/pry/commands/help.rb', line 79 def sorted_commands(commands) commands.sort_by { |command| command.[:listing].to_s } end |
#sorted_group_names(groups) ⇒ Array<String>
Returns 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_commands ⇒ Object
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 |