klogg icon indicating copy to clipboard operation
klogg copied to clipboard

Feature request: stdin redirection

Open ltrzesniewski opened this issue 3 years ago • 6 comments

I'd like to feed klogg a generated file, and stop generating the file when the user closes it. This is problematic if an instance of klogg is already running, as the secondary instance will send a message to the primary instance then exit (see sendFilesToPrimaryInstance), which makes it hard to determine when the user closes the file.

Proposed solution: add a command line option such as --wait, which would keep the process alive until the file is closed or until the main process exits.

Waiting until the primary process exits would be much easier to implement, but would be less precise. Waiting until the file is closed would require the primary instance to communicate this info back to the secondary one, and would also require the first started instance to behave like a secondary and to spawn a primary which would show the UI. I'm not sure if that's worth the effort.

Many thanks for this app BTW! 🙂

ltrzesniewski avatar Jun 08 '22 12:06 ltrzesniewski

Alternatively, if klogg could read from stdin, that would be awesome, but I suppose it would be complicated to handle...

ltrzesniewski avatar Jun 08 '22 13:06 ltrzesniewski

That's an interesting use case. I think that more general solution would be adding a command line option for secondary instance to output a list of currently opened files. Then external programs can do whatever they want with it.

variar avatar Jun 08 '22 18:06 variar

Actually, the real use case is to feed the output of kubectl logs --follow to klogg easily. This command tails the logs of a service deployed in Kubernetes, and it would be really convenient to open them in klogg.

Since klogg doesn't currently support reading from stdin, I thought about redirecting the output of that command to a file and then open that file in klogg. The issue with this approach is that I would need to detect when the file is closed in klogg in order to stop the kubectl command.

ltrzesniewski avatar Jun 08 '22 22:06 ltrzesniewski

Reading stdin is indeed a useful feature. The problem with stdin is that klogg doesn't store file content in memory, so the underlying file should be seekable.

But it should be possible to implement a workaround. A secondary instance can read from stdin and write to a temporary file. It will communicate the file name and some back communication channel to primary (like QSharedMemory id). Primary then can read as normal, and when user closes the file it will notify secondary to close stdin and exit.

If there is no primary then the first klogg instance will spawn a secondary to do the stdin redirection.

variar avatar Jun 08 '22 23:06 variar

The interface for most parts of UI is quite simple. It requires a way to tell an offset by line number and read a block of bytes between two offsets. So it should be possible to implement primary-secondary communication protocol that provides the data. This allows to create not only local stdin reader but also a remote secondary instance over ssh connection.

variar avatar Jun 08 '22 23:06 variar

But it should be possible to implement a workaround. A secondary instance can read from stdin and write to a temporary file. It will communicate the file name and some back communication channel to primary (like QSharedMemory id). Primary then can read as normal, and when user closes the file it will notify secondary to close stdin and exit.

That would be awesome 🙂

If there is no primary then the first klogg instance will spawn a secondary to do the stdin redirection.

Wouldn't it be better the other way around? If there's no primary, the first klogg instance would spawn a primary and become a secondary itself, then handle stdin redirection? This way the behavior would be the same whether there is already an instance running or not. Closing the tab would close the secondary.

ltrzesniewski avatar Jun 09 '22 08:06 ltrzesniewski