From 506fbca2916a7325f322057e8d412bd8f30f3f58 Mon Sep 17 00:00:00 2001 From: Per Westerlund Date: Sun, 16 May 2021 17:35:20 +0200 Subject: [PATCH] Add branch names to rev log --- src/components/commitlist.rs | 51 +++++++++++++++++++++++++++++++++++- src/tabs/revlog.rs | 8 +++++- src/ui/style.rs | 14 ++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/components/commitlist.rs b/src/components/commitlist.rs index f5f0e02da4..10ab86701f 100644 --- a/src/components/commitlist.rs +++ b/src/components/commitlist.rs @@ -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, }; @@ -26,11 +27,14 @@ use tui::{ const ELEMENTS_PER_LINE: usize = 10; +type Branches = BTreeMap>; + /// pub struct CommitList { title: String, selection: usize, branch: Option, + local_branch_list: Option, count_total: usize, items: ItemBatch, scroll_state: (Instant, f32), @@ -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, @@ -73,6 +78,27 @@ impl CommitList { self.branch = name; } + /// + pub fn set_local_branch_list( + &mut self, + branch_list: Option>, + ) { + 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 @@ -192,6 +218,7 @@ impl CommitList { e: &'a LogEntry, selected: bool, tags: Option, + local_branches: Option, theme: &Theme, width: usize, now: DateTime, @@ -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 { @@ -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::>() + .join(" ") + }); txt.push(Self::get_entry_to_add( e, idx + self.scroll_top.get() == selection, tags, + branches, &self.theme, width, now, diff --git a/src/tabs/revlog.rs b/src/tabs/revlog.rs index 89fdb0de38..59570a81ca 100644 --- a/src/tabs/revlog.rs +++ b/src/tabs/revlog.rs @@ -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; @@ -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); diff --git a/src/ui/style.rs b/src/ui/style.rs index 747fd1766e..129038d029 100644 --- a/src/ui/style.rs +++ b/src/ui/style.rs @@ -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, @@ -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) @@ -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,