From 5ee74b6ab58ec9bec719737f179275174f8b01d5 Mon Sep 17 00:00:00 2001 From: J-Kappes <87168492+J-Kappes@users.noreply.github.com> Date: Fri, 15 Sep 2023 11:55:37 +0000 Subject: [PATCH] fix input --until-bytes: now stops at any of given bytes (#10235) - this PR should close #10197 # Description `input --bytes-until` takes a string but used to only terminate on the first byte of that string. Now it checks for each byte in the string. # User-Facing Changes all of the above. No change in documentation needed. New behavior arguably fits better. # Tests + Formatting don't know how to test input --- .../nu-command/src/platform/input/input_.rs | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/crates/nu-command/src/platform/input/input_.rs b/crates/nu-command/src/platform/input/input_.rs index 54e6f575c6..d6aadf576b 100644 --- a/crates/nu-command/src/platform/input/input_.rs +++ b/crates/nu-command/src/platform/input/input_.rs @@ -5,6 +5,7 @@ use crossterm::{ style::Print, terminal::{self, ClearType}, }; +use itertools::Itertools; use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; @@ -40,9 +41,9 @@ impl Command for Input { .allow_variants_without_examples(true) .optional("prompt", SyntaxShape::String, "prompt to show the user") .named( - "bytes-until", + "bytes-until-any", SyntaxShape::String, - "read bytes (not text) until a stop byte", + "read bytes (not text) until any of the given stop bytes is seen", Some('u'), ) .named( @@ -63,7 +64,7 @@ impl Command for Input { _input: PipelineData, ) -> Result { let prompt: Option = call.opt(engine_state, stack, 0)?; - let bytes_until: Option = call.get_flag(engine_state, stack, "bytes-until")?; + let bytes_until: Option = call.get_flag(engine_state, stack, "bytes-until-any")?; let suppress_output = call.has_flag("suppress-output"); let numchar: Option> = call.get_flag(engine_state, stack, "numchar")?; let numchar: Spanned = numchar.unwrap_or(Spanned { @@ -80,19 +81,6 @@ impl Command for Input { )); } - let byte_until = if let Some(bytes_until) = bytes_until { - if let Some(c) = bytes_until.bytes().next() { - Some(c) - } else { - let _ = crossterm::terminal::disable_raw_mode(); - return Err(ShellError::IOError( - "input can't stop on this byte".to_string(), - )); - } - } else { - None - }; - if let Some(prompt) = &prompt { print!("{prompt}"); let _ = std::io::stdout().flush(); @@ -127,8 +115,8 @@ impl Command for Input { continue; } - if let Some(byte_until) = byte_until { - if c == byte_until as char { + if let Some(bytes_until) = bytes_until.as_ref() { + if bytes_until.bytes().contains(&(c as u8)) { break; } }