improve factoring

This commit is contained in:
Florent Vilmart 2025-03-21 09:34:08 -04:00
parent fb75e16375
commit 6e78f36075

View File

@ -56,53 +56,40 @@ impl Command for Input {
call: &Call, call: &Call,
_input: PipelineData, _input: PipelineData,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let prompt_str: Option<String> = call.opt(engine_state, stack, 0)?; // Those options are not supported by reedline, default to the legacy
let bytes_until: Option<String> = call.get_flag(engine_state, stack, "bytes-until-any")?;
let suppress_output = call.has_flag(engine_state, stack, "suppress-output")?;
let numchar_flag: Option<Spanned<i64>> = call.get_flag(engine_state, stack, "numchar")?;
let numchar: Spanned<i64> = 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
// implementation // implementation
if suppress_output || bytes_until.is_some() || numchar_flag.is_some() { let use_legacy = [
call.get_flag::<String>(engine_state, stack, "bytes-until-any")?
.is_some(),
call.has_flag(engine_state, stack, "suppress-output")?,
call.get_flag::<Spanned<i64>>(engine_state, stack, "numchar")?
.is_some(),
]
.iter()
.any(|x| *x);
if use_legacy {
return self.legacy_input(engine_state, stack, call, _input); return self.legacy_input(engine_state, stack, call, _input);
} }
// Here we will render the default prompt to the right let prompt_str: Option<String> = call.opt(engine_state, stack, 0)?;
let default_val: Option<String> = call.get_flag(engine_state, stack, "default")?; let default_val: Option<String> = call.get_flag(engine_state, stack, "default")?;
let from_io_error = IoError::factory(call.head, None);
let default_str = match (&prompt_str, &default_val) { 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(), _ => "".to_string(),
}; };
let mut buf = String::new();
let prompt = ReedlinePrompt { let prompt = ReedlinePrompt {
// Put the default value in the indicator for now.
indicator: default_str, indicator: default_str,
left_prompt: prompt_str.unwrap_or("".to_string()), left_prompt: prompt_str.unwrap_or("".to_string()),
// Breaking change, the default is now in the right prompt
right_prompt: "".to_string(), right_prompt: "".to_string(),
}; };
let mut line_editor = Reedline::create(); 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); line_editor = line_editor.with_ansi_colors(false);
// TODO handle options let mut buf = String::new();
loop { loop {
match line_editor.read_line(&prompt) { match line_editor.read_line(&prompt) {
Ok(Signal::Success(buffer)) => { Ok(Signal::Success(buffer)) => {