Skip to content

Commit 0f9389c

Browse files
authored
display commit description in addition to hash in file view (#1380)
1 parent 45d850e commit 0f9389c

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

src/components/revision_files.rs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ use crate::{
1111
AsyncAppNotification, AsyncNotification,
1212
};
1313
use anyhow::Result;
14-
use asyncgit::sync::{self, CommitId, RepoPathRef, TreeFile};
14+
use asyncgit::sync::{
15+
self, get_commit_info, CommitId, CommitInfo, RepoPathRef,
16+
TreeFile,
17+
};
1518
use crossbeam_channel::Sender;
1619
use crossterm::event::Event;
1720
use filetreelist::{FileTree, FileTreeItem};
21+
use std::fmt::Write;
1822
use std::{
1923
collections::BTreeSet,
2024
convert::From,
@@ -27,6 +31,8 @@ use tui::{
2731
widgets::{Block, Borders},
2832
Frame,
2933
};
34+
use unicode_truncate::UnicodeTruncateStr;
35+
use unicode_width::UnicodeWidthStr;
3036

3137
enum Focus {
3238
Tree,
@@ -43,7 +49,7 @@ pub struct RevisionFilesComponent {
4349
tree: FileTree,
4450
scroll: VerticalScroll,
4551
visible: bool,
46-
revision: Option<CommitId>,
52+
revision: Option<CommitInfo>,
4753
focus: Focus,
4854
key_config: SharedKeyConfig,
4955
}
@@ -82,23 +88,24 @@ impl RevisionFilesComponent {
8288
self.show()?;
8389

8490
let same_id =
85-
self.revision.map(|c| c == commit).unwrap_or_default();
91+
self.revision.as_ref().map_or(false, |c| c.id == commit);
8692
if !same_id {
8793
self.files =
8894
sync::tree_files(&self.repo.borrow(), commit)?;
8995
let filenames: Vec<&Path> =
9096
self.files.iter().map(|f| f.path.as_path()).collect();
9197
self.tree = FileTree::new(&filenames, &BTreeSet::new())?;
9298
self.tree.collapse_but_root();
93-
self.revision = Some(commit);
99+
self.revision =
100+
Some(get_commit_info(&self.repo.borrow(), &commit)?);
94101
}
95102

96103
Ok(())
97104
}
98105

99106
///
100-
pub const fn revision(&self) -> Option<CommitId> {
101-
self.revision
107+
pub const fn revision(&self) -> Option<&CommitInfo> {
108+
self.revision.as_ref()
102109
}
103110

104111
///
@@ -161,7 +168,7 @@ impl RevisionFilesComponent {
161168
self.queue.push(InternalEvent::OpenPopup(
162169
StackablePopupOpen::BlameFile(BlameFileOpen {
163170
file_path: path,
164-
commit_id: self.revision,
171+
commit_id: self.revision.as_ref().map(|c| c.id),
165172
selection: None,
166173
}),
167174
));
@@ -260,12 +267,7 @@ impl RevisionFilesComponent {
260267

261268
let is_tree_focused = matches!(self.focus, Focus::Tree);
262269

263-
let title = format!(
264-
"Files at [{}]",
265-
self.revision
266-
.map(|c| c.get_short_string())
267-
.unwrap_or_default(),
268-
);
270+
let title = self.title_within(tree_width);
269271
ui::draw_list_block(
270272
f,
271273
area,
@@ -283,6 +285,40 @@ impl RevisionFilesComponent {
283285
self.scroll.draw(f, area, &self.theme);
284286
}
285287
}
288+
289+
fn title_within(&self, tree_width: usize) -> String {
290+
let mut title = String::from("Files at");
291+
let message = self.revision.as_ref().and_then(|c| {
292+
let _ = write!(title, " {{{}}}", c.id.get_short_string());
293+
294+
c.message.lines().next()
295+
});
296+
297+
if let Some(message) = message {
298+
const ELLIPSIS: char = '\u{2026}'; // …
299+
300+
let available = tree_width
301+
.saturating_sub(title.width())
302+
.saturating_sub(
303+
2 /* frame end corners */ + 1 /* space */ + 2, /* square brackets */
304+
);
305+
306+
if message.width() <= available {
307+
let _ = write!(title, " [{}]", message);
308+
} else if available > 1 {
309+
let _ = write!(
310+
title,
311+
" [{}{}]",
312+
message.unicode_truncate(available - 1).0,
313+
ELLIPSIS
314+
);
315+
} else {
316+
title.push(ELLIPSIS);
317+
}
318+
}
319+
320+
title
321+
}
286322
}
287323

288324
impl DrawableComponent for RevisionFilesComponent {

src/components/revision_files_popup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl RevisionFilesPopup {
9595
if let Some(revision) = self.files.revision() {
9696
self.queue.push(InternalEvent::PopupStackPush(
9797
StackablePopupOpen::FileTree(FileTreeOpen {
98-
commit_id: revision,
98+
commit_id: revision.id,
9999
selection: self.files.selection(),
100100
}),
101101
));

0 commit comments

Comments
 (0)