From 4842885aa64680aac5f58856345ab0e1f399827c Mon Sep 17 00:00:00 2001 From: lesleyrs <19632758+lesleyrs@users.noreply.github.com> Date: Thu, 13 Apr 2023 22:21:35 +0200 Subject: [PATCH] fix(examples): update input in examples to only use press events (#129) --- examples/demo/crossterm.rs | 18 ++++++++++-------- examples/list.rs | 16 +++++++++------- examples/popup.rs | 12 +++++++----- examples/table.rs | 14 ++++++++------ examples/tabs.rs | 14 ++++++++------ 5 files changed, 42 insertions(+), 32 deletions(-) diff --git a/examples/demo/crossterm.rs b/examples/demo/crossterm.rs index 15e7eb7e..acf5ff89 100644 --- a/examples/demo/crossterm.rs +++ b/examples/demo/crossterm.rs @@ -1,6 +1,6 @@ use crate::{app::App, ui}; use crossterm::{ - event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, + event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; @@ -56,13 +56,15 @@ fn run_app( .unwrap_or_else(|| Duration::from_secs(0)); if crossterm::event::poll(timeout)? { if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char(c) => app.on_key(c), - KeyCode::Left => app.on_left(), - KeyCode::Up => app.on_up(), - KeyCode::Right => app.on_right(), - KeyCode::Down => app.on_down(), - _ => {} + if key.kind == KeyEventKind::Press { + match key.code { + KeyCode::Char(c) => app.on_key(c), + KeyCode::Left => app.on_left(), + KeyCode::Up => app.on_up(), + KeyCode::Right => app.on_right(), + KeyCode::Down => app.on_down(), + _ => {} + } } } } diff --git a/examples/list.rs b/examples/list.rs index 9d3bb503..11fe65ad 100644 --- a/examples/list.rs +++ b/examples/list.rs @@ -1,5 +1,5 @@ use crossterm::{ - event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, + event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; @@ -186,12 +186,14 @@ fn run_app( .unwrap_or_else(|| Duration::from_secs(0)); if crossterm::event::poll(timeout)? { if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - KeyCode::Left => app.items.unselect(), - KeyCode::Down => app.items.next(), - KeyCode::Up => app.items.previous(), - _ => {} + if key.kind == KeyEventKind::Press { + match key.code { + KeyCode::Char('q') => return Ok(()), + KeyCode::Left => app.items.unselect(), + KeyCode::Down => app.items.next(), + KeyCode::Up => app.items.previous(), + _ => {} + } } } } diff --git a/examples/popup.rs b/examples/popup.rs index 4b376519..6a299fb9 100644 --- a/examples/popup.rs +++ b/examples/popup.rs @@ -9,7 +9,7 @@ use ratatui::{ use std::{error::Error, io}; use crossterm::{ - event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, + event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; @@ -57,10 +57,12 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( terminal.draw(|f| ui(f, &app))?; if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - KeyCode::Char('p') => app.show_popup = !app.show_popup, - _ => {} + if key.kind == KeyEventKind::Press { + match key.code { + KeyCode::Char('q') => return Ok(()), + KeyCode::Char('p') => app.show_popup = !app.show_popup, + _ => {} + } } } } diff --git a/examples/table.rs b/examples/table.rs index 38b5c80e..8f692f75 100644 --- a/examples/table.rs +++ b/examples/table.rs @@ -1,5 +1,5 @@ use crossterm::{ - event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, + event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; @@ -106,11 +106,13 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( terminal.draw(|f| ui(f, &mut app))?; if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - KeyCode::Down => app.next(), - KeyCode::Up => app.previous(), - _ => {} + if key.kind == KeyEventKind::Press { + match key.code { + KeyCode::Char('q') => return Ok(()), + KeyCode::Down => app.next(), + KeyCode::Up => app.previous(), + _ => {} + } } } } diff --git a/examples/tabs.rs b/examples/tabs.rs index b5c24946..abcafe88 100644 --- a/examples/tabs.rs +++ b/examples/tabs.rs @@ -1,5 +1,5 @@ use crossterm::{ - event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, + event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind}, execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; @@ -72,11 +72,13 @@ fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<( terminal.draw(|f| ui(f, &app))?; if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - KeyCode::Right => app.next(), - KeyCode::Left => app.previous(), - _ => {} + if key.kind == KeyEventKind::Press { + match key.code { + KeyCode::Char('q') => return Ok(()), + KeyCode::Right => app.next(), + KeyCode::Left => app.previous(), + _ => {} + } } } }