Class: IRB::ExtendCommand::ShowCmds

Inherits:
Nop show all
Defined in:
lib/irb/cmd/show_cmds.rb

Instance Attribute Summary

Attributes inherited from Nop

#irb_context

Instance Method Summary collapse

Methods inherited from Nop

category, description, execute, #initialize

Constructor Details

This class inherits a constructor from IRB::ExtendCommand::Nop

Instance Method Details

#execute(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/irb/cmd/show_cmds.rb', line 15

def execute(*args)
  commands_info = IRB::ExtendCommandBundle.all_commands_info
  commands_grouped_by_categories = commands_info.group_by { |cmd| cmd[:category] }

  user_aliases = irb_context.instance_variable_get(:@user_aliases)

  commands_grouped_by_categories["Aliases"] = user_aliases.map do |alias_name, target|
    { display_name: alias_name, description: "Alias for `#{target}`" }
  end

  if irb_context.with_debugger
    # Remove the original "Debugging" category
    commands_grouped_by_categories.delete("Debugging")
    # Remove the `help` command as it's delegated to the debugger
    commands_grouped_by_categories["Context"].delete_if { |cmd| cmd[:display_name] == :help }
    # Add an empty "Debugging (from debug.gem)" category at the end
    commands_grouped_by_categories["Debugging (from debug.gem)"] = []
  end

  longest_cmd_name_length = commands_info.map { |c| c[:display_name].length }.max

  output = StringIO.new

  commands_grouped_by_categories.each do |category, cmds|
    output.puts Color.colorize(category, [:BOLD])

    cmds.each do |cmd|
      output.puts "  #{cmd[:display_name].to_s.ljust(longest_cmd_name_length)}    #{cmd[:description]}"
    end

    output.puts
  end

  # Append the debugger help at the end
  if irb_context.with_debugger
    output.puts DEBUGGER__.help
  end

  Pager.page_content(output.string)
end