summaryrefslogtreecommitdiffstats
path: root/src/tree.rs
diff options
context:
space:
mode:
authorqkzk <qu3nt1n@gmail.com>2023-01-31 20:48:09 +0100
committerqkzk <qu3nt1n@gmail.com>2023-01-31 20:48:09 +0100
commitc3532f7c1629f53ad18c539cfe86cc015959981c (patch)
tree7a76ff51726e8eb2182acbc381680882a4590122 /src/tree.rs
parent8a79377013e60930eb0af68a16e96c19a6abec1c (diff)
some memory improvment
Diffstat (limited to 'src/tree.rs')
-rw-r--r--src/tree.rs55
1 files changed, 27 insertions, 28 deletions
diff --git a/src/tree.rs b/src/tree.rs
index 2461fb5..a10ad51 100644
--- a/src/tree.rs
+++ b/src/tree.rs
@@ -27,21 +27,16 @@ impl ColoredString {
}
fn from_node(current_node: &Node, colors: &Colors) -> Self {
- let mut text = Self::fold_symbols(current_node);
- text.push_str(&current_node.filename());
- Self::new(text, current_node.attr(colors), current_node.filepath())
- }
-
- fn fold_symbols(current_node: &Node) -> String {
- if current_node.is_dir {
- match current_node.folded {
- true => "▸ ",
- false => "▾ ",
+ let text = if current_node.is_dir {
+ if current_node.folded {
+ format!("▸ {}", &current_node.fileinfo.filename)
+ } else {
+ format!("▾ {}", &current_node.fileinfo.filename)
}
} else {
- ""
- }
- .to_owned()
+ current_node.filename()
+ };
+ Self::new(text, current_node.attr(colors), current_node.filepath())
}
}
@@ -53,7 +48,7 @@ pub struct Node {
pub fileinfo: FileInfo,
pub position: Vec<usize>,
pub folded: bool,
- is_dir: bool,
+ pub is_dir: bool,
index: Option<usize>,
}
@@ -198,24 +193,28 @@ impl Tree {
if max_depth == 0 {
return Ok(vec![]);
}
- let mut leaves = vec![];
let FileKind::Directory = fileinfo.file_kind else { return Ok(vec![]) };
let Some(mut files) =
files_collection(fileinfo, users_cache, display_hidden, filter_kind)
- else { return Ok(leaves) };
+ else { return Ok(vec![]) };
sort_kind.sort(&mut files);
- for (index, fileinfo) in files.iter().enumerate() {
- let mut position = parent_position.clone();
- position.push(files.len() - index - 1);
- leaves.push(Self::create_tree_from_fileinfo(
- fileinfo.to_owned(),
- max_depth - 1,
- users_cache,
- filter_kind,
- display_hidden,
- position,
- )?)
- }
+ let leaves = files
+ .iter()
+ .enumerate()
+ .map(|(index, fileinfo)| {
+ let mut position = parent_position.clone();
+ position.push(files.len() - index - 1);
+ Self::create_tree_from_fileinfo(
+ fileinfo.to_owned(),
+ max_depth - 1,
+ users_cache,
+ filter_kind,
+ display_hidden,
+ position,
+ )
+ .unwrap()
+ })
+ .collect();
Ok(leaves)
}