How to know when itś the current last line in the console with popen on ruby

40 Views Asked by At

I'm trying to make a program with action cable that can start a specific process in the terminal and interact with it with commands given by the user. So far I've managed to run a command in the terminal by installing open4 with popen (The default ruby popen wasn´t working correctly for some reason) like this:

[6]  development  (main)> pid, stdin, stdout, stderr = Open4::popen4 "./my_program"
=> [37158, #<IO:fd 41>, #<IO:fd 43>, #<IO:fd 46>]
[7]  development  (main)> stdout.gets
=> " __  __       _       _____          \n"
[8]  development  (main)> stdout.gets
=> "|  \\/  | __ _| |_ ___|  ___|   _ _ __ v 0.17 \n"
[9]  development  (main)> stdout.gets
=> "| |\\/| |/ _` | __/ _ \\ |_ | | | | '_ \\   \n"
[10]  development  (main)> stdout.gets
=> "| |  | | (_| | ||  __/  _|| |_| | | | |  \n"
[11]  development  (main)> stdout.gets
=> "|_|  |_|\\__,_|\\__\\___|_|   \\__,_|_| |_|  \n"
[12]  development  (main)> stdout.gets
=> "\n"
[13]  development  (main)> stdout.gets
=> "\n"
[14]  development  (main)> stdout.gets
=> "\n"

That works lovely, but if I try to do another stdout.gets it just "freezes", it's probably not frozen and just waits indefinitely for something to give me, stdout.eof? returns false so that isn´t useful.

I need to send the program's output to the user, but I also need to maintain the program running to accept further instructions from the user. How can I know when I've reached the last line of the current terminal output?

0

There are 0 best solutions below