use crate::selectors::error::{SelectorError, SelectorResult};
pub struct ConfirmationService;
impl ConfirmationService {
fn confirm_impl(message: &str, default: bool) -> SelectorResult<bool> {
if !atty::is(atty::Stream::Stdin) {
return Ok(default);
}
inquire::Confirm::new(message)
.with_default(default)
.prompt()
.map_err(|e| {
let msg = e.to_string();
if msg.contains("canceled") || msg.contains("cancelled") {
SelectorError::Cancelled
} else {
SelectorError::Failed(format!("Confirmation failed: {}", e))
}
})
}
pub fn confirm(message: &str, default: bool) -> SelectorResult<bool> {
Self::confirm_impl(message, default)
}
pub fn confirm_deletion(item_name: &str, item_type: &str) -> SelectorResult<bool> {
Self::confirm_impl(&format!("Delete '{}' {}?", item_name, item_type), false)
}
pub fn confirm_overwrite(item_name: &str, item_type: &str) -> SelectorResult<bool> {
Self::confirm_impl(
&format!("{} '{}' already exists. Overwrite?", item_type, item_name),
false,
)
}
pub fn confirm_action(action_description: &str) -> SelectorResult<bool> {
Self::confirm_impl(action_description, false)
}
}