From 696a8d6936ad929025c3098e9020e70e959496c2 Mon Sep 17 00:00:00 2001 From: viruscamp Date: Thu, 2 Mar 2023 20:29:16 +0800 Subject: [PATCH] fix #109 typing twice in windows with crossterm-0.26+ --- crates/irust/src/irust.rs | 4 ++++ crates/irust/src/irust/engine.rs | 14 +++++++++++++- crates/irust/src/utils.rs | 6 +++++- crates/printer/examples/shell.rs | 6 +++++- script_examples/irust_vim_dylib/src/script.rs | 4 ++++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/irust/src/irust.rs b/crates/irust/src/irust.rs index 6d4288a9..3d8fc557 100644 --- a/crates/irust/src/irust.rs +++ b/crates/irust/src/irust.rs @@ -179,6 +179,10 @@ impl IRust { self.execute(Command::HandleCtrlC)?; } Event::Key(key_event) => match key_event { + KeyEvent { + kind: KeyEventKind::Release, + .. + } => (), KeyEvent { code: KeyCode::Char(c), modifiers: KeyModifiers::NONE, diff --git a/crates/irust/src/irust/engine.rs b/crates/irust/src/irust/engine.rs index 952ce4fb..d664a287 100644 --- a/crates/irust/src/irust/engine.rs +++ b/crates/irust/src/irust/engine.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, io::Write}; use crossterm::{ cursor::SetCursorStyle, - event::{Event, KeyCode, KeyEvent, KeyModifiers}, + event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}, style::Color, terminal::ClearType, }; @@ -369,6 +369,10 @@ impl IRust { if let Ok(key_event) = crossterm::event::read() { match key_event { + Event::Key(KeyEvent { + kind: KeyEventKind::Release, + .. + }) => continue, Event::Key(KeyEvent { code: KeyCode::Char(c), modifiers: KeyModifiers::NONE, @@ -461,6 +465,10 @@ impl IRust { if let Ok(key_event) = crossterm::event::read() { match key_event { + Event::Key(KeyEvent { + kind: KeyEventKind::Release, + .. + }) => (), Event::Key(KeyEvent { code: KeyCode::Char(c), modifiers: KeyModifiers::NONE, @@ -695,6 +703,10 @@ impl IRust { // 1 - wait for the macro key let macro_key = loop { match crossterm::event::read()? { + Event::Key(KeyEvent { + kind: KeyEventKind::Release, + .. + }) => (), Event::Key(KeyEvent { code: KeyCode::Char(c), modifiers: KeyModifiers::NONE, diff --git a/crates/irust/src/utils.rs b/crates/irust/src/utils.rs index 1e056446..53feddc2 100644 --- a/crates/irust/src/utils.rs +++ b/crates/irust/src/utils.rs @@ -263,13 +263,17 @@ fn _balanced_quotes(s: &str) -> bool { } pub fn ctrlc_cancel(process: &mut std::process::Child) -> Result<()> { - use crossterm::event::{Event, KeyCode, KeyEvent}; + use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind}; // Running a command as Command::new().output takes at minimum 1ms // So Polling should take a similar order of magnitude if let Ok(event) = crossterm::event::poll(std::time::Duration::from_millis(1)) { if event { if let Ok(event) = crossterm::event::read() { match event { + Event::Key(KeyEvent { + kind: KeyEventKind::Release, + .. + }) => (), Event::Key(KeyEvent { code: KeyCode::Char('c'), modifiers: crossterm::event::KeyModifiers::CONTROL, diff --git a/crates/printer/examples/shell.rs b/crates/printer/examples/shell.rs index 1f03f0d6..785d73f8 100644 --- a/crates/printer/examples/shell.rs +++ b/crates/printer/examples/shell.rs @@ -1,4 +1,4 @@ -use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; +use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; use crossterm::style::Color; use printer::{ buffer::Buffer, @@ -17,6 +17,10 @@ fn main() -> Result<()> { let inp = crossterm::event::read()?; match inp { crossterm::event::Event::Key(key) => match key { + KeyEvent { + kind: KeyEventKind::Release, + .. + } => (), KeyEvent { code: KeyCode::Char(c), modifiers: KeyModifiers::NONE, diff --git a/script_examples/irust_vim_dylib/src/script.rs b/script_examples/irust_vim_dylib/src/script.rs index 71d30330..81ee7be5 100644 --- a/script_examples/irust_vim_dylib/src/script.rs +++ b/script_examples/irust_vim_dylib/src/script.rs @@ -34,6 +34,10 @@ impl Vim { let cmd = (|| match event { Event::Key(key) => match key { + KeyEvent { + kind: KeyEventKind::Release, + .. + } => None, KeyEvent { code: KeyCode::Char(c), modifiers,