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

linenoiseRemoteRefereshLine() #140

Closed
wants to merge 1 commit into from

Conversation

garethspor
Copy link

We've been using linenoise in a larger multi-threaded project. When another thread uses printf to display some message and a linenoise prompt is on the screen, the message prints at the end of our current prompt, and no new prompt is displayed resulting in ugly output.

Our new function allows other processes to remotely refresh the linenoise line. Thereby they can print their string on its own line, then reshesh the prompt so everything looks nice.

This is accomplished by replacing read in several places with a function that uses select to wait on two file descriptors, stdin and a control pipe that we create. linenoiseRemoteRefreshLine() pushes a new control
character (ctrl-r) into our control pipe; when that is received, a flag is set that calls for refresh line to be called.

When other processes call printf and linenoise had a prompt displayed,
the text gets printed after the prompt and looks horrible.

This function allows other threads or processes to refresh the linenoise
line. Thereby they can print their string on its own line, then reshesh
the prompt so everything looks nice.

This is accomplished by replacing read in several places with a function
that uses select to wait on two file descriptors, stdin and a control
pipe that we create. linenoiseRemoteRefreshLine() pushes a new control
character (ctrl-r) into our control pipe; when that is received, a flag
is set that calls for refresh line to be called.
@rain-1
Copy link

rain-1 commented Mar 30, 2018

This patch has a high level of complexity and while it implements a feature that's valuable to your specific application I feel that there may be alternative solutions in other cases.

This patch doesn't need to be merged.

@antirez
Copy link
Owner

antirez commented Jul 18, 2018

Hello, I agree with @rain-1. Specifically the way I think linenoise should evolve to support this use case, is to make it internally able to be used in a stateless way. So instead of reading in a direct way, we should have an interface that feeds linenoise with characters. This way the normal blocking behavior can be easily provided by using, internally, the non-blocking API. However for advanced uses, it is possible to instead use linenoise by creating a context and feeding it with bytes and so forth. I have plans to make such changes, because in the future those features will be needed for redis-cli... Note how this also makes linenoise able to be used easily in order to, for example, create an IRC client or any more complex system doing multiplexing on multiple I/O devices: the console, sockets, and so forth.

@antirez antirez closed this Jul 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants