Skip to content

Commit

Permalink
[ruby/reline] Avoid STDIN.winsize called in require "reline"
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng authored and matzbot committed May 10, 2024
1 parent 3ec5a93 commit 68b6fe7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def lines(screen_width)
def initialize(config, encoding)
@config = config
@completion_append_character = ''
@screen_size = Reline::IOGate.get_screen_size
@screen_size = [0, 0] # Should be initialized with actual winsize in LineEditor#reset
reset_variables(encoding: encoding)
end

Expand Down
20 changes: 19 additions & 1 deletion test/reline/test_reline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,28 @@ def test_dumb_terminal
assert_equal("Reline::GeneralIO", out.chomp)
end

def test_require_reline_should_not_trigger_winsize
pend if win?
lib = File.expand_path("../../lib", __dir__)
code = <<~RUBY
require "io/console"
def STDIN.tty?; true; end
def STDOUT.tty?; true; end
def STDIN.winsize; raise; end
require("reline") && p(Reline.core.io_gate)
RUBY
out = IO.popen([{}, Reline.test_rubybin, "-I#{lib}", "-e", code], &:read)
assert_equal("Reline::ANSI", out.chomp)
end

def win?
/mswin|mingw/.match?(RUBY_PLATFORM)
end

def get_reline_encoding
if encoding = Reline.core.encoding
encoding
elsif RUBY_PLATFORM =~ /mswin|mingw/
elsif win?
Encoding::UTF_8
else
Encoding::default_external
Expand Down

0 comments on commit 68b6fe7

Please sign in to comment.