Skip to content

Commit a6fe58e

Browse files
committed
Add irb_info command
1 parent 9ef0e6e commit a6fe58e

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

lib/irb/cmd/info.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: false
2+
3+
require_relative "nop"
4+
5+
# :stopdoc:
6+
module IRB
7+
module ExtendCommand
8+
class Info < Nop
9+
def execute
10+
Class.new {
11+
def inspect
12+
<<~EOM.chomp
13+
Ruby version: #{RUBY_VERSION}
14+
IRB version: #{IRB.version}
15+
InputMethod: #{IRB.CurrentContext.io.inspect}
16+
.irbrc path: #{IRB.rc_file}
17+
EOM
18+
end
19+
alias_method :to_s, :inspect
20+
}.new
21+
end
22+
end
23+
end
24+
end
25+
# :startdoc:

lib/irb/extend-command.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ def irb_context
121121
[:help, NO_OVERRIDE],
122122
],
123123

124+
[
125+
:irb_info, :Info, "irb/cmd/info"
126+
],
127+
124128
]
125129

126130
# Installs the default irb commands:

lib/irb/input-method.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def gets
4343
def readable_after_eof?
4444
false
4545
end
46+
47+
# For debug message
48+
def inspect
49+
'Abstract InputMethod'
50+
end
4651
end
4752

4853
class StdioInputMethod < InputMethod
@@ -93,6 +98,11 @@ def line(line_no)
9398
def encoding
9499
@stdin.external_encoding
95100
end
101+
102+
# For debug message
103+
def inspect
104+
'StdioInputMethod'
105+
end
96106
end
97107

98108
# Use a File for IO with irb, see InputMethod
@@ -125,6 +135,11 @@ def gets
125135
def encoding
126136
@io.external_encoding
127137
end
138+
139+
# For debug message
140+
def inspect
141+
'FileInputMethod'
142+
end
128143
end
129144

130145
begin
@@ -202,6 +217,13 @@ def encoding
202217
end
203218
Readline.completion_append_character = nil
204219
Readline.completion_proc = IRB::InputCompletor::CompletionProc
220+
221+
# For debug message
222+
def inspect
223+
inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
224+
readline_impl = (defined?(Reline) && Readline == Reline) ? 'Reline' : 'ext/readline'
225+
"ReadlineInputMethod with #{readline_impl} #{Readline::VERSION} and #{inputrc_path}"
226+
end
205227
end
206228
rescue LoadError
207229
end
@@ -297,5 +319,16 @@ def line(line_no)
297319
def encoding
298320
@stdin.external_encoding
299321
end
322+
323+
# For debug message
324+
def inspect
325+
config = Reline::Config.new
326+
if config.respond_to?(:inputrc_path)
327+
inputrc_path = config.inputrc_path
328+
else
329+
inputrc_path = File.expand_path(ENV['INPUTRC'] || '~/.inputrc')
330+
end
331+
"ReidlineInputMethod with Reline #{Reline::VERSION} and #{inputrc_path}"
332+
end
300333
end
301334
end

test/irb/test_cmd.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: false
2+
require "test/unit"
3+
require "irb"
4+
require "irb/extend-command"
5+
6+
module TestIRB
7+
class ExtendCommand < Test::Unit::TestCase
8+
def setup
9+
@pwd = Dir.pwd
10+
@tmpdir = File.join(Dir.tmpdir, "test_reline_config_#{$$}")
11+
begin
12+
Dir.mkdir(@tmpdir)
13+
rescue Errno::EEXIST
14+
FileUtils.rm_rf(@tmpdir)
15+
Dir.mkdir(@tmpdir)
16+
end
17+
Dir.chdir(@tmpdir)
18+
@home_backup = ENV["HOME"]
19+
ENV["HOME"] = @tmpdir
20+
end
21+
22+
def teardown
23+
ENV["HOME"] = @home_backup
24+
Dir.chdir(@pwd)
25+
FileUtils.rm_rf(@tmpdir)
26+
end
27+
28+
def test_irb_info_multiline
29+
FileUtils.touch("#{@tmpdir}/.inputrc")
30+
FileUtils.touch("#{@tmpdir}/.irbrc")
31+
IRB.setup(__FILE__, argv: [])
32+
IRB.conf[:USE_MULTILINE] = true
33+
IRB.conf[:USE_SINGLELINE] = false
34+
workspace = IRB::WorkSpace.new(self)
35+
irb = IRB::Irb.new(workspace)
36+
IRB.conf[:MAIN_CONTEXT] = irb.context
37+
expected = %r{
38+
Ruby\sversion: .+\n
39+
IRB\sversion:\sirb .+\n
40+
InputMethod:\sReidlineInputMethod\swith\sReline .+ and .+\n
41+
\.irbrc\spath: .+
42+
}x
43+
assert_match expected, irb.context.main.irb_info.to_s
44+
end
45+
46+
def test_irb_info_singleline
47+
FileUtils.touch("#{@tmpdir}/.inputrc")
48+
FileUtils.touch("#{@tmpdir}/.irbrc")
49+
IRB.setup(__FILE__, argv: [])
50+
IRB.conf[:USE_MULTILINE] = false
51+
IRB.conf[:USE_SINGLELINE] = true
52+
workspace = IRB::WorkSpace.new(self)
53+
irb = IRB::Irb.new(workspace)
54+
IRB.conf[:MAIN_CONTEXT] = irb.context
55+
expected = %r{
56+
Ruby\sversion: .+\n
57+
IRB\sversion:\sirb .+\n
58+
InputMethod:\sReadlineInputMethod\swith .+ and .+\n
59+
\.irbrc\spath: .+
60+
}x
61+
assert_match expected, irb.context.main.irb_info.to_s
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)