|
1 | 1 | require_relative 'helper' |
2 | 2 | require 'reline' |
3 | 3 | require 'stringio' |
| 4 | +begin |
| 5 | + require "pty" |
| 6 | +rescue LoadError # some platforms don't support PTY |
| 7 | +end |
4 | 8 |
|
5 | 9 | class Reline::Test < Reline::TestCase |
6 | 10 | class DummyCallbackObject |
@@ -432,6 +436,37 @@ def win? |
432 | 436 | /mswin|mingw/.match?(RUBY_PLATFORM) |
433 | 437 | end |
434 | 438 |
|
| 439 | + def test_tty_amibuous_width |
| 440 | + omit unless defined?(PTY) |
| 441 | + ruby_file = Tempfile.create('rubyfile') |
| 442 | + ruby_file.write(<<~RUBY) |
| 443 | + require 'reline' |
| 444 | + Thread.new { sleep 2; puts 'timeout'; exit } |
| 445 | + p [Reline.ambiguous_width, gets.chomp] |
| 446 | + RUBY |
| 447 | + ruby_file.close |
| 448 | + lib = File.expand_path('../../lib', __dir__) |
| 449 | + cmd = [{ 'TERM' => 'xterm' }, 'ruby', '-I', lib, ruby_file.to_path] |
| 450 | + |
| 451 | + # Calculate ambiguous width from cursor position |
| 452 | + [1, 2].each do |ambiguous_width| |
| 453 | + PTY.spawn(*cmd) do |r, w, pid| |
| 454 | + loop { break if r.readpartial(1024).include?("\e[6n") } |
| 455 | + w.puts "hello\e[10;#{ambiguous_width + 1}Rworld" |
| 456 | + assert_include(r.gets, [ambiguous_width, 'helloworld'].inspect) |
| 457 | + Process.waitpid pid |
| 458 | + end |
| 459 | + end |
| 460 | + |
| 461 | + # Ambiguous width = 1 when cursor pos timed out |
| 462 | + PTY.spawn(*cmd) do |r, w, pid| |
| 463 | + loop { break if r.readpartial(1024).include?("\e[6n") } |
| 464 | + w.puts "hello\e[10;2Sworld" |
| 465 | + assert_include(r.gets, [1, "hello\e[10;2Sworld"].inspect) |
| 466 | + Process.waitpid pid |
| 467 | + end |
| 468 | + end |
| 469 | + |
435 | 470 | def get_reline_encoding |
436 | 471 | if encoding = Reline.core.encoding |
437 | 472 | encoding |
|
0 commit comments