Crate for reading partial lines from the terminal?

I'm looking for a crate to read and react to single characters without the user having to press enter.

  • I only need Linux support, other platforms are irrelevant.
  • I need to display a prompt (for examples: Apply changes? [Yes/No/Diff/Ask for each file] where the user can then enter Y, N, D or A).
  • The code is not async or multi threaded at this point in the program execution, so I don't need to worry about other parts of the output clobbering the terminal.
  • The code will be running in normal terminal mode with scrollback, none of that "alternate screen mode". This is not a full TUI program. Just a CLI program with some interactive questions.

It seems something like rustyline or reedline should fit, but it seems those only return control to the program when the user presses enter. I can't really find anything for my use case.

I'm sure ratatui could probably do this, but that seems like way overkill.

The terminal works not in raw mode by default, so that it usually read a line buffer.

In order to read only one key event, you should turn on raw mode, so that the crate console can help.

Here is its official example which just meets your demand.

1 Like

Thanks. I was (vaguely) aware of raw mode but had forgotten the name of it, and did not know any details.

You may also want to look at dialoguer, which is built on top of console by the same people and provides functionality for prompting for input without having to wait for "Enter".

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.