nu-explore: Fix repeated char issue in cmdline (#9139)

Must be fixed; (though I'd test it)

Thanks for the reference [fdncred](https://github.com/fdncred).

close #9128

---------

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
Maxim Zhiburt 2023-05-08 20:38:42 +03:00 committed by GitHub
parent 7a945848de
commit a5d02a0737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,7 @@ use std::{
time::{Duration, Instant},
};
use crossterm::event::{poll, read, Event, KeyEvent};
use crossterm::event::{poll, read, Event, KeyEvent, KeyEventKind};
pub struct UIEvents {
tick_rate: Duration,
@ -37,13 +37,19 @@ impl UIEvents {
match poll(self.tick_rate) {
Ok(true) => {
if let Event::Key(event) = read()? {
Ok(Some(event))
} else {
let time_spent = now.elapsed();
let rest = self.tick_rate - time_spent;
Self { tick_rate: rest }.next()
// We are only interested in Pressed events;
// It's crucial because there are cases where terminal MIGHT produce false events;
// 2 events 1 for release 1 for press.
// Want to react only on 1 of them so we do.
if event.kind == KeyEventKind::Press {
return Ok(Some(event));
}
}
let time_spent = now.elapsed();
let rest = self.tick_rate - time_spent;
Self { tick_rate: rest }.next()
}
Ok(false) => Ok(None),
Err(err) => Err(err),
@ -53,7 +59,7 @@ impl UIEvents {
pub fn try_next(&self) -> Result<Option<KeyEvent>> {
match poll(Duration::default()) {
Ok(true) => match read()? {
Event::Key(event) => Ok(Some(event)),
Event::Key(event) if event.kind == KeyEventKind::Press => Ok(Some(event)),
_ => Ok(None),
},
Ok(false) => Ok(None),