diff options
author | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-03 17:16:00 +0530 |
---|---|---|
committer | Sebastian Thiel <sthiel@thoughtworks.com> | 2019-06-03 17:16:00 +0530 |
commit | 4f4ea1e9b3813062ebe87032339bd4bcd87ee3b4 (patch) | |
tree | e988f7cc37db2965c25102251584bbf58c5d3451 /src/common.rs | |
parent | bc80db2b3f026cc10f9a06f0db624d32c1bd807f (diff) |
Add 'O' to open a folder or file using the default program
Diffstat (limited to 'src/common.rs')
-rw-r--r-- | src/common.rs | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/common.rs b/src/common.rs index dbcad2d..5961072 100644 --- a/src/common.rs +++ b/src/common.rs @@ -5,7 +5,26 @@ use crate::{ use itertools::Itertools; use jwalk::WalkDir; use petgraph::Direction; -use std::{fmt, path::Path}; +use std::{fmt, path::Path, path::PathBuf}; + +pub(crate) fn path_of(tree: &Tree, mut node_idx: TreeIndex) -> PathBuf { + const THE_ROOT: usize = 1; + let mut entries = Vec::new(); + + while let Some(parent_idx) = tree.neighbors_directed(node_idx, petgraph::Incoming).next() { + entries.push(get_entry_or_panic(tree, node_idx)); + node_idx = parent_idx; + } + entries.push(get_entry_or_panic(tree, node_idx)); + entries + .iter() + .rev() + .skip(THE_ROOT) + .fold(PathBuf::new(), |mut acc, entry| { + acc.push(&entry.name); + acc + }) +} pub(crate) fn get_entry_or_panic(tree: &Tree, node_idx: TreeIndex) -> &EntryData { tree.node_weight(node_idx) |