this post was submitted on 23 Jul 2024
12 points (100.0% liked)

Rust Programming

7734 readers
2 users here now

founded 5 years ago
MODERATORS
 

For context, I am using the libraries bevy and print_typewriter.

I noticed that before the program even starts, I am able to type in characters. This is bad, since when I ask for the user's input, the previous characters are included inside of it.

How do I make sure that only the user's input after the program starts, and after I print a question gets in?

you are viewing a single comment's thread
view the rest of the comments
[–] Vector@lemmy.world 14 points 1 month ago* (last edited 1 month ago) (9 children)

In general allowing content to be supplied in advance on stdin is desirable behavior, because it allows a developer to (for example) write applications that work as pipes that can have content queued on the input stream ready for processing.

If that behaviour doesn’t suit your use case and you need to only accept input after a certain point, you could read() and simply ignore/discard the current content of stdin before you write your question/accept the answer from the user.

[–] Binette@lemmy.ml 2 points 1 month ago (8 children)

Thanks! So far it kinda works, but since I'm using print_typewriter, the characters that I'm printing are printed one by one, and user input can slip in between them. I'm not sure how to prevent them from showing up in the first place, and not make them appear in stdin.

Or maybe in this case I shouldn't use the terminal, right?

[–] Vector@lemmy.world 2 points 1 month ago

I’m sure there are ways to suppress output from stdin presenting in the terminal but I couldn’t tell you how to do it without looking it up myself.

The easiest entry point to this problem that I can think of off the top of my head is password input masking (e.g., when you run sudo and type your password, it prevents character output even though the characters are read by the application).

There is almost certainly a much better and more appropriate mechanism to prevent stdin characters from printing directly to the terminal (perhaps some kind of special character? A TTY control option?) but I don’t know it off-hand.

load more comments (7 replies)
load more comments (7 replies)