use std::borrow::Cow; use std::path::Path; use inquire::error::InquireError; use inquire::{Confirm, Editor, MultiSelect, Password, Select, Text}; use roneo_core::config::ChoiceSpec; use roneo_core::{OverwriteChoice, PromptRequest, Prompter, Result, RoneoError}; use serde_json::Value as JsonValue; use crate::style; #[derive(Debug, Default)] pub struct TerminalPrompter; impl Prompter for TerminalPrompter { fn prompt_string(&self, req: &PromptRequest) -> Result { let msg = format_prompt_message(req); if req.secret { let mut prompt = Password::new(&msg).without_confirmation(); if let Some(help) = &req.placeholder { prompt = prompt.with_help_message(help); } return prompt.prompt().map_err(map_inquire_err); } if req.multiline { let mut editor = Editor::new(&msg); if let Some(def) = &req.default_display { editor = editor.with_predefined_text(def); } return editor.prompt().map_err(map_inquire_err); } let mut prompt = Text::new(&msg); if let Some(def) = &req.default_display { prompt = prompt.with_default(def); } if let Some(ph) = &req.placeholder { prompt = prompt.with_placeholder(ph); } prompt.prompt().map_err(map_inquire_err) } fn prompt_bool(&self, req: &PromptRequest, default: bool) -> Result { let msg = format_prompt_message(req); Confirm::new(&msg) .with_default(default) .prompt() .map_err(map_inquire_err) } fn prompt_select( &self, req: &PromptRequest, choices: &[ChoiceSpec], default_idx: Option, ) -> Result { let msg = format_prompt_message(req); let labels: Vec<&str> = choices.iter().map(|c| c.label.as_str()).collect(); let mut prompt = Select::new(&msg, labels); if let Some(idx) = default_idx { prompt = prompt.with_starting_cursor(idx); } let picked_label = prompt.prompt().map_err(map_inquire_err)?; let choice = choices .iter() .find(|c| c.label == picked_label) .ok_or_else(|| RoneoError::InvalidAnswer { question: req.name.clone(), message: "Selected choice not found".into(), })?; Ok(choice.value.clone()) } fn prompt_multiselect( &self, req: &PromptRequest, choices: &[ChoiceSpec], defaults: &[usize], ) -> Result> { let msg = format_prompt_message(req); let labels: Vec<&str> = choices.iter().map(|c| c.label.as_str()).collect(); let prompt = MultiSelect::new(&msg, labels).with_default(defaults); let picked_labels = prompt.prompt().map_err(map_inquire_err)?; Ok(picked_labels .into_iter() .filter_map(|label| { choices .iter() .find(|c| c.label == label) .map(|c| c.value.clone()) }) .collect()) } fn show_validation_error(&self, question: &str, error: &str) { eprintln!( " {} {}: {}", style::red_bold("✗"), style::bold(question), style::red(error) ); } fn confirm_overwrite(&self, path: &Path) -> Result { let prompt = format!("Overwrite '{}'?", path.display()); let options = vec!["No (skip)", "Yes", "Yes to all", "Quit"]; let picked = Select::new(&prompt, options) .with_starting_cursor(0) .prompt() .map_err(map_inquire_err)?; Ok(match picked { "Yes" => OverwriteChoice::Yes, "Yes to all" => OverwriteChoice::All, "Quit" => OverwriteChoice::Quit, _ => OverwriteChoice::No, }) } } fn format_prompt_message(req: &PromptRequest) -> Cow<'_, str> { match req .qmark .as_deref() .map(str::trim) .filter(|s| !s.is_empty()) { Some(qmark) => Cow::Owned(format!("{qmark} {}", req.message)), None => Cow::Borrowed(&req.message), } } fn map_inquire_err(err: InquireError) -> RoneoError { match err { InquireError::OperationCanceled | InquireError::OperationInterrupted => { RoneoError::UserAborted } other => RoneoError::InvalidAnswer { question: "prompt".into(), message: other.to_string(), }, } }