Skip to content

Commit

Permalink
fix: windows double keypress issue (#48)
Browse files Browse the repository at this point in the history
* fix(paths): add os specific directory handling for windows

Adjusted the path generation in `get_results_dir_path`, `handle_main_command`, and `setup_terminal` to account for Windows-specific local config directory conventions using the `cfg!` macro.

* fix: windows double keypress issue

* style(fmt): apply cargo fmt

* feat(workflows): enable windows-gnu target release
  • Loading branch information
0Ky authored Oct 24, 2023
1 parent 33a3b08 commit 2792331
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 101 deletions.
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/rust-build.action@v1.4.3
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/rust-build.action@v1.4.3
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

0 comments on commit 2792331

Please sign in to comment.