This commit is contained in:
Florent Vilmart 2025-03-24 08:01:07 -04:00
parent 3ae38d374c
commit 457feb0843

View File

@ -88,34 +88,32 @@ impl Command for Input {
let prompt_str: Option<String> = call.opt(engine_state, stack, 0)?; 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 history_file_val: Option<String> = call.get_flag(engine_state, stack, "history-file")?; let history_file_val: Option<String> =
call.get_flag(engine_state, stack, "history-file")?;
let max_history: usize = call let max_history: usize = call
.get_flag::<i64>(engine_state, stack, "max-history")? .get_flag::<i64>(engine_state, stack, "max-history")?
.map(|l| if l < 0 { 0 } else { l as usize }) .map(|l| if l < 0 { 0 } else { l as usize })
.unwrap_or(HISTORY_SIZE); .unwrap_or(HISTORY_SIZE);
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}) "), (Some(_prompt), Some(val)) => format!("(default: {val}) "),
_ => "".to_string(), _ => "".to_string(),
}; };
let history_entries = match input { let history_entries = match input {
PipelineData::Value(Value::List { vals, .. }, ..) => { PipelineData::Value(Value::List { vals, .. }, ..) => Some(vals),
Some(vals)
}
_ => None, _ => None,
}; };
// If we either have history entries or history file, we create an history // If we either have history entries or history file, we create an history
let history = match (history_entries.is_some(), history_file_val.is_some()) { let history = match (history_entries.is_some(), history_file_val.is_some()) {
(false, false) => None, (false, false) => None, // Neither are set, no need for history support
_ => { _ => {
let mut history = match history_file_val { let mut history = match history_file_val {
Some(file) => FileBackedHistory::with_file(max_history, file.into()), Some(file) => FileBackedHistory::with_file(max_history, file.into()),
None => FileBackedHistory::new(max_history) None => FileBackedHistory::new(max_history),
}.expect("Error creating history file"); }
.expect("Error creating history file");
if let Some(vals) = history_entries { if let Some(vals) = history_entries {
vals.iter().for_each(|val| { vals.iter().for_each(|val| {
@ -125,7 +123,7 @@ impl Command for Input {
}); });
} }
Some(history) Some(history)
}, }
}; };
let prompt = ReedlinePrompt { let prompt = ReedlinePrompt {
@ -156,7 +154,7 @@ impl Command for Input {
} }
Ok(_) => continue, Ok(_) => continue,
Err(event_error) => { Err(event_error) => {
crossterm::terminal::disable_raw_mode().map_err(&from_io_error)?; let from_io_error = IoError::factory(call.head, None);
return Err(from_io_error(event_error).into()); return Err(from_io_error(event_error).into());
} }
} }
@ -186,7 +184,7 @@ impl Command for Input {
}, },
Example { Example {
description: "Get input from the user with history, and assign to a variable", description: "Get input from the user with history, and assign to a variable",
example: "let user_input = ([past,command,entries] | input)", example: "let user_input = ([past,command,entries] | input )",
result: None, result: None,
}, },
Example { Example {