diff options
author | Stan Lo <stan.lo@shopify.com> | 2022-12-08 19:10:19 +0000 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2022-12-08 19:10:23 +0000 |
commit | 3956bb859c2442d34ea171db8f92f3e5895c43d9 (patch) | |
tree | 4800d4eb49ceadec321f37e644ab98fb1f597aa6 /lib/irb/cmd/show_cmds.rb | |
parent | 2cea8e014dbbbb68e4e8be367529b2beae564c54 (diff) |
[ruby/irb] Add "show_cmds" command to list all commands'
descriptions
(https://github.com/ruby/irb/pull/463)
https://github.com/ruby/irb/commit/7e857655ac
Diffstat (limited to 'lib/irb/cmd/show_cmds.rb')
-rw-r--r-- | lib/irb/cmd/show_cmds.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/irb/cmd/show_cmds.rb b/lib/irb/cmd/show_cmds.rb new file mode 100644 index 0000000000..acced27d48 --- /dev/null +++ b/lib/irb/cmd/show_cmds.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +require "stringio" +require_relative "nop" + +module IRB + # :stopdoc: + + module ExtendCommand + class ShowCmds < Nop + 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] } + longest_cmd_name_length = commands_info.map { |c| c[:display_name] }.max { |a, b| a.length <=> b.length }.length + + 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 + + puts output.string + + nil + end + end + end + + # :startdoc: +end |