indicate if branch has an upstream and some cleanup

This commit is contained in:
Stephan Dilly 2020-11-01 19:43:19 +01:00
parent c4fdbf7aba
commit 88813381da
3 changed files with 56 additions and 62 deletions

View file

@ -43,6 +43,8 @@ pub struct BranchForDisplay {
pub top_commit: CommitId,
///
pub is_head: bool,
///
pub has_upstream: bool,
}
/// Used to return only the nessessary information for displaying a branch
@ -67,6 +69,7 @@ pub fn get_branches_to_display(
)?,
top_commit: top_commit.id().into(),
is_head: branch.is_head(),
has_upstream: branch.upstream().is_ok(),
})
})
.filter_map(Result::ok)

View file

@ -228,7 +228,7 @@ impl SelectBranchComponent {
}
/// Get all the names of the branches in the repo
pub fn get_branch_names() -> Result<Vec<BranchForDisplay>> {
get_branches_to_display(CWD).map_err(anyhow::Error::new)
Ok(get_branches_to_display(CWD)?)
}
///
@ -325,70 +325,47 @@ impl SelectBranchComponent {
branch_name += "...";
}
let selected =
self.selection as usize - self.scroll_top.get() == i;
let is_head_str =
if displaybranch.is_head { "*" } else { " " };
let has_upstream_str = if displaybranch.has_upstream {
"\u{2191}"
} else {
" "
};
txt.push(Spans::from(
if self.selection as usize - self.scroll_top.get()
== i
{
vec![
Span::styled(
format!("{} ", is_head_str),
theme.commit_author(true),
),
Span::styled(
format!(
">{:w$} ",
branch_name,
w = branch_name_length
),
theme.commit_author(true),
),
Span::styled(
format!(
"{} ",
displaybranch
.top_commit
.get_short_string()
),
theme.commit_hash(true),
),
Span::styled(
commit_message.to_string(),
theme.text(true, true),
),
]
} else {
vec![
Span::styled(
format!("{} ", is_head_str),
theme.commit_author(false),
),
Span::styled(
format!(
" {:w$} ",
branch_name,
w = branch_name_length
),
theme.commit_author(false),
),
Span::styled(
format!(
"{} ",
displaybranch
.top_commit
.get_short_string()
),
theme.commit_hash(false),
),
Span::styled(
commit_message.to_string(),
theme.text(true, false),
),
]
},
));
let span_prefix = Span::styled(
format!("{}{} ", is_head_str, has_upstream_str),
theme.commit_author(selected),
);
let span_hash = Span::styled(
format!(
"{} ",
displaybranch.top_commit.get_short_string()
),
theme.commit_hash(selected),
);
let span_msg = Span::styled(
commit_message.to_string(),
theme.text(true, selected),
);
let span_name = Span::styled(
format!(
"{:w$} ",
branch_name,
w = branch_name_length
),
theme.branch(selected, displaybranch.is_head),
);
txt.push(Spans::from(vec![
span_prefix,
span_name,
span_hash,
span_msg,
]));
}
Ok(Text::from(txt))

View file

@ -70,6 +70,20 @@ impl Theme {
}
}
pub fn branch(&self, selected: bool, head: bool) -> Style {
let branch = if head {
Style::default().add_modifier(Modifier::BOLD)
} else {
Style::default()
};
if selected {
branch.patch(Style::default().bg(self.selection_bg))
} else {
branch
}
}
pub fn tab(&self, selected: bool) -> Style {
if selected {
self.text(true, false)