[ruby/irb] Page show_source's output

(https://github.com/ruby/irb/pull/719)

https://github.com/ruby/irb/commit/3cedc5cb62
This commit is contained in:
Stan Lo 2023-09-22 14:00:25 +01:00 committed by git
parent c0d27af114
commit f59b488b5a
2 changed files with 19 additions and 12 deletions

View File

@ -2,6 +2,7 @@
require_relative "nop" require_relative "nop"
require_relative "../source_finder" require_relative "../source_finder"
require_relative "../pager"
require_relative "../color" require_relative "../color"
module IRB module IRB
@ -40,12 +41,16 @@ module IRB
private private
def show_source(source) def show_source(source)
puts file_content = IRB::Color.colorize_code(File.read(source.file))
puts "#{bold("From")}: #{source.file}:#{source.first_line}" code = file_content.lines[(source.first_line - 1)...source.last_line].join
puts content = <<~CONTENT
code = IRB::Color.colorize_code(File.read(source.file))
puts code.lines[(source.first_line - 1)...source.last_line].join #{bold("From")}: #{source.file}:#{source.first_line}
puts
#{code}
CONTENT
Pager.page_content(content)
end end
def bold(str) def bold(str)

View File

@ -6,6 +6,11 @@ require_relative "helper"
module TestIRB module TestIRB
class InputTest < IntegrationTestCase class InputTest < IntegrationTestCase
def test_symbol_aliases_are_handled_correctly def test_symbol_aliases_are_handled_correctly
write_rc <<~RUBY
# disable pager
STDIN.singleton_class.define_method(:tty?) { false }
RUBY
write_ruby <<~'RUBY' write_ruby <<~'RUBY'
class Foo class Foo
end end
@ -21,12 +26,11 @@ module TestIRB
end end
def test_symbol_aliases_are_handled_correctly_with_singleline_mode def test_symbol_aliases_are_handled_correctly_with_singleline_mode
@irbrc = Tempfile.new('irbrc') write_rc <<~RUBY
@irbrc.write <<~RUBY # disable pager
STDIN.singleton_class.define_method(:tty?) { false }
IRB.conf[:USE_SINGLELINE] = true IRB.conf[:USE_SINGLELINE] = true
RUBY RUBY
@irbrc.close
@envs['IRBRC'] = @irbrc.path
write_ruby <<~'RUBY' write_ruby <<~'RUBY'
class Foo class Foo
@ -43,8 +47,6 @@ module TestIRB
# Make sure it's tested in singleline mode # Make sure it's tested in singleline mode
assert_include output, "InputMethod: ReadlineInputMethod" assert_include output, "InputMethod: ReadlineInputMethod"
assert_include output, "From: #{@ruby_file.path}:1" assert_include output, "From: #{@ruby_file.path}:1"
ensure
@irbrc.unlink if @irbrc
end end
def test_symbol_aliases_dont_affect_ruby_syntax def test_symbol_aliases_dont_affect_ruby_syntax