Skip to content

Commit 8886434

Browse files
authored
Allow defining custom commands in IRB (#886)
This is a feature that has been requested for a long time. It is now possible to define custom commands in IRB. Example usage: ```ruby require "irb/command" class HelloCommand < IRB::Command::Base description "Prints hello world" category "My commands" help_message "It doesn't do more than printing hello world." def execute puts "Hello world" end end IRB::Command.register(:hello, HelloCommand) ```
1 parent b32aee4 commit 8886434

File tree

5 files changed

+268
-279
lines changed

5 files changed

+268
-279
lines changed

lib/irb.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
require_relative "irb/init"
1212
require_relative "irb/context"
13-
require_relative "irb/command"
13+
require_relative "irb/default_commands"
1414

1515
require_relative "irb/ruby-lex"
1616
require_relative "irb/statement"

lib/irb/command.rb

Lines changed: 12 additions & 250 deletions
Original file line numberDiff line numberDiff line change
@@ -7,261 +7,23 @@
77
require_relative "command/base"
88

99
module IRB # :nodoc:
10-
module Command; end
11-
ExtendCommand = Command
10+
module Command
11+
@commands = {}
1212

13-
# Installs the default irb extensions command bundle.
14-
module ExtendCommandBundle
15-
# See ExtendCommandBundle.execute_as_command?.
16-
NO_OVERRIDE = 0
17-
OVERRIDE_PRIVATE_ONLY = 0x01
18-
OVERRIDE_ALL = 0x02
13+
class << self
14+
attr_reader :commands
1915

20-
@EXTEND_COMMANDS = [
21-
[
22-
:irb_context, :Context, "command/context",
23-
[:context, NO_OVERRIDE],
24-
[:conf, NO_OVERRIDE],
25-
],
26-
[
27-
:irb_exit, :Exit, "command/exit",
28-
[:exit, OVERRIDE_PRIVATE_ONLY],
29-
[:quit, OVERRIDE_PRIVATE_ONLY],
30-
[:irb_quit, OVERRIDE_PRIVATE_ONLY],
31-
],
32-
[
33-
:irb_exit!, :ForceExit, "command/force_exit",
34-
[:exit!, OVERRIDE_PRIVATE_ONLY],
35-
],
36-
37-
[
38-
:irb_current_working_workspace, :CurrentWorkingWorkspace, "command/chws",
39-
[:cwws, NO_OVERRIDE],
40-
[:pwws, NO_OVERRIDE],
41-
[:irb_print_working_workspace, OVERRIDE_ALL],
42-
[:irb_cwws, OVERRIDE_ALL],
43-
[:irb_pwws, OVERRIDE_ALL],
44-
[:irb_current_working_binding, OVERRIDE_ALL],
45-
[:irb_print_working_binding, OVERRIDE_ALL],
46-
[:irb_cwb, OVERRIDE_ALL],
47-
[:irb_pwb, OVERRIDE_ALL],
48-
],
49-
[
50-
:irb_change_workspace, :ChangeWorkspace, "command/chws",
51-
[:chws, NO_OVERRIDE],
52-
[:cws, NO_OVERRIDE],
53-
[:irb_chws, OVERRIDE_ALL],
54-
[:irb_cws, OVERRIDE_ALL],
55-
[:irb_change_binding, OVERRIDE_ALL],
56-
[:irb_cb, OVERRIDE_ALL],
57-
[:cb, NO_OVERRIDE],
58-
],
59-
60-
[
61-
:irb_workspaces, :Workspaces, "command/pushws",
62-
[:workspaces, NO_OVERRIDE],
63-
[:irb_bindings, OVERRIDE_ALL],
64-
[:bindings, NO_OVERRIDE],
65-
],
66-
[
67-
:irb_push_workspace, :PushWorkspace, "command/pushws",
68-
[:pushws, NO_OVERRIDE],
69-
[:irb_pushws, OVERRIDE_ALL],
70-
[:irb_push_binding, OVERRIDE_ALL],
71-
[:irb_pushb, OVERRIDE_ALL],
72-
[:pushb, NO_OVERRIDE],
73-
],
74-
[
75-
:irb_pop_workspace, :PopWorkspace, "command/pushws",
76-
[:popws, NO_OVERRIDE],
77-
[:irb_popws, OVERRIDE_ALL],
78-
[:irb_pop_binding, OVERRIDE_ALL],
79-
[:irb_popb, OVERRIDE_ALL],
80-
[:popb, NO_OVERRIDE],
81-
],
82-
83-
[
84-
:irb_load, :Load, "command/load"],
85-
[
86-
:irb_require, :Require, "command/load"],
87-
[
88-
:irb_source, :Source, "command/load",
89-
[:source, NO_OVERRIDE],
90-
],
91-
92-
[
93-
:irb, :IrbCommand, "command/subirb"],
94-
[
95-
:irb_jobs, :Jobs, "command/subirb",
96-
[:jobs, NO_OVERRIDE],
97-
],
98-
[
99-
:irb_fg, :Foreground, "command/subirb",
100-
[:fg, NO_OVERRIDE],
101-
],
102-
[
103-
:irb_kill, :Kill, "command/subirb",
104-
[:kill, OVERRIDE_PRIVATE_ONLY],
105-
],
106-
107-
[
108-
:irb_debug, :Debug, "command/debug",
109-
[:debug, NO_OVERRIDE],
110-
],
111-
[
112-
:irb_edit, :Edit, "command/edit",
113-
[:edit, NO_OVERRIDE],
114-
],
115-
[
116-
:irb_break, :Break, "command/break",
117-
],
118-
[
119-
:irb_catch, :Catch, "command/catch",
120-
],
121-
[
122-
:irb_next, :Next, "command/next"
123-
],
124-
[
125-
:irb_delete, :Delete, "command/delete",
126-
[:delete, NO_OVERRIDE],
127-
],
128-
[
129-
:irb_step, :Step, "command/step",
130-
[:step, NO_OVERRIDE],
131-
],
132-
[
133-
:irb_continue, :Continue, "command/continue",
134-
[:continue, NO_OVERRIDE],
135-
],
136-
[
137-
:irb_finish, :Finish, "command/finish",
138-
[:finish, NO_OVERRIDE],
139-
],
140-
[
141-
:irb_backtrace, :Backtrace, "command/backtrace",
142-
[:backtrace, NO_OVERRIDE],
143-
[:bt, NO_OVERRIDE],
144-
],
145-
[
146-
:irb_debug_info, :Info, "command/info",
147-
[:info, NO_OVERRIDE],
148-
],
149-
150-
[
151-
:irb_help, :Help, "command/help",
152-
[:help, NO_OVERRIDE],
153-
[:show_cmds, NO_OVERRIDE],
154-
],
155-
156-
[
157-
:irb_show_doc, :ShowDoc, "command/show_doc",
158-
[:show_doc, NO_OVERRIDE],
159-
],
160-
161-
[
162-
:irb_info, :IrbInfo, "command/irb_info"
163-
],
164-
165-
[
166-
:irb_ls, :Ls, "command/ls",
167-
[:ls, NO_OVERRIDE],
168-
],
169-
170-
[
171-
:irb_measure, :Measure, "command/measure",
172-
[:measure, NO_OVERRIDE],
173-
],
174-
175-
[
176-
:irb_show_source, :ShowSource, "command/show_source",
177-
[:show_source, NO_OVERRIDE],
178-
],
179-
[
180-
:irb_whereami, :Whereami, "command/whereami",
181-
[:whereami, NO_OVERRIDE],
182-
],
183-
[
184-
:irb_history, :History, "command/history",
185-
[:history, NO_OVERRIDE],
186-
[:hist, NO_OVERRIDE],
187-
],
188-
189-
[
190-
:irb_disable_irb, :DisableIrb, "command/disable_irb",
191-
[:disable_irb, NO_OVERRIDE],
192-
],
193-
]
194-
195-
def self.command_override_policies
196-
@@command_override_policies ||= @EXTEND_COMMANDS.flat_map do |cmd_name, cmd_class, load_file, *aliases|
197-
[[cmd_name, OVERRIDE_ALL]] + aliases
198-
end.to_h
199-
end
200-
201-
def self.execute_as_command?(name, public_method:, private_method:)
202-
case command_override_policies[name]
203-
when OVERRIDE_ALL
204-
true
205-
when OVERRIDE_PRIVATE_ONLY
206-
!public_method
207-
when NO_OVERRIDE
208-
!public_method && !private_method
209-
end
210-
end
211-
212-
def self.command_names
213-
command_override_policies.keys.map(&:to_s)
214-
end
215-
216-
@@commands = []
217-
218-
def self.all_commands_info
219-
return @@commands unless @@commands.empty?
220-
user_aliases = IRB.CurrentContext.command_aliases.each_with_object({}) do |(alias_name, target), result|
221-
result[target] ||= []
222-
result[target] << alias_name
223-
end
224-
225-
@EXTEND_COMMANDS.each do |cmd_name, cmd_class, load_file, *aliases|
226-
if !defined?(Command) || !Command.const_defined?(cmd_class, false)
227-
require_relative load_file
228-
end
229-
230-
klass = Command.const_get(cmd_class, false)
231-
aliases = aliases.map { |a| a.first }
232-
233-
if additional_aliases = user_aliases[cmd_name]
234-
aliases += additional_aliases
235-
end
236-
237-
display_name = aliases.shift || cmd_name
238-
@@commands << { display_name: display_name, description: klass.description, category: klass.category }
16+
# Registers a command with the given name.
17+
# Aliasing is intentionally not supported at the moment.
18+
def register(name, command_class)
19+
@commands[name] = [command_class, []]
23920
end
24021

241-
@@commands
242-
end
243-
244-
# Convert a command name to its implementation class if such command exists
245-
def self.load_command(command)
246-
command = command.to_sym
247-
@EXTEND_COMMANDS.each do |cmd_name, cmd_class, load_file, *aliases|
248-
next if cmd_name != command && aliases.all? { |alias_name, _| alias_name != command }
249-
250-
if !defined?(Command) || !Command.const_defined?(cmd_class, false)
251-
require_relative load_file
252-
end
253-
return Command.const_get(cmd_class, false)
22+
# This API is for IRB's internal use only and may change at any time.
23+
# Please do NOT use it.
24+
def _register_with_aliases(name, command_class, *aliases)
25+
@commands[name] = [command_class, aliases]
25426
end
255-
nil
256-
end
257-
258-
def self.def_extend_command(cmd_name, cmd_class, load_file, *aliases)
259-
@EXTEND_COMMANDS.delete_if { |name,| name == cmd_name }
260-
@EXTEND_COMMANDS << [cmd_name, cmd_class, load_file, *aliases]
261-
262-
# Just clear memoized values
263-
@@commands = []
264-
@@command_override_policies = nil
26527
end
26628
end
26729
end

lib/irb/command/edit.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'shellwords'
22

3+
require_relative "../color"
34
require_relative "../source_finder"
45

56
module IRB

0 commit comments

Comments
 (0)