Skip to content

Commit 65445eb

Browse files
author
Stephan Dilly
committed
support tree-view blaming (see #714)
1 parent 4591fbb commit 65445eb

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

Diff for: filetree/src/filetree.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
error::Result, filetreeitems::FileTreeItems,
3-
tree_iter::TreeIterator,
3+
tree_iter::TreeIterator, TreeItemInfo,
44
};
55
use std::{collections::BTreeSet, usize};
66

@@ -77,6 +77,18 @@ impl FileTree {
7777
self.visual_selection.as_ref()
7878
}
7979

80+
///
81+
pub fn selected_file(&self) -> Option<&TreeItemInfo> {
82+
self.selection.and_then(|index| {
83+
let item = &self.items.tree_items[index];
84+
if item.kind().is_path() {
85+
None
86+
} else {
87+
Some(item.info())
88+
}
89+
})
90+
}
91+
8092
///
8193
pub fn collapse_recursive(&mut self) {
8294
if let Some(selection) = self.selection {

Diff for: src/components/revision_files.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
};
77
use crate::{
88
keys::SharedKeyConfig,
9-
queue::Queue,
9+
queue::{InternalEvent, Queue},
1010
strings::{self, order},
1111
ui::{self, style::SharedTheme},
1212
};
@@ -27,6 +27,7 @@ const FOLDER_ICON_EXPANDED: &str = "\u{25be}"; //▾
2727
const EMPTY_STR: &str = "";
2828

2929
pub struct RevisionFilesComponent {
30+
queue: Queue,
3031
title: String,
3132
theme: SharedTheme,
3233
files: Vec<TreeFile>,
@@ -41,12 +42,13 @@ pub struct RevisionFilesComponent {
4142
impl RevisionFilesComponent {
4243
///
4344
pub fn new(
44-
_queue: &Queue,
45+
queue: &Queue,
4546
_sender: &Sender<AsyncNotification>,
4647
theme: SharedTheme,
4748
key_config: SharedKeyConfig,
4849
) -> Self {
4950
Self {
51+
queue: queue.clone(),
5052
title: String::new(),
5153
tree: FileTree::default(),
5254
theme,
@@ -109,6 +111,20 @@ impl RevisionFilesComponent {
109111
let path = format!("{}{}{}", indent_str, path_arrow, path);
110112
Span::styled(path, theme.file_tree_item(is_path, selected))
111113
}
114+
115+
fn blame(&self) -> bool {
116+
self.tree.selected_file().map_or(false, |file| {
117+
self.queue.borrow_mut().push_back(
118+
InternalEvent::BlameFile(
119+
file.full_path()
120+
.strip_prefix("./")
121+
.unwrap_or_default()
122+
.to_string(),
123+
),
124+
);
125+
true
126+
})
127+
}
112128
}
113129

114130
impl DrawableComponent for RevisionFilesComponent {
@@ -178,6 +194,15 @@ impl Component for RevisionFilesComponent {
178194
.order(1),
179195
);
180196

197+
out.push(
198+
CommandInfo::new(
199+
strings::commands::blame_file(&self.key_config),
200+
self.tree.selected_file().is_some(),
201+
true,
202+
)
203+
.order(order::NAV),
204+
);
205+
181206
tree_nav_cmds(&self.tree, &self.key_config, out);
182207
}
183208

@@ -193,6 +218,13 @@ impl Component for RevisionFilesComponent {
193218
let consumed = if key == self.key_config.exit_popup {
194219
self.hide();
195220
true
221+
} else if key == self.key_config.blame {
222+
if self.blame() {
223+
self.hide();
224+
true
225+
} else {
226+
false
227+
}
196228
} else {
197229
tree_nav(&mut self.tree, &self.key_config, key)
198230
};

0 commit comments

Comments
 (0)