Squash Node and DirEnt objects into single object

This commit is contained in:
bootandy
2018-04-03 17:05:28 +01:00
parent 6198e3183f
commit 120b4e16e7
3 changed files with 23 additions and 41 deletions
+5 -5
View File
@@ -29,11 +29,11 @@ fn display_node<S: Into<String>>(
is = is.replace("├──", ""); is = is.replace("├──", "");
is = is.replace("├─┬", ""); is = is.replace("├─┬", "");
let printable_node_slashes = node_to_print.entry().name().matches('/').count(); let printable_node_slashes = node_to_print.name().matches('/').count();
let mut num_siblings = to_display.iter().fold(0, |a, b| { let mut num_siblings = to_display.iter().fold(0, |a, b| {
if node_to_print.children().contains(b) if node_to_print.children().contains(b)
&& b.entry().name().matches('/').count() == printable_node_slashes + 1 && b.name().matches('/').count() == printable_node_slashes + 1
{ {
a + 1 a + 1
} else { } else {
@@ -49,7 +49,7 @@ fn display_node<S: Into<String>>(
.fold(false, |has_kids, n| has_kids || to_display.contains(&n)); .fold(false, |has_kids, n| has_kids || to_display.contains(&n));
let has_children = node.children().len() > 0 && has_display_children; let has_children = node.children().len() > 0 && has_display_children;
if node.entry().name().matches('/').count() == printable_node_slashes + 1 { if node.name().matches('/').count() == printable_node_slashes + 1 {
num_siblings -= 1; num_siblings -= 1;
let tree_chars = { let tree_chars = {
@@ -81,7 +81,7 @@ fn display_node<S: Into<String>>(
} }
fn print_this_node(node_to_print: &Node, is_biggest: bool, depth: u8, indentation_str: &str) { fn print_this_node(node_to_print: &Node, is_biggest: bool, depth: u8, indentation_str: &str) {
let padded_size = format!("{:>5}", human_readable_number(node_to_print.entry().size()),); let padded_size = format!("{:>5}", human_readable_number(node_to_print.size()),);
println!( println!(
"{} {} {}", "{} {} {}",
if is_biggest { if is_biggest {
@@ -92,7 +92,7 @@ fn print_this_node(node_to_print: &Node, is_biggest: bool, depth: u8, indentatio
indentation_str, indentation_str,
Fixed(7) Fixed(7)
.on(Fixed(cmp::min(8, (depth) as u8) + 231)) .on(Fixed(cmp::min(8, (depth) as u8) + 231))
.paint(node_to_print.entry().name().to_string()) .paint(node_to_print.name().to_string())
); );
} }
+12 -29
View File
@@ -2,21 +2,17 @@ use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Node { pub struct Node {
entry: DirEnt, name: String,
size: u64,
children: Vec<Node>, children: Vec<Node>,
} }
#[derive(Clone, Debug)]
pub struct DirEnt {
name: String,
size: u64,
}
impl Node { impl Node {
pub fn new(entry: DirEnt, children: Vec<Node>) -> Self { pub fn new<S: Into<String>>(name: S, size: u64, children: Vec<Node>) -> Self {
Node { Node {
entry: entry,
children: children, children: children,
name: name.into(),
size: size,
} }
} }
@@ -24,19 +20,6 @@ impl Node {
&self.children &self.children
} }
pub fn entry(&self) -> &DirEnt {
&self.entry
}
}
impl DirEnt {
pub fn new(name: &str, size: u64) -> Self {
DirEnt {
name: String::from(name),
size: size,
}
}
pub fn name(&self) -> &String { pub fn name(&self) -> &String {
&self.name &self.name
} }
@@ -48,22 +31,22 @@ impl DirEnt {
impl Ord for Node { impl Ord for Node {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
if self.entry.size > other.entry.size { if self.size > other.size {
Ordering::Less Ordering::Less
} else if self.entry.size < other.entry.size { } else if self.size < other.size {
Ordering::Greater Ordering::Greater
} else { } else {
let my_slashes = self.entry.name.matches('/').count(); let my_slashes = self.name.matches('/').count();
let other_slashes = other.entry.name.matches('/').count(); let other_slashes = other.name.matches('/').count();
if my_slashes > other_slashes { if my_slashes > other_slashes {
Ordering::Greater Ordering::Greater
} else if my_slashes < other_slashes { } else if my_slashes < other_slashes {
Ordering::Less Ordering::Less
} else { } else {
if self.entry.name < other.entry.name { if self.name < other.name {
Ordering::Less Ordering::Less
} else if self.entry.name > other.entry.name { } else if self.name > other.name {
Ordering::Greater Ordering::Greater
} else { } else {
Ordering::Equal Ordering::Equal
@@ -79,7 +62,7 @@ impl PartialOrd for Node {
} }
impl PartialEq for Node { impl PartialEq for Node {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
(&self.entry.name, self.entry.size) == (&other.entry.name, other.entry.size) (&self.name, self.size) == (&other.name, other.size)
} }
} }
impl Eq for Node {} impl Eq for Node {}
+6 -7
View File
@@ -3,7 +3,7 @@ use std::collections::HashSet;
use std::fs::{self, ReadDir}; use std::fs::{self, ReadDir};
use std::io; use std::io;
use dust::{DirEnt, Node}; use dust::Node;
mod platform; mod platform;
use self::platform::*; use self::platform::*;
@@ -28,8 +28,8 @@ fn examine_dir_str(loc: &str, apparent_size: bool) -> (bool, Node) {
let (hp, result) = examine_dir(fs::read_dir(loc), apparent_size, &mut inodes); let (hp, result) = examine_dir(fs::read_dir(loc), apparent_size, &mut inodes);
// This needs to be folded into the below recursive call somehow // This needs to be folded into the below recursive call somehow
let new_size = result.iter().fold(0, |a, b| a + b.entry().size()); let new_size = result.iter().fold(0, |a, b| a + b.size());
(hp, Node::new(DirEnt::new(loc, new_size), result)) (hp, Node::new(loc, new_size, result))
} }
fn examine_dir( fn examine_dir(
@@ -64,11 +64,10 @@ fn examine_dir(
let (hp, recursive) = let (hp, recursive) =
examine_dir(fs::read_dir(d.path()), apparent_size, inodes); examine_dir(fs::read_dir(d.path()), apparent_size, inodes);
have_permission = have_permission && hp; have_permission = have_permission && hp;
let new_size = let new_size = recursive.iter().fold(size, |a, b| a + b.size());
recursive.iter().fold(size, |a, b| a + b.entry().size()); result.push(Node::new(s, new_size, recursive))
result.push(Node::new(DirEnt::new(&s, new_size), recursive))
} else { } else {
result.push(Node::new(DirEnt::new(&s, size), vec![])) result.push(Node::new(s, size, vec![]))
} }
} }
(_, None) => have_permission = false, (_, None) => have_permission = false,