ruby/lib/irb/command/show_cmds.rb
Stan Lo f5801e2bf4 [ruby/irb] Standardize command related names
(https://github.com/ruby/irb/pull/873)

* Replace ExtendCommand with Command and standardize command related names

1. Rename lib/irb/extend-command.rb to lib/irb/command.rb
2. Rename lib/irb/cmd/*.rb to lib/irb/command/*.rb
3. Rename test/irb/test_cmd.rb to test/irb/test_command.rb
4. Rename ExtendCommand to Command

* Alias ExtendCommand to Command and deprecate it

* Rename Command::Nop to Command::Base

* Not deprecate old constants just yet

* Add lib/irb/cmd/nop.rb back

462c1284af
2024-02-16 16:47:36 +00:00

59 lines
1.8 KiB
Ruby

# frozen_string_literal: true
require "stringio"
require_relative "../pager"
module IRB
# :stopdoc:
module Command
class ShowCmds < Base
category "IRB"
description "List all available commands and their description."
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
end
end
# :startdoc:
end