-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Getch for OTP26 #8037
Comments
Would you say this work-around is suitable for use in a loop? For example, if we wanted to capture all the keypresses of a skilled typist typing at full speed? Thank you. |
It is the exact same logic that we use for the normal Erlang shell. So if that is fast enough, then this is fast enough. |
Lovely, thank you again. I know lots of people who will be very happy to have a work-around. |
Maybe it would also be good to note that if you are a skilled typist and type very fast (for instance holding down the |
Easy ways to reliably observe multiple characters in the same message: hit an arrow key, or start typing in a language with multi-byte characters: Left = It also sends window-resize events: But it also uncooks the terminal, leading to \n not rendering as expected in the demo, and requiring additional workarounds (\r\n newlines on unix):
|
Is there a workaround using My particular use-case is collecting user input in raw mode during an test run using Elixir's |
If you pass |
Appreciate the suggestion. My hope, however, is for an option that does not require any flags on start, but that can "take over" the |
I don't know of a way to do that. You might be able to do it using some creative Better to spend that time on implementing a PR adding an official API to do what you want to do :) |
If you all are open to it, I completely agree! |
Yes, we would like to have such an API. My idea for adding it would be to add To make this work on Windows, maybe we first have to implement my "lazy read" solution in #8113. |
I'd like to make sure I understand the various parts. Starting from the lowest level and getting more abstract, we have The suggestion in #8113 points to the fact that |
Yes, that seems correct. To add to the complexity, I've not spent too much time thinking everything through, so I'm not sure if it will work or not. It is quite a bit of work just to see if the API will work, which is why I've shied away from it so far.
nitpick, |
This was very interesting. I found it especially useful that it is possible to get window resize events too. It is nice that there might be plans to make it more official in the future. Whatever the form it will take, I think it would be bit important to be able to receive both window resizes and keystrokes/input. If someone stumbles on this before official support has landed, it might be useful to know that to be able to receive in a loop, it is also necessary to start a process and register start() ->
proc_lib:spawn(
fun() ->
TTYState = prim_tty:init(#{}),
register(user, self()),
loop(TTYState)
end).
loop(TTYState) ->
receive
... -> do_stuff(),
loop(TTYState)
end. |
FYI, with #8887 you can now get window resize events on Unix. I've also implemented getch support that will be part of Erlang 28. I'll link the PR here once it is opened. |
Fixed in #8962 |
Thank you for your work on this @garazdawi! I'm hoping to test this out soon. |
Is your feature request related to a problem? Please describe.
We've got elixir command line scripts that requires
getch
. The use case is to detect single keypress events, without needing the return key.In OTP 25 we had a nice solution for
getch
. Our approach usedPort.open({:spawn, "tty_sl -c -e"}, [:binary, :eof])
. Here is a gist with a working demo script.In OTP 26, Lukas Larsson re-implemented the erlang shell, dropping
tty_sl
and replacing it withprim_tty
.Here is an demo module that implements
getch
for OTP26 usingprim_tty
...To run: put the module code into
my_ttt.erl
, then$ erlc my_ttt.erl && erl -user my_ttt
This solution is undocumented, and uses an internal API that can change at any time. It would be great to have official support for
getch
.Describe the solution you'd like
Documentation and stable API for
getch
, based on OTP26/prim_tty.Describe alternatives you've considered
In the near term we'll use a variation of the demo module above.
Additional context
Thanks to Lukas Larsson for giving guidance on OTP26/prim_tty.
The text was updated successfully, but these errors were encountered: