mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Cleanup (#121)
* remove unneeded identity .map()
* remove redunant field name in struct
* impl num_siblings for Node
* replace nested if w/ match in get_tree_chars
* remove unneeded field name in struct
* remove redundant println! & logic in display_node
* make get_children_from_node a Node method
* Revert "remove redundant println! & logic in display_node"
This reverts commit 40777025d5.
This commit is contained in:
+17
-38
@@ -34,32 +34,16 @@ pub struct DisplayData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DisplayData {
|
impl DisplayData {
|
||||||
#[allow(clippy::collapsible_if)]
|
|
||||||
fn get_tree_chars(&self, was_i_last: bool, has_children: bool) -> &'static str {
|
fn get_tree_chars(&self, was_i_last: bool, has_children: bool) -> &'static str {
|
||||||
if self.is_reversed {
|
match (self.is_reversed, was_i_last, has_children) {
|
||||||
if was_i_last {
|
(true, true, true) => "┌─┴",
|
||||||
if has_children {
|
(true, true, false) => "┌──",
|
||||||
"┌─┴"
|
(true, false, true) => "├─┴",
|
||||||
} else {
|
(true, _, _) => "├──",
|
||||||
"┌──"
|
(false, true, true) => "└─┬",
|
||||||
}
|
(false, true, false) => "└──",
|
||||||
} else if has_children {
|
(false, false, true) => "├─┬",
|
||||||
"├─┴"
|
(false, false, false) => "├──",
|
||||||
} else {
|
|
||||||
"├──"
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if was_i_last {
|
|
||||||
if has_children {
|
|
||||||
"└─┬"
|
|
||||||
} else {
|
|
||||||
"└──"
|
|
||||||
}
|
|
||||||
} else if has_children {
|
|
||||||
"├─┬"
|
|
||||||
} else {
|
|
||||||
"├──"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,15 +73,6 @@ impl DisplayData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_children_from_node(node: Node, is_reversed: bool) -> impl Iterator<Item = Node> {
|
|
||||||
if is_reversed {
|
|
||||||
let n: Vec<Node> = node.children.into_iter().rev().map(|a| a).collect();
|
|
||||||
n.into_iter()
|
|
||||||
} else {
|
|
||||||
node.children.into_iter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DrawData<'a> {
|
struct DrawData<'a> {
|
||||||
indent: String,
|
indent: String,
|
||||||
percent_bar: String,
|
percent_bar: String,
|
||||||
@@ -177,7 +152,7 @@ pub fn draw_it(
|
|||||||
|
|
||||||
let first_size_bar = repeat(BLOCKS[0]).take(max_bar_length).collect::<String>();
|
let first_size_bar = repeat(BLOCKS[0]).take(max_bar_length).collect::<String>();
|
||||||
|
|
||||||
for c in get_children_from_node(root_node, is_reversed) {
|
for c in root_node.get_children_from_node(is_reversed) {
|
||||||
let display_data = DisplayData {
|
let display_data = DisplayData {
|
||||||
short_paths: !use_full_path,
|
short_paths: !use_full_path,
|
||||||
is_reversed,
|
is_reversed,
|
||||||
@@ -234,9 +209,13 @@ fn display_node(node: Node, draw_data: &DrawData, is_biggest: bool, is_last: boo
|
|||||||
percent_bar: bar_text,
|
percent_bar: bar_text,
|
||||||
display_data: draw_data.display_data,
|
display_data: draw_data.display_data,
|
||||||
};
|
};
|
||||||
let num_siblings = node.children.len() as u64;
|
|
||||||
|
|
||||||
for (count, c) in get_children_from_node(node, draw_data.display_data.is_reversed).enumerate() {
|
let num_siblings = node.num_siblings();
|
||||||
|
|
||||||
|
for (count, c) in node
|
||||||
|
.get_children_from_node(draw_data.display_data.is_reversed)
|
||||||
|
.enumerate()
|
||||||
|
{
|
||||||
let is_biggest = dd.display_data.is_biggest(count, num_siblings);
|
let is_biggest = dd.display_data.is_biggest(count, num_siblings);
|
||||||
let was_i_last = dd.display_data.is_last(count, num_siblings);
|
let was_i_last = dd.display_data.is_last(count, num_siblings);
|
||||||
display_node(c, &dd, is_biggest, was_i_last);
|
display_node(c, &dd, is_biggest, was_i_last);
|
||||||
@@ -400,7 +379,7 @@ mod tests {
|
|||||||
by_filecount: false,
|
by_filecount: false,
|
||||||
num_chars_needed_on_left_most: 5,
|
num_chars_needed_on_left_most: 5,
|
||||||
base_size: 1,
|
base_size: 1,
|
||||||
longest_string_length: longest_string_length,
|
longest_string_length,
|
||||||
ls_colors: LsColors::from_env().unwrap_or_default(),
|
ls_colors: LsColors::from_env().unwrap_or_default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-1
@@ -17,7 +17,7 @@ use self::platform::*;
|
|||||||
|
|
||||||
type PathData = (PathBuf, u64, Option<(u64, u64)>);
|
type PathData = (PathBuf, u64, Option<(u64, u64)>);
|
||||||
|
|
||||||
#[derive(Debug, Default, Eq)]
|
#[derive(Debug, Default, Eq, Clone)]
|
||||||
pub struct Node {
|
pub struct Node {
|
||||||
pub name: PathBuf,
|
pub name: PathBuf,
|
||||||
pub size: u64,
|
pub size: u64,
|
||||||
@@ -46,6 +46,22 @@ impl PartialEq for Node {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Node {
|
||||||
|
pub fn num_siblings(&self) -> u64 {
|
||||||
|
self.children.len() as u64
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_children_from_node(&self, is_reversed: bool) -> impl Iterator<Item = Node> {
|
||||||
|
let children = self.children.clone();
|
||||||
|
if is_reversed {
|
||||||
|
let children: Vec<Node> = children.into_iter().rev().collect();
|
||||||
|
return children.into_iter();
|
||||||
|
}
|
||||||
|
|
||||||
|
children.into_iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_a_parent_of<P: AsRef<Path>>(parent: P, child: P) -> bool {
|
pub fn is_a_parent_of<P: AsRef<Path>>(parent: P, child: P) -> bool {
|
||||||
let parent = parent.as_ref();
|
let parent = parent.as_ref();
|
||||||
let child = child.as_ref();
|
let child = child.as_ref();
|
||||||
|
|||||||
Reference in New Issue
Block a user