ytil_tui/
git_branch.rs

1use std::fmt::Display;
2use std::ops::Deref;
3
4use color_eyre::owo_colors::OwoColorize as _;
5use ytil_git::branch::Branch;
6
7/// A wrapper around [`Branch`] for display purposes.
8///
9/// # Rationale
10/// Provides a custom [`Display`] implementation for branch selection UI.
11struct RenderableBranch(pub Branch);
12
13impl Deref for RenderableBranch {
14    type Target = Branch;
15
16    fn deref(&self) -> &Self::Target {
17        &self.0
18    }
19}
20
21impl Display for RenderableBranch {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        let styled_date_time = format!("({})", self.committer_date_time());
24        write!(f, "{} {}", self.name(), styled_date_time.blue())
25    }
26}
27
28/// Prompts the user to select a branch from all available branches.
29///
30/// # Errors
31/// - If [`ytil_git::branch::get_all_no_redundant`] fails.
32/// - If [`crate::minimal_select`] fails.
33pub fn select() -> color_eyre::Result<Option<Branch>> {
34    let branches = ytil_git::branch::get_all_no_redundant()?;
35
36    let Some(branch) = crate::minimal_select(branches.into_iter().map(RenderableBranch).collect())? else {
37        return Ok(None);
38    };
39
40    Ok(Some(branch.0))
41}