Skip to content

Commit

Permalink
[ruby/irb] Page show_source's output
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 authored and matzbot committed Sep 22, 2023
1 parent c0d27af commit f59b488
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
17 changes: 11 additions & 6 deletions lib/irb/cmd/show_source.rb
Expand Up @@ -2,6 +2,7 @@

require_relative "nop"
require_relative "../source_finder"
require_relative "../pager"
require_relative "../color"

module IRB
Expand Down Expand Up @@ -40,12 +41,16 @@ def execute(str = nil)
private

def show_source(source)
puts
puts "#{bold("From")}: #{source.file}:#{source.first_line}"
puts
code = IRB::Color.colorize_code(File.read(source.file))
puts code.lines[(source.first_line - 1)...source.last_line].join
puts
file_content = IRB::Color.colorize_code(File.read(source.file))
code = file_content.lines[(source.first_line - 1)...source.last_line].join
content = <<~CONTENT
#{bold("From")}: #{source.file}:#{source.first_line}
#{code}
CONTENT

Pager.page_content(content)
end

def bold(str)
Expand Down
14 changes: 8 additions & 6 deletions test/irb/test_irb.rb
Expand Up @@ -6,6 +6,11 @@
module TestIRB
class InputTest < IntegrationTestCase
def test_symbol_aliases_are_handled_correctly
write_rc <<~RUBY
# disable pager
STDIN.singleton_class.define_method(:tty?) { false }
RUBY

write_ruby <<~'RUBY'
class Foo
end
Expand All @@ -21,12 +26,11 @@ class Foo
end

def test_symbol_aliases_are_handled_correctly_with_singleline_mode
@irbrc = Tempfile.new('irbrc')
@irbrc.write <<~RUBY
write_rc <<~RUBY
# disable pager
STDIN.singleton_class.define_method(:tty?) { false }
IRB.conf[:USE_SINGLELINE] = true
RUBY
@irbrc.close
@envs['IRBRC'] = @irbrc.path

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

def test_symbol_aliases_dont_affect_ruby_syntax
Expand Down

0 comments on commit f59b488

Please sign in to comment.