From 6a6471b04b8231f06d93ebde393be5ac0ecd8992 Mon Sep 17 00:00:00 2001 From: msp <100043909+dev-msp@users.noreply.github.com> Date: Thu, 31 Mar 2022 15:20:31 -0400 Subject: [PATCH] feat: add --suppress-output (-s) to input command (#5017) * feat: add --suppress-output (-s) to input command * Don't handle cursor position since existing impl doesn't * Handle all raw mode outcomes --- crates/nu-command/src/platform/input.rs | 35 ++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/platform/input.rs b/crates/nu-command/src/platform/input.rs index 9a36031a69..a778e7aa16 100644 --- a/crates/nu-command/src/platform/input.rs +++ b/crates/nu-command/src/platform/input.rs @@ -1,3 +1,4 @@ +use crossterm::event::{Event, KeyCode, KeyModifiers}; use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; @@ -27,6 +28,7 @@ impl Command for Input { "read bytes (not text) until a stop byte", Some('u'), ) + .switch("suppress-output", "don't print keystroke values", Some('s')) .category(Category::Platform) } @@ -39,6 +41,7 @@ impl Command for Input { ) -> Result { let prompt: Option = call.opt(engine_state, stack, 0)?; let bytes_until: Option = call.get_flag(engine_state, stack, "bytes-until")?; + let suppress_output = call.has_flag("suppress-output"); if let Some(bytes_until) = bytes_until { let _ = crossterm::terminal::enable_raw_mode(); @@ -83,8 +86,38 @@ impl Command for Input { let _ = std::io::stdout().flush(); } - // Just read a normal line of text let mut buf = String::new(); + + if suppress_output { + crossterm::terminal::enable_raw_mode()?; + loop { + match crossterm::event::read() { + Ok(Event::Key(k)) => match k.code { + // TODO: maintain keycode parity with existing command + KeyCode::Char(_) if k.modifiers != KeyModifiers::NONE => continue, + KeyCode::Char(c) => buf.push(c), + KeyCode::Backspace => { + let _ = buf.pop(); + } + KeyCode::Enter => break, + _ => continue, + }, + Ok(_) => continue, + Err(event_error) => { + crossterm::terminal::disable_raw_mode()?; + return Err(event_error.into()); + } + } + } + crossterm::terminal::disable_raw_mode()?; + return Ok(Value::String { + val: buf, + span: call.head, + } + .into_pipeline_data()); + } + + // Just read a normal line of text let input = std::io::stdin().read_line(&mut buf); match input {