use crate::appui::{AppUi, BranchToDeleteInfo};
use crate::tui;
pub struct InteractiveAppUi;
fn format_branch_info(branch_info: &BranchToDeleteInfo) -> String {
let container_str = branch_info
.contained_in
.iter()
.map(|x| format!(" - {}", x))
.collect::<Vec<String>>()
.join("\n");
format!("{}, contained in:\n{} \n", branch_info.name, container_str)
}
impl AppUi for InteractiveAppUi {
fn log_info(&self, msg: &str) {
tui::log_info(msg);
}
fn log_warning(&self, msg: &str) {
tui::log_warning(msg);
}
fn log_error(&self, msg: &str) {
tui::log_error(msg);
}
fn select_branches_to_delete(
&self,
branch_infos: &[BranchToDeleteInfo],
) -> Vec<BranchToDeleteInfo> {
let select_items: Vec<String> = branch_infos
.iter()
.map(format_branch_info)
.collect::<Vec<String>>();
let selections = tui::select("Select branches to delete", &select_items);
selections
.iter()
.map(|&x| branch_infos[x].clone())
.collect::<Vec<BranchToDeleteInfo>>()
}
fn select_identical_branches_to_delete(&self, branches: &[String]) -> Vec<String> {
let mut items = branches.to_vec();
items.sort();
let selections = tui::select(
"These branches point to the same commit, which is contained in another branch,\
so it is safe to delete them all.\n\
Select branches to delete",
&items,
);
selections
.iter()
.map(|&x| items[x].clone())
.collect::<Vec<String>>()
}
fn select_identical_branches_to_delete_keep_one(&self, branches: &[String]) -> Vec<String> {
let mut items = branches.to_vec();
items.sort();
let mut selections: Vec<usize>;
println!(
"These branches point to the same commit, but no other branch contains this commit, \
so you can delete all of them but one.\n"
);
loop {
selections = tui::select("Select branches to delete", &items);
if selections.len() == items.len() {
self.log_error("You must leave at least one branch unchecked.");
} else {
break;
}
}
selections
.iter()
.map(|&x| items[x].clone())
.collect::<Vec<String>>()
}
fn select_default_branch(&self, branches: &[String]) -> Option<String> {
let mut items = branches.to_vec();
items.sort();
tui::select_one("Select the branch to use as the default branch", &items)
.map(|x| items[x].clone())
}
}