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

Unblocking read_key() / writing to stdin #188

Open
george-cosma opened this issue Oct 5, 2023 · 1 comment
Open

Unblocking read_key() / writing to stdin #188

george-cosma opened this issue Oct 5, 2023 · 1 comment

Comments

@george-cosma
Copy link

george-cosma commented Oct 5, 2023

I've been recently writing some async code, and one of the requirements was reading from the consol in raw mode, in a separate thread. Now, this isn't an issue per se:

    let console_result = tokio::task::spawn_blocking(move || Term::stdout().read_key()).await?;

Using this code snippet, I can put the blocking read_key() on it's own thread and be happy. Unfortunately, if I need to cancel this operation I don't have a consistent mechanism to do so. Tokio doesn't really allow me to kill the blocking thread directly, or I don't know of a way. There isn't a pretty way to do async console reading/writing - there are some options on windows, but no idea on unix systems.

An alternative would be implementing the functionality to write to stdin. I've made a rough proof-of-conept in a local fork that sends a pre-determined key. A proper system to write to stdin would solve this issue, but does require the implementation of a new feature

Note: my implementation of writing to stdin may not be that stable, but it works for me:

Note: although TIOCSTI was revoked in OpenBSD 6.2 (Oct 2017), apparently the Linux Kernel developers are of the opposite mind, categorically refusing to revoke it (you can read more about this in the OpenBSD Journal).

[Source]

@fadeevab
Copy link
Contributor

I faced the same problem with the same idea of the solution.

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

No branches or pull requests

2 participants