Skip to content

Commit

Permalink
Wait pty readable if there may be data to read
Browse files Browse the repository at this point in the history
  • Loading branch information
YO4 committed Sep 25, 2024
1 parent 92e188a commit 8fe5bd0
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/yamatanooroti/vterm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def start_terminal(height, width, command, wait: 0.01, timeout: 2, startup_messa
@timeout = timeout
@wait = wait
@result = nil
@maybe_read = false

@pty_output, @pty_input, @pid = PTY.spawn(*command)
@pty_output.winsize = [height, width]
Expand Down Expand Up @@ -41,6 +42,7 @@ def write(str)
end
end
@pty_input.write(str_to_write)
@maybe_read = true
# Write str (e.g. `exit`) to pty_input might terminate the process.
try_sync
end
Expand Down Expand Up @@ -80,6 +82,7 @@ def close
response = @vterm.read
begin
@pty_input.write(response)
@maybe_read = true if response != nil && response != ""
rescue Errno::EIO
# In case process terminates suddenly after writing "\e[6n"
end
Expand All @@ -89,7 +92,10 @@ def close
private def sync(wait = @wait)
sync_until = Time.now + @timeout
while @pty_output.wait_readable(wait)
vterm_write(@pty_output.read_nonblock(65536))
@pty_output.wait_readable(sync_until - Time.now) if @maybe_read
chunk = @pty_output.read_nonblock(65536)
@maybe_read = false
vterm_write(chunk)
break if Time.now > sync_until
end
end
Expand Down

0 comments on commit 8fe5bd0

Please sign in to comment.