console-rs / console-rs/console

How to flush stdin buffer before calling `read_key()`?

Open
#187 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.2k
Forks
144
Avg merge
8h 24m
Merged PRs (30d)
4

Description

I'd like to be able to prevent keypresses from being recognized before read_key() is actually called. Currently I'm doing the following:

let term = Term::stdout();

thread::sleep(Duration::from_secs(5));

match term.read_key()? {
    _ => ...
}

I'd like for any keypresses that occur while the thread is asleep to not be registered, but if I press something during the timeout it still registers once the timeout is complete. I've tried calling flush(), but this seems to only affect stdout, not stdin:

let term = Term::stdout();

thread::sleep(Duration::from_secs(5));
term.flush()?;

match term.read_key()? {
    _ => ...
}

Thoughts?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the Term::read_key and Term::flush entry points used in the example, then inspect how input is handled around the five-second sleep. Determine whether pending keypresses can be discarded before read_key() without affecting stdout; done means keys pressed during the delay are not returned by the subsequent read.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.