From 6e78f36075b78416115e992899d39829bba321e5 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Fri, 21 Mar 2025 09:34:08 -0400 Subject: [PATCH] improve factoring --- .../nu-command/src/platform/input/input_.rs | 49 +++++++------------ 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/crates/nu-command/src/platform/input/input_.rs b/crates/nu-command/src/platform/input/input_.rs index 033eb5a137..29994cb0c2 100644 --- a/crates/nu-command/src/platform/input/input_.rs +++ b/crates/nu-command/src/platform/input/input_.rs @@ -56,53 +56,40 @@ impl Command for Input { call: &Call, _input: PipelineData, ) -> Result { - let prompt_str: Option = call.opt(engine_state, stack, 0)?; - let bytes_until: Option = call.get_flag(engine_state, stack, "bytes-until-any")?; - let suppress_output = call.has_flag(engine_state, stack, "suppress-output")?; - let numchar_flag: Option> = call.get_flag(engine_state, stack, "numchar")?; - let numchar: Spanned = numchar_flag.unwrap_or(Spanned { - item: i64::MAX, - span: call.head, - }); - - let from_io_error = IoError::factory(call.head, None); - - if numchar.item < 1 { - return Err(ShellError::UnsupportedInput { - msg: "Number of characters to read has to be positive".to_string(), - input: "value originated from here".to_string(), - msg_span: call.head, - input_span: numchar.span, - }); - } - - // Those 2 options are not supported by reedline, default to the legacy + // Those options are not supported by reedline, default to the legacy // implementation - if suppress_output || bytes_until.is_some() || numchar_flag.is_some() { + let use_legacy = [ + call.get_flag::(engine_state, stack, "bytes-until-any")? + .is_some(), + call.has_flag(engine_state, stack, "suppress-output")?, + call.get_flag::>(engine_state, stack, "numchar")? + .is_some(), + ] + .iter() + .any(|x| *x); + + if use_legacy { return self.legacy_input(engine_state, stack, call, _input); } - // Here we will render the default prompt to the right + let prompt_str: Option = call.opt(engine_state, stack, 0)?; let default_val: Option = call.get_flag(engine_state, stack, "default")?; + + let from_io_error = IoError::factory(call.head, None); + let default_str = match (&prompt_str, &default_val) { - (Some(_prompt), Some(val)) => format!("(default: {val}) ").to_string(), + (Some(_prompt), Some(val)) => format!("(default: {val}) "), _ => "".to_string(), }; - let mut buf = String::new(); let prompt = ReedlinePrompt { - // Put the default value in the indicator for now. indicator: default_str, left_prompt: prompt_str.unwrap_or("".to_string()), - // Breaking change, the default is now in the right prompt right_prompt: "".to_string(), }; let mut line_editor = Reedline::create(); - // Disable ansi colors for now, for backwards compat. This will be configurable in the - // future line_editor = line_editor.with_ansi_colors(false); - // TODO handle options - + let mut buf = String::new(); loop { match line_editor.read_line(&prompt) { Ok(Signal::Success(buffer)) => {