Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add branch names to rev log #713

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion src/components/commitlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ use crate::{
ui::style::{SharedTheme, Theme},
};
use anyhow::Result;
use asyncgit::sync::Tags;
use asyncgit::sync::{BranchInfo, CommitId, Tags};
use chrono::{DateTime, Local};
use crossterm::event::Event;
use std::collections::BTreeMap;
use std::{
borrow::Cow, cell::Cell, cmp, convert::TryFrom, time::Instant,
};
Expand All @@ -26,11 +27,14 @@ use tui::{

const ELEMENTS_PER_LINE: usize = 10;

type Branches = BTreeMap<CommitId, Vec<BranchInfo>>;

///
pub struct CommitList {
title: String,
selection: usize,
branch: Option<String>,
local_branch_list: Option<Branches>,
count_total: usize,
items: ItemBatch,
scroll_state: (Instant, f32),
Expand All @@ -52,6 +56,7 @@ impl CommitList {
items: ItemBatch::default(),
selection: 0,
branch: None,
local_branch_list: None,
count_total: 0,
scroll_state: (Instant::now(), 0_f32),
tags: None,
Expand All @@ -73,6 +78,27 @@ impl CommitList {
self.branch = name;
}

///
pub fn set_local_branch_list(
&mut self,
branch_list: Option<Vec<BranchInfo>>,
) {
let mut res = Branches::new();
let mut adder = |key: CommitId, value: BranchInfo| {
if let Some(key) = res.get_mut(&key) {
key.push(value)
} else {
res.insert(key, vec![value]);
}
};

for branch in branch_list.unwrap_or_default() {
adder(branch.top_commit, branch);
}

self.local_branch_list = Some(res);
}

///
pub const fn selection(&self) -> usize {
self.selection
Expand Down Expand Up @@ -192,6 +218,7 @@ impl CommitList {
e: &'a LogEntry,
selected: bool,
tags: Option<String>,
local_branches: Option<String>,
theme: &Theme,
width: usize,
now: DateTime<Local>,
Expand Down Expand Up @@ -231,6 +258,16 @@ impl CommitList {

txt.push(splitter.clone());

// branches
txt.push(Span::styled(
Cow::from(if let Some(branches) = local_branches {
format!(" {}", branches)
} else {
String::from("")
}),
theme.branch_in_log(selected),
));

// commit tags
txt.push(Span::styled(
Cow::from(if let Some(tags) = tags {
Expand Down Expand Up @@ -270,10 +307,22 @@ impl CommitList {
.as_ref()
.and_then(|t| t.get(&e.id))
.map(|tags| tags.join(" "));
let branches = self
.local_branch_list
.as_ref()
.and_then(|b| b.get(&e.id))
.map(|branches| {
branches
.iter()
.map(|b| b.name.to_string())
.collect::<Vec<String>>()
.join(" ")
});
txt.push(Self::get_entry_to_add(
e,
idx + self.scroll_top.get() == selection,
tags,
branches,
&self.theme,
width,
now,
Expand Down
8 changes: 7 additions & 1 deletion src/tabs/revlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
use anyhow::Result;
use asyncgit::{
cached,
sync::{self, CommitId},
sync::{self, get_branches_info, CommitId},
AsyncLog, AsyncNotification, AsyncTags, FetchStatus, CWD,
};
use crossbeam_channel::Sender;
Expand Down Expand Up @@ -97,6 +97,12 @@ impl Revlog {
self.branch_name.lookup().map(Some).unwrap_or(None),
);

self.list.set_local_branch_list(
get_branches_info(CWD, true)
.map(Some)
.unwrap_or(None),
);

if self.commit_details.is_visible() {
let commit = self.selected_commit();
let tags = self.selected_commit_tags(&commit);
Expand Down
14 changes: 14 additions & 0 deletions src/ui/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub struct Theme {
#[serde(with = "Color")]
commit_author: Color,
#[serde(with = "Color")]
commit_local_branch: Color,
#[serde(with = "Color")]
danger_fg: Color,
#[serde(with = "Color")]
push_gauge_bg: Color,
Expand Down Expand Up @@ -90,6 +92,17 @@ impl Theme {
}
}

pub fn branch_in_log(&self, selected: bool) -> Style {
Style::default()
.fg(self.commit_local_branch)
.add_modifier(Modifier::BOLD)
.bg(if selected {
self.selection_bg
} else {
Color::Reset
})
}

pub fn tab(&self, selected: bool) -> Style {
if selected {
self.text(true, false)
Expand Down Expand Up @@ -320,6 +333,7 @@ impl Default for Theme {
commit_hash: Color::Magenta,
commit_time: Color::LightCyan,
commit_author: Color::Green,
commit_local_branch: Color::LightGreen,
danger_fg: Color::Red,
push_gauge_bg: Color::Blue,
push_gauge_fg: Color::Reset,
Expand Down