Skip to content

Commit

Permalink
Fix/input suppress output on windows (#9459)
Browse files Browse the repository at this point in the history
this PR should close #9018

# Description
This PR aims to fix the `input` command with `--suppress-output` on
Windows
This fixes two separates issues : 
- Keypresses being duplicated in output due to "Release" event not being
ignored
- "Return" event from entering the `input -s "blah :"` still being in
the event buffer (need to be cleared before reading keypresses)

# Tests + Formatting

- `cargo fmt --all -- --check` ✔️ 
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` ✔️
- `cargo test --workspace` ✔️
- `cargo run -- crates/nu-std/tests/run.nu` ✔️
  • Loading branch information
Sygmei committed Jun 19, 2023
1 parent d78e3c3 commit d25fb3a
Showing 1 changed file with 33 additions and 17 deletions.
50 changes: 33 additions & 17 deletions crates/nu-command/src/platform/input/input_.rs
@@ -1,4 +1,4 @@
use crossterm::event::{Event, KeyCode, KeyModifiers};
use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers};
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
Expand All @@ -7,6 +7,7 @@ use nu_protocol::{
Type, Value,
};
use std::io::{Read, Write};
use std::time::Duration;

#[derive(Clone)]
pub struct Input;
Expand Down Expand Up @@ -132,31 +133,46 @@ impl Command for Input {

if suppress_output || numchar_exists {
crossterm::terminal::enable_raw_mode()?;
// clear terminal events
while crossterm::event::poll(Duration::from_secs(0))? {
// If there's an event, read it to remove it from the queue
let _ = crossterm::event::read()?;
}

loop {
if i64::try_from(buf.len()).unwrap_or(0) >= numchar.item {
let _ = crossterm::terminal::disable_raw_mode();
break;
}
match crossterm::event::read() {
Ok(Event::Key(k)) => match k.code {
// TODO: maintain keycode parity with existing command
KeyCode::Char(c) => {
if k.modifiers == KeyModifiers::ALT
|| k.modifiers == KeyModifiers::CONTROL
{
if k.modifiers == KeyModifiers::CONTROL && c == 'c' {
crossterm::terminal::disable_raw_mode()?;
return Err(ShellError::IOError("SIGINT".to_string()));
Ok(Event::Key(k)) => match k.kind {
KeyEventKind::Press | KeyEventKind::Repeat => {
match k.code {
// TODO: maintain keycode parity with existing command
KeyCode::Char(c) => {
if k.modifiers == KeyModifiers::ALT
|| k.modifiers == KeyModifiers::CONTROL
{
if k.modifiers == KeyModifiers::CONTROL && c == 'c' {
crossterm::terminal::disable_raw_mode()?;
return Err(ShellError::IOError(
"SIGINT".to_string(),
));
}
continue;
}

buf.push(c);
}
KeyCode::Backspace => {
let _ = buf.pop();
}
continue;
KeyCode::Enter => {
break;
}
_ => continue,
}

buf.push(c);
}
KeyCode::Backspace => {
let _ = buf.pop();
}
KeyCode::Enter => break,
_ => continue,
},
Ok(_) => continue,
Expand Down

0 comments on commit d25fb3a

Please sign in to comment.