Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait pty readable if there may be data to read #23

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pty_output.wait_readable(sync_until - Time.now) seems to have no effect.
This code is inside while @pty_output.wait_readable(wait) so @pty_output is already readable.

chunk = @pty_output.read_nonblock(65536)
@maybe_read = false
vterm_write(chunk)
break if Time.now > sync_until
end
end
Expand Down