Add support for -d depth flag

Following a user request the option '-d N' allows a user to output N
levels of sub directories.

Fixed bug: so that trailing slashes are now removed.
This commit is contained in:
andy.boot
2018-04-23 15:39:20 +01:00
parent 0bded9698a
commit e6c777fb8b
3 changed files with 96 additions and 31 deletions
+29 -22
View File
@@ -7,7 +7,8 @@ static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
pub fn draw_it(
permissions: bool,
short_paths: bool,
base_dirs: Vec<&str>,
depth: Option<u64>,
base_dirs: Vec<String>,
to_display: Vec<(String, u64)>,
) -> () {
if !permissions {
@@ -15,13 +16,13 @@ pub fn draw_it(
}
for f in base_dirs {
display_node(f, &to_display, true, short_paths, "")
display_node(f, &to_display, true, short_paths, depth, "")
}
}
fn get_size(nodes: &Vec<(String, u64)>, node_to_print: String) -> Option<u64> {
fn get_size(nodes: &Vec<(String, u64)>, node_to_print: &String) -> Option<u64> {
for &(ref k, ref v) in nodes.iter() {
if *k == node_to_print {
if *k == *node_to_print {
return Some(*v);
}
}
@@ -29,18 +30,25 @@ fn get_size(nodes: &Vec<(String, u64)>, node_to_print: String) -> Option<u64> {
}
fn display_node<S: Into<String>>(
node_to_print: &str,
node_to_print: String,
to_display: &Vec<(String, u64)>,
is_biggest: bool,
short_paths: bool,
depth: Option<u64>,
indentation_str: S,
) {
let mut is = indentation_str.into();
let size = get_size(to_display, node_to_print.to_string());
match size {
let new_depth = match depth {
None => None,
Some(0) => return,
Some(d) => Some(d - 1),
};
match get_size(to_display, &node_to_print) {
None => println!("Can not find path: {}", node_to_print),
Some(size) => {
print_this_node(node_to_print, size, is_biggest, short_paths, is.as_ref());
let mut is = indentation_str.into();
let ntp: &str = node_to_print.as_ref();
print_this_node(ntp, size, is_biggest, short_paths, is.as_ref());
is = is.replace("└─┬", " ");
is = is.replace("└──", " ");
@@ -50,9 +58,7 @@ fn display_node<S: Into<String>>(
let printable_node_slashes = node_to_print.matches('/').count();
let mut num_siblings = to_display.iter().fold(0, |a, b| {
if b.0.starts_with(node_to_print)
&& b.0.matches('/').count() == printable_node_slashes + 1
{
if b.0.starts_with(ntp) && b.0.matches('/').count() == printable_node_slashes + 1 {
a + 1
} else {
a
@@ -61,26 +67,27 @@ fn display_node<S: Into<String>>(
let mut is_biggest = true;
for &(ref k, _) in to_display.iter() {
if k.starts_with(node_to_print)
&& k.matches('/').count() == printable_node_slashes + 1
{
if k.starts_with(ntp) && k.matches('/').count() == printable_node_slashes + 1 {
num_siblings -= 1;
let mut has_children = false;
for &(ref k2, _) in to_display.iter() {
let kk :&str = k.as_ref();
if k2.starts_with(kk)
&& k2.matches('/').count() == printable_node_slashes + 2
{
has_children = true;
if new_depth.is_none() || new_depth.unwrap() != 1 {
for &(ref k2, _) in to_display.iter() {
let kk: &str = k.as_ref();
if k2.starts_with(kk)
&& k2.matches('/').count() == printable_node_slashes + 2
{
has_children = true;
}
}
}
display_node(
&k,
k.to_string(),
to_display,
is_biggest,
short_paths,
new_depth,
is.to_string() + get_tree_chars(num_siblings, has_children),
);
is_biggest = false;