If you don't know or care about pagers and process management, you can stop reading now.
Background: I have a program that compile my code each time I modify it. It produces logs continuously on a terminal.
I'm trying to write a go program to wrap this program so that each compile log is opened in a pager, and so that when a new compile starts, the pager for the previous compile is automatically closed. Essentially, I'm trying to change the program from doing (pseudo-shell)
while true; do
awaitChange
build
end
to:
while true; do
awaitChange
kill $!
build | less & # <--- except less should be in the foreground of the terminal
to_kill=$!
end
There are wrinkles: I don't control the program that executes this loop. So I wrote a go program to process the input and separate it into a series of output buffers based on a regexp. I've gotten it to the point where I can start and kill less, and feed it the separated inputs.
My problem: less is not behaving as it would if you ran it on the commandline. The arrow keys don't work, ^C either does nothing (on the first run of less) or interrupts the parent program, etc.
I believe my mistake is that I am not correctly putting less into the foreground. I also suspect that I'm not correctly moving my go program back into the foreground each time I kill the child less process. I'm wondering if anyone knows the magical incantation that is required to make this work properly. My current code is here (with the compiler replaced by a random number generator, since it is proprietary to the company I work for). I have marked with TODOs the things I think are not working correctly.
Any ideas what I'm doing wrong here?