Skip to content

Commit 506fbca

Browse files
committed
Add branch names to rev log
1 parent 79c17f4 commit 506fbca

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

src/components/commitlist.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ use crate::{
1010
ui::style::{SharedTheme, Theme},
1111
};
1212
use anyhow::Result;
13-
use asyncgit::sync::Tags;
13+
use asyncgit::sync::{BranchInfo, CommitId, Tags};
1414
use chrono::{DateTime, Local};
1515
use crossterm::event::Event;
16+
use std::collections::BTreeMap;
1617
use std::{
1718
borrow::Cow, cell::Cell, cmp, convert::TryFrom, time::Instant,
1819
};
@@ -26,11 +27,14 @@ use tui::{
2627

2728
const ELEMENTS_PER_LINE: usize = 10;
2829

30+
type Branches = BTreeMap<CommitId, Vec<BranchInfo>>;
31+
2932
///
3033
pub struct CommitList {
3134
title: String,
3235
selection: usize,
3336
branch: Option<String>,
37+
local_branch_list: Option<Branches>,
3438
count_total: usize,
3539
items: ItemBatch,
3640
scroll_state: (Instant, f32),
@@ -52,6 +56,7 @@ impl CommitList {
5256
items: ItemBatch::default(),
5357
selection: 0,
5458
branch: None,
59+
local_branch_list: None,
5560
count_total: 0,
5661
scroll_state: (Instant::now(), 0_f32),
5762
tags: None,
@@ -73,6 +78,27 @@ impl CommitList {
7378
self.branch = name;
7479
}
7580

81+
///
82+
pub fn set_local_branch_list(
83+
&mut self,
84+
branch_list: Option<Vec<BranchInfo>>,
85+
) {
86+
let mut res = Branches::new();
87+
let mut adder = |key: CommitId, value: BranchInfo| {
88+
if let Some(key) = res.get_mut(&key) {
89+
key.push(value)
90+
} else {
91+
res.insert(key, vec![value]);
92+
}
93+
};
94+
95+
for branch in branch_list.unwrap_or_default() {
96+
adder(branch.top_commit, branch);
97+
}
98+
99+
self.local_branch_list = Some(res);
100+
}
101+
76102
///
77103
pub const fn selection(&self) -> usize {
78104
self.selection
@@ -192,6 +218,7 @@ impl CommitList {
192218
e: &'a LogEntry,
193219
selected: bool,
194220
tags: Option<String>,
221+
local_branches: Option<String>,
195222
theme: &Theme,
196223
width: usize,
197224
now: DateTime<Local>,
@@ -231,6 +258,16 @@ impl CommitList {
231258

232259
txt.push(splitter.clone());
233260

261+
// branches
262+
txt.push(Span::styled(
263+
Cow::from(if let Some(branches) = local_branches {
264+
format!(" {}", branches)
265+
} else {
266+
String::from("")
267+
}),
268+
theme.branch_in_log(selected),
269+
));
270+
234271
// commit tags
235272
txt.push(Span::styled(
236273
Cow::from(if let Some(tags) = tags {
@@ -270,10 +307,22 @@ impl CommitList {
270307
.as_ref()
271308
.and_then(|t| t.get(&e.id))
272309
.map(|tags| tags.join(" "));
310+
let branches = self
311+
.local_branch_list
312+
.as_ref()
313+
.and_then(|b| b.get(&e.id))
314+
.map(|branches| {
315+
branches
316+
.iter()
317+
.map(|b| b.name.to_string())
318+
.collect::<Vec<String>>()
319+
.join(" ")
320+
});
273321
txt.push(Self::get_entry_to_add(
274322
e,
275323
idx + self.scroll_top.get() == selection,
276324
tags,
325+
branches,
277326
&self.theme,
278327
width,
279328
now,

src/tabs/revlog.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
use anyhow::Result;
1313
use asyncgit::{
1414
cached,
15-
sync::{self, CommitId},
15+
sync::{self, get_branches_info, CommitId},
1616
AsyncLog, AsyncNotification, AsyncTags, FetchStatus, CWD,
1717
};
1818
use crossbeam_channel::Sender;
@@ -97,6 +97,12 @@ impl Revlog {
9797
self.branch_name.lookup().map(Some).unwrap_or(None),
9898
);
9999

100+
self.list.set_local_branch_list(
101+
get_branches_info(CWD, true)
102+
.map(Some)
103+
.unwrap_or(None),
104+
);
105+
100106
if self.commit_details.is_visible() {
101107
let commit = self.selected_commit();
102108
let tags = self.selected_commit_tags(&commit);

src/ui/style.rs

+14
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub struct Theme {
4848
#[serde(with = "Color")]
4949
commit_author: Color,
5050
#[serde(with = "Color")]
51+
commit_local_branch: Color,
52+
#[serde(with = "Color")]
5153
danger_fg: Color,
5254
#[serde(with = "Color")]
5355
push_gauge_bg: Color,
@@ -90,6 +92,17 @@ impl Theme {
9092
}
9193
}
9294

95+
pub fn branch_in_log(&self, selected: bool) -> Style {
96+
Style::default()
97+
.fg(self.commit_local_branch)
98+
.add_modifier(Modifier::BOLD)
99+
.bg(if selected {
100+
self.selection_bg
101+
} else {
102+
Color::Reset
103+
})
104+
}
105+
93106
pub fn tab(&self, selected: bool) -> Style {
94107
if selected {
95108
self.text(true, false)
@@ -320,6 +333,7 @@ impl Default for Theme {
320333
commit_hash: Color::Magenta,
321334
commit_time: Color::LightCyan,
322335
commit_author: Color::Green,
336+
commit_local_branch: Color::LightGreen,
323337
danger_fg: Color::Red,
324338
push_gauge_bg: Color::Blue,
325339
push_gauge_fg: Color::Reset,

0 commit comments

Comments
 (0)