lib/expectr.rb in expectr-1.0.3 vs lib/expectr.rb in expectr-1.1.0

- old
+ new

@@ -1,8 +1,9 @@ require 'pty' require 'timeout' require 'thread' +require 'io/console' require 'expectr/error' require 'expectr/version' # Public: Expectr is an API to the functionality of Expect (see @@ -86,10 +87,11 @@ @out_mutex = Mutex.new @out_update = false @interact = false @stdout,@stdin,@pid = PTY.spawn(cmd) + @stdout.winsize = STDOUT.winsize Thread.new do while @pid > 0 unless select([@stdout], nil, nil, @timeout).nil? buf = ''.encode("UTF-8") @@ -150,10 +152,15 @@ # SIGTSTP should be sent along to the process as well. old_tstp_trap = trap 'TSTP' do send "\C-z" end + + # SIGWINCH should trigger an update to the child process + old_winch_trap = trap 'WINCH' do + @stdout.winsize = STDOUT.winsize + end interact = Thread.new do input = ''.encode("UTF-8") while @pid > 0 && @interact if select([STDIN], nil, nil, 1) @@ -162,10 +169,11 @@ end end trap 'INT', old_int_trap trap 'TSTP', old_tstp_trap + trap 'WINCH', old_winch_trap `stty #{old_tty}` @interact = false end blocking ? interact.join : interact @@ -289,9 +297,16 @@ def clear_buffer! @out_mutex.synchronize do @buffer = ''.encode("UTF-8") @out_update = false end + end + + # Public: Return the child's window size. + # + # Returns a two-element array (same as IO#winsize). + def winsize + @stdout.winsize end # Internal: Print buffer to STDOUT if @flush_buffer is true # # buf - String to be printed to STDOUT