Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: windows double keypress issue #48

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 30 additions & 30 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
on:
release:
types: [created]

jobs:
release:
name: release ${{ matrix.target }}
runs-on: ubuntu-latest
permissions:
contents: write
strategy:
fail-fast: false
matrix:
include:
# - target: x86_64-pc-windows-gnu
# archive: zip
# - target: x86_64-unknown-linux-musl
# archive: tar.gz tar.xz tar.zst
- target: x86_64-apple-darwin
archive: zip
steps:
- uses: actions/checkout@master
- name: Compile and release
uses: rust-build/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TOOLCHAIN_VERSION: 1.70.0
with:
RUSTTARGET: ${{ matrix.target }}
ARCHIVE_TYPES: ${{ matrix.archive }}
on:
release:
types: [created]
jobs:
release:
name: release ${{ matrix.target }}
runs-on: ubuntu-latest
permissions:
contents: write
strategy:
fail-fast: false
matrix:
include:
- target: x86_64-pc-windows-gnu
archive: zip
# - target: x86_64-unknown-linux-musl
# archive: tar.gz tar.xz tar.zst
- target: x86_64-apple-darwin
archive: zip
steps:
- uses: actions/checkout@master
- name: Compile and release
uses: rust-build/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
TOOLCHAIN_VERSION: 1.70.0
with:
RUSTTARGET: ${{ matrix.target }}
ARCHIVE_TYPES: ${{ matrix.archive }}
126 changes: 64 additions & 62 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! And test statistics are returned from the runner.

use anyhow::{Context, Result};
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
use crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use mockall::automock;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -106,68 +106,70 @@ impl Runner {

if event::poll(timeout).context("Unable to poll for event")? {
if let Event::Key(key) = event::read().context("Unable to read event")? {
match self.input_mode {
InputMode::Normal => match key.code {
KeyCode::Char('e') => {
start_time = if is_started {
start_time + pause_time.elapsed()
} else {
Instant::now()
};
is_started = true;
self.input_mode = InputMode::Editing;
}
KeyCode::Char('q') => {
// todo return canceled test error and handle it in main
return Ok(TestResults::new(
Stats::default(),
self.config.clone(),
false,
));
}
_ => {}
},
InputMode::Editing => match key.code {
// Crossterm returns `ctrl+w` or ``ctrl+h` when `ctrl+backspace` is pressed
// see: https://github.com/crossterm-rs/crossterm/issues/504
KeyCode::Char('h') | KeyCode::Char('w')
if key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.remove_last_word();
}
KeyCode::Char(c) => {
self.input.push(c);

let expected_input = self
.expected_input
.get_string(self.input.len())
.chars()
.collect::<Vec<char>>();

let is_correct =
self.input.chars().last() == expected_input.last().copied();

if !is_correct {
self.raw_mistakes_count += 1;
} else {
self.raw_valid_characters_count += 1;
if key.kind == KeyEventKind::Press {
match self.input_mode {
InputMode::Normal => match key.code {
KeyCode::Char('e') => {
start_time = if is_started {
start_time + pause_time.elapsed()
} else {
Instant::now()
};
is_started = true;
self.input_mode = InputMode::Editing;
}
}
KeyCode::Backspace
if key.modifiers.contains(KeyModifiers::ALT)
| key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.remove_last_word();
}
KeyCode::Backspace => {
self.input.pop();
}
KeyCode::Esc => {
pause_time = Instant::now();
self.input_mode = InputMode::Normal;
}
_ => {}
},
KeyCode::Char('q') => {
// todo return canceled test error and handle it in main
return Ok(TestResults::new(
Stats::default(),
self.config.clone(),
false,
));
}
_ => {}
},
InputMode::Editing => match key.code {
// Crossterm returns `ctrl+w` or ``ctrl+h` when `ctrl+backspace` is pressed
// see: https://github.com/crossterm-rs/crossterm/issues/504
KeyCode::Char('h') | KeyCode::Char('w')
if key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.remove_last_word();
}
KeyCode::Char(c) => {
self.input.push(c);

let expected_input = self
.expected_input
.get_string(self.input.len())
.chars()
.collect::<Vec<char>>();

let is_correct =
self.input.chars().last() == expected_input.last().copied();

if !is_correct {
self.raw_mistakes_count += 1;
} else {
self.raw_valid_characters_count += 1;
}
}
KeyCode::Backspace
if key.modifiers.contains(KeyModifiers::ALT)
| key.modifiers.contains(KeyModifiers::CONTROL) =>
{
self.remove_last_word();
}
KeyCode::Backspace => {
self.input.pop();
}
KeyCode::Esc => {
pause_time = Instant::now();
self.input_mode = InputMode::Normal;
}
_ => {}
},
}
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/test_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use anyhow::{Context, Result};
use chrono::{DateTime, Datelike, Local, Timelike};
use crossterm::event::{self, Event, KeyCode};
use crossterm::event::{self, Event, KeyCode, KeyEventKind};
use ratatui::{
prelude::{Backend, Constraint, Direction, Layout, Rect},
style::{Style, Stylize},
Expand Down Expand Up @@ -196,11 +196,13 @@ impl TestResults {

if event::poll(Duration::from_millis(100)).context("Unable to poll for event")? {
if let Event::Key(key) = event::read().context("Unable to read event")? {
match key.code {
KeyCode::Esc => {
break;
if key.kind == KeyEventKind::Press {
match key.code {
KeyCode::Esc => {
break;
}
_ => {}
}
_ => {}
}
}
}
Expand Down Expand Up @@ -313,11 +315,13 @@ pub fn render_results<B: Backend>(

if event::poll(Duration::from_millis(100)).context("Unable to poll for event")? {
if let Event::Key(key) = event::read().context("Unable to read event")? {
match key.code {
KeyCode::Esc => {
break;
if key.kind == KeyEventKind::Press {
match key.code {
KeyCode::Esc => {
break;
}
_ => {}
}
_ => {}
}
}
}
Expand Down