Skip to content

Commit 3cedc5c

Browse files
authored
Page show_source's output (#719)
1 parent 1d2d35d commit 3cedc5c

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

lib/irb/cmd/show_source.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_relative "nop"
44
require_relative "../source_finder"
5+
require_relative "../pager"
56
require_relative "../color"
67

78
module IRB
@@ -40,12 +41,16 @@ def execute(str = nil)
4041
private
4142

4243
def show_source(source)
43-
puts
44-
puts "#{bold("From")}: #{source.file}:#{source.first_line}"
45-
puts
46-
code = IRB::Color.colorize_code(File.read(source.file))
47-
puts code.lines[(source.first_line - 1)...source.last_line].join
48-
puts
44+
file_content = IRB::Color.colorize_code(File.read(source.file))
45+
code = file_content.lines[(source.first_line - 1)...source.last_line].join
46+
content = <<~CONTENT
47+
48+
#{bold("From")}: #{source.file}:#{source.first_line}
49+
50+
#{code}
51+
CONTENT
52+
53+
Pager.page_content(content)
4954
end
5055

5156
def bold(str)

test/irb/test_irb.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
module TestIRB
77
class InputTest < IntegrationTestCase
88
def test_symbol_aliases_are_handled_correctly
9+
write_rc <<~RUBY
10+
# disable pager
11+
STDIN.singleton_class.define_method(:tty?) { false }
12+
RUBY
13+
914
write_ruby <<~'RUBY'
1015
class Foo
1116
end
@@ -21,12 +26,11 @@ class Foo
2126
end
2227

2328
def test_symbol_aliases_are_handled_correctly_with_singleline_mode
24-
@irbrc = Tempfile.new('irbrc')
25-
@irbrc.write <<~RUBY
29+
write_rc <<~RUBY
30+
# disable pager
31+
STDIN.singleton_class.define_method(:tty?) { false }
2632
IRB.conf[:USE_SINGLELINE] = true
2733
RUBY
28-
@irbrc.close
29-
@envs['IRBRC'] = @irbrc.path
3034

3135
write_ruby <<~'RUBY'
3236
class Foo
@@ -43,8 +47,6 @@ class Foo
4347
# Make sure it's tested in singleline mode
4448
assert_include output, "InputMethod: ReadlineInputMethod"
4549
assert_include output, "From: #{@ruby_file.path}:1"
46-
ensure
47-
@irbrc.unlink if @irbrc
4850
end
4951

5052
def test_symbol_aliases_dont_affect_ruby_syntax

0 commit comments

Comments
 (0)