Merge pull request #1 from nebkor/master

Minor cleanup, prettier output
This commit is contained in:
andy boot
2018-03-21 12:12:19 -04:00
committed by GitHub
+30 -13
View File
@@ -31,8 +31,8 @@ impl Ord for Node {
} else if self.dir.size < other.dir.size { } else if self.dir.size < other.dir.size {
Ordering::Greater Ordering::Greater
} else { } else {
let my_slashes = self.dir.name.matches("/").count(); let my_slashes = self.dir.name.matches('/').count();
let other_slashes = other.dir.name.matches("/").count(); let other_slashes = other.dir.name.matches('/').count();
if my_slashes > other_slashes { if my_slashes > other_slashes {
Ordering::Greater Ordering::Greater
@@ -68,7 +68,7 @@ struct Dir {
size: u64, size: u64,
} }
static DEFAULT_NUMBER_OF_LINES: &'static str = &"15"; static DEFAULT_NUMBER_OF_LINES: &'static str = "15";
fn main() { fn main() {
let options = App::new("Trailing args example") let options = App::new("Trailing args example")
@@ -91,16 +91,16 @@ fn main() {
}; };
let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap(); let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap();
let (permissions, results) = get_dir_tree(filenames); let (permissions, results) = get_dir_tree(&filenames);
let slice_it = find_big_ones(&results, number_of_lines); let slice_it = find_big_ones(&results, number_of_lines);
display(permissions, slice_it); display(permissions, &slice_it);
} }
fn get_dir_tree(filenames: Vec<&str>) -> (bool, Vec<Node>) { fn get_dir_tree(filenames: &Vec<&str>) -> (bool, Vec<Node>) {
let mut permissions = true; let mut permissions = true;
let mut results = vec![]; let mut results = vec![];
for b in filenames { for b in filenames {
let mut new_name = String::from(b); let mut new_name = String::from(*b);
while new_name.chars().last() == Some('/') && new_name.len() != 1 { while new_name.chars().last() == Some('/') && new_name.len() != 1 {
new_name.pop(); new_name.pop();
} }
@@ -253,7 +253,7 @@ fn find_big_ones<'a>(l: &'a Vec<Node>, max_to_show: usize) -> Vec<&Node> {
} }
} }
fn display(permissions: bool, to_display: Vec<&Node>) -> () { fn display(permissions: bool, to_display: &Vec<&Node>) -> () {
if !permissions { if !permissions {
eprintln!("Did not have permissions for all directories"); eprintln!("Did not have permissions for all directories");
} }
@@ -271,14 +271,16 @@ fn display_node<S: Into<String>>(
let mut is = indentation_str.into(); let mut is = indentation_str.into();
print_this_node(node_to_print, is_first, depth, is.as_ref()); print_this_node(node_to_print, is_first, depth, is.as_ref());
is = is.replace("└─┬", " ");
is = is.replace("└──", " "); is = is.replace("└──", " ");
is = is.replace("├──", ""); is = is.replace("├──", "");
is = is.replace("├─┬", "");
let printable_node_slashes = node_to_print.dir.name.matches("/").count(); let printable_node_slashes = node_to_print.dir.name.matches('/').count();
let mut num_sibblings = 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.dir.name.matches("/").count() == printable_node_slashes + 1 && b.dir.name.matches('/').count() == printable_node_slashes + 1
{ {
a + 1 a + 1
} else { } else {
@@ -287,16 +289,30 @@ fn display_node<S: Into<String>>(
}); });
let mut is_biggest = true; let mut is_biggest = true;
let mut has_display_children = false;
for node in to_display { for node in to_display {
if node_to_print.children.contains(node) { if node_to_print.children.contains(node) {
let has_children = node.children.len() > 0;
if node.dir.name.matches("/").count() == printable_node_slashes + 1 { if node.dir.name.matches("/").count() == printable_node_slashes + 1 {
num_sibblings -= 1; num_siblings -= 1;
for ref n in node.children.iter() {
has_display_children = has_display_children || to_display.contains(n);
}
let has_children = has_children && has_display_children;
let tree_chars = { let tree_chars = {
if num_sibblings == 0 { if num_siblings == 0 {
if has_children {
"└─┬"
} else {
"└──" "└──"
}
} else {
if has_children {
"├─┬"
} else { } else {
"├──" "├──"
} }
}
}; };
display_node( display_node(
&node, &node,
@@ -346,6 +362,7 @@ fn human_readable_number(size: u64) -> (String) {
} }
mod tests { mod tests {
#[allow(unused_imports)]
use super::*; use super::*;
#[test] #[test]