1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright 2021 Aurélien Gâteau <mail@agateau.com>
 *
 * This file is part of git-bonsai.
 *
 * Git-bonsai is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
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())
    }
}