Skip to content
This repository
Browse code

Slightly refactor help command

  • Loading branch information...
commit bcfa5ebc72e2314e7c58a0bd04806da775e42d73 1 parent 73167e0
John Mair banister authored

Showing 1 changed file with 58 additions and 20 deletions. Show diff stats Hide diff stats

  1. +58 20 lib/pry/commands/help.rb
78 lib/pry/commands/help.rb
@@ -42,47 +42,85 @@ def process
42 42 def display_index(groups)
43 43 help_text = []
44 44
45   - groups.keys.sort_by(&method(:group_sort_key)).each do |key|
46   - commands = groups[key].sort_by{ |command| command.options[:listing].to_s }
  45 + sorted_group_names(groups).each do |group_name|
  46 + commands = sorted_commands(groups[group_name])
47 47
48   - unless commands.empty?
49   - help_text << "#{text.bold(key)}\n" + commands.map do |command|
50   - " #{command.options[:listing].to_s.ljust(18)} #{command.description}"
51   - end.join("\n")
  48 + if commands.any?
  49 + help_text << help_text_for_commands(group_name, commands)
52 50 end
53 51 end
54 52
55 53 stagger_output(help_text.join("\n\n"))
56 54 end
57 55
  56 + # Given a group name and an array of commands,
  57 + # return the help string for those commands.
  58 + #
  59 + # @param [String] name The group name.
  60 + # @param [Array<Pry::Command>]] commands
  61 + # @return [String] The generated help string.
  62 + def help_text_for_commands(name, commands)
  63 + "#{text.bold(name)}\n" + commands.map do |command|
  64 + " #{command.options[:listing].to_s.ljust(18)} #{command.description}"
  65 + end.join("\n")
  66 + end
  67 +
  68 + # @param [Hash] groups
  69 + # @return [Array<String>] An array of sorted group names.
  70 + def sorted_group_names(groups)
  71 + groups.keys.sort_by(&method(:group_sort_key))
  72 + end
  73 +
  74 + # Sort an array of commands by their `listing` name.
  75 + #
  76 + # @param [Array<Pry::Command>] commands The commands to sort
  77 + # @return [Array<Pry::Command>] commands sorted by listing name.
  78 + def sorted_commands(commands)
  79 + commands.sort_by{ |command| command.options[:listing].to_s }
  80 + end
  81 +
58 82 # Display help for an individual command or group.
59 83 #
60   - # @param String The string to search for.
  84 + # @param [String] search The string to search for.
61 85 def display_search(search)
62 86 if command = command_set.find_command_for_help(search)
63 87 display_command(command)
64 88 else
65   - groups = search_hash(search, command_groups)
  89 + display_filtered_search_results(search)
  90 + end
  91 + end
66 92
67   - if groups.size > 0
68   - display_index(groups)
69   - return
70   - end
  93 + # Display help for a searched item, filtered first by group
  94 + # and if that fails, filtered by command name.
  95 + #
  96 + # @param [String] search The string to search for.
  97 + def display_filtered_search_results(search)
  98 + groups = search_hash(search, command_groups)
71 99
72   - filtered = search_hash(search, visible_commands)
73   - raise CommandError, "No help found for '#{args.first}'" if filtered.empty?
  100 + if groups.size > 0
  101 + display_index(groups)
  102 + else
  103 + display_filtered_commands(search)
  104 + end
  105 + end
74 106
75   - if filtered.size == 1
76   - display_command(filtered.values.first)
77   - else
78   - display_index({"'#{search}' commands" => filtered.values})
79   - end
  107 + # Display help for a searched item, filtered by group
  108 + #
  109 + # @param [String] search The string to search for.
  110 + def display_filtered_commands(search)
  111 + filtered = search_hash(search, visible_commands)
  112 + raise CommandError, "No help found for '#{args.first}'" if filtered.empty?
  113 +
  114 + if filtered.size == 1
  115 + display_command(filtered.values.first)
  116 + else
  117 + display_index({"'#{search}' commands" => filtered.values})
80 118 end
81 119 end
82 120
83 121 # Display help for an individual command.
84 122 #
85   - # @param [Pry::Command]
  123 + # @param [Pry::Command] command
86 124 def display_command(command)
87 125 stagger_output command.new.help
88 126 end

0 comments on commit bcfa5eb

Please sign in to comment.
Something went wrong with that request. Please try again.