support tree-view blaming (see #714)

This commit is contained in:
Stephan Dilly 2021-05-21 17:42:27 +02:00
parent 4591fbb965
commit 65445eba5e
2 changed files with 47 additions and 3 deletions

View file

@ -1,6 +1,6 @@
use crate::{
error::Result, filetreeitems::FileTreeItems,
tree_iter::TreeIterator,
tree_iter::TreeIterator, TreeItemInfo,
};
use std::{collections::BTreeSet, usize};
@ -77,6 +77,18 @@ impl FileTree {
self.visual_selection.as_ref()
}
///
pub fn selected_file(&self) -> Option<&TreeItemInfo> {
self.selection.and_then(|index| {
let item = &self.items.tree_items[index];
if item.kind().is_path() {
None
} else {
Some(item.info())
}
})
}
///
pub fn collapse_recursive(&mut self) {
if let Some(selection) = self.selection {

View file

@ -6,7 +6,7 @@ use super::{
};
use crate::{
keys::SharedKeyConfig,
queue::Queue,
queue::{InternalEvent, Queue},
strings::{self, order},
ui::{self, style::SharedTheme},
};
@ -27,6 +27,7 @@ const FOLDER_ICON_EXPANDED: &str = "\u{25be}"; //▾
const EMPTY_STR: &str = "";
pub struct RevisionFilesComponent {
queue: Queue,
title: String,
theme: SharedTheme,
files: Vec<TreeFile>,
@ -41,12 +42,13 @@ pub struct RevisionFilesComponent {
impl RevisionFilesComponent {
///
pub fn new(
_queue: &Queue,
queue: &Queue,
_sender: &Sender<AsyncNotification>,
theme: SharedTheme,
key_config: SharedKeyConfig,
) -> Self {
Self {
queue: queue.clone(),
title: String::new(),
tree: FileTree::default(),
theme,
@ -109,6 +111,20 @@ impl RevisionFilesComponent {
let path = format!("{}{}{}", indent_str, path_arrow, path);
Span::styled(path, theme.file_tree_item(is_path, selected))
}
fn blame(&self) -> bool {
self.tree.selected_file().map_or(false, |file| {
self.queue.borrow_mut().push_back(
InternalEvent::BlameFile(
file.full_path()
.strip_prefix("./")
.unwrap_or_default()
.to_string(),
),
);
true
})
}
}
impl DrawableComponent for RevisionFilesComponent {
@ -178,6 +194,15 @@ impl Component for RevisionFilesComponent {
.order(1),
);
out.push(
CommandInfo::new(
strings::commands::blame_file(&self.key_config),
self.tree.selected_file().is_some(),
true,
)
.order(order::NAV),
);
tree_nav_cmds(&self.tree, &self.key_config, out);
}
@ -193,6 +218,13 @@ impl Component for RevisionFilesComponent {
let consumed = if key == self.key_config.exit_popup {
self.hide();
true
} else if key == self.key_config.blame {
if self.blame() {
self.hide();
true
} else {
false
}
} else {
tree_nav(&mut self.tree, &self.key_config, key)
};