use crossterm::event::{KeyCode, KeyEvent};
use crate::state::{AppState, PackageItem};
pub(super) fn handle_confirm_install(
ke: KeyEvent,
app: &mut AppState,
items: &[PackageItem],
) -> bool {
match ke.code {
KeyCode::Esc => {
app.modal = crate::state::Modal::None;
}
KeyCode::Enter => {
let new_modal = handle_confirm_install_enter(
&mut app.refresh_installed_until,
&mut app.next_installed_refresh_at,
&mut app.pending_install_names,
app.dry_run,
items,
);
app.modal = new_modal;
}
KeyCode::Char('s' | 'S') => {
let new_modal = handle_confirm_install_scan(&mut app.pending_install_names, items);
app.modal = new_modal;
}
_ => {}
}
false
}
pub(super) fn handle_confirm_remove(
ke: KeyEvent,
app: &mut AppState,
items: &[PackageItem],
) -> bool {
match ke.code {
KeyCode::Esc | KeyCode::Enter => {
app.modal = crate::state::Modal::None;
}
KeyCode::Char('y' | 'Y') => {
let names: Vec<String> = items.iter().map(|p| p.name.clone()).collect();
crate::install::start_integrated_remove_all(
app,
&names,
app.dry_run,
app.remove_cascade_mode,
);
app.remove_list
.retain(|p| !names.iter().any(|n| n == &p.name));
app.remove_state.select(None);
if !app.dry_run {
app.refresh_installed_until =
Some(std::time::Instant::now() + std::time::Duration::from_secs(8));
app.next_installed_refresh_at = None;
app.pending_remove_names = Some(names);
}
app.modal = crate::state::Modal::None;
}
_ => {}
}
false
}
fn handle_confirm_install_enter(
refresh_installed_until: &mut Option<std::time::Instant>,
next_installed_refresh_at: &mut Option<std::time::Instant>,
pending_install_names: &mut Option<Vec<String>>,
dry_run: bool,
items: &[PackageItem],
) -> crate::state::Modal {
let list = items.to_vec();
if list.len() <= 1 {
if let Some(it) = list.first() {
crate::install::spawn_install(it, None, dry_run);
if !dry_run {
*refresh_installed_until =
Some(std::time::Instant::now() + std::time::Duration::from_secs(12));
*next_installed_refresh_at = None;
*pending_install_names = Some(vec![it.name.clone()]);
}
}
} else {
crate::install::spawn_install_all(&list, dry_run);
if !dry_run {
*refresh_installed_until =
Some(std::time::Instant::now() + std::time::Duration::from_secs(12));
*next_installed_refresh_at = None;
*pending_install_names = Some(list.iter().map(|p| p.name.clone()).collect());
}
}
crate::state::Modal::None
}
fn handle_confirm_install_scan(
pending_install_names: &mut Option<Vec<String>>,
items: &[PackageItem],
) -> crate::state::Modal {
let list = items.to_vec();
let mut names: Vec<String> = Vec::new();
for it in &list {
if matches!(it.source, crate::state::Source::Aur) {
names.push(it.name.clone());
}
}
if names.is_empty() {
crate::state::Modal::Alert {
message: "No AUR packages selected to scan.\nSelect AUR results or add AUR packages to the Install list, then press 's'.".into(),
}
} else {
*pending_install_names = Some(names);
let prefs = crate::theme::settings();
crate::state::Modal::ScanConfig {
do_clamav: prefs.scan_do_clamav,
do_trivy: prefs.scan_do_trivy,
do_semgrep: prefs.scan_do_semgrep,
do_shellcheck: prefs.scan_do_shellcheck,
do_virustotal: prefs.scan_do_virustotal,
do_custom: prefs.scan_do_custom,
do_sleuth: prefs.scan_do_sleuth,
cursor: 0,
}
}
}