mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
By default only print last leaf of path
Fixes #9 https://github.com/bootandy/dust/issues/9 Instead of printing the all sub tree leaves only the last leaf is now printed. See readme change for example The flag '-p' was added to print the sub tree the old way
This commit is contained in:
+40
-7
@@ -4,13 +4,18 @@ use self::ansi_term::Colour::Fixed;
|
||||
|
||||
static UNITS: [char; 4] = ['T', 'G', 'M', 'K'];
|
||||
|
||||
pub fn draw_it(permissions: bool, base_dirs: Vec<&str>, to_display: Vec<(String, u64)>) -> () {
|
||||
pub fn draw_it(
|
||||
permissions: bool,
|
||||
short_paths: bool,
|
||||
base_dirs: Vec<&str>,
|
||||
to_display: Vec<(String, u64)>,
|
||||
) -> () {
|
||||
if !permissions {
|
||||
eprintln!("Did not have permissions for all directories");
|
||||
}
|
||||
|
||||
for f in base_dirs {
|
||||
display_node(f, &to_display, true, "")
|
||||
display_node(f, &to_display, true, short_paths, "")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +32,7 @@ fn display_node<S: Into<String>>(
|
||||
node_to_print: &str,
|
||||
to_display: &Vec<(String, u64)>,
|
||||
is_biggest: bool,
|
||||
short_paths: bool,
|
||||
indentation_str: S,
|
||||
) {
|
||||
let mut is = indentation_str.into();
|
||||
@@ -34,7 +40,7 @@ fn display_node<S: Into<String>>(
|
||||
match size {
|
||||
None => println!("Can not find path: {}", node_to_print),
|
||||
Some(size) => {
|
||||
print_this_node(node_to_print, size, is_biggest, is.as_ref());
|
||||
print_this_node(node_to_print, size, is_biggest, short_paths, is.as_ref());
|
||||
|
||||
is = is.replace("└─┬", " ");
|
||||
is = is.replace("└──", " ");
|
||||
@@ -74,6 +80,7 @@ fn display_node<S: Into<String>>(
|
||||
&k,
|
||||
to_display,
|
||||
is_biggest,
|
||||
short_paths,
|
||||
is.to_string() + get_tree_chars(num_siblings, has_children),
|
||||
);
|
||||
is_biggest = false;
|
||||
@@ -98,15 +105,41 @@ fn get_tree_chars(num_siblings: u64, has_children: bool) -> &'static str {
|
||||
}
|
||||
}
|
||||
}
|
||||
fn print_this_node(node_name: &str, size: u64, is_biggest: bool, indentation: &str) {
|
||||
|
||||
fn print_this_node(
|
||||
node_name: &str,
|
||||
size: u64,
|
||||
is_biggest: bool,
|
||||
short_paths: bool,
|
||||
indentation: &str,
|
||||
) {
|
||||
let pretty_size = format!("{:>5}", human_readable_number(size),);
|
||||
println!(
|
||||
"{}",
|
||||
format_string(node_name, is_biggest, pretty_size.as_ref(), indentation)
|
||||
format_string(
|
||||
node_name,
|
||||
is_biggest,
|
||||
short_paths,
|
||||
pretty_size.as_ref(),
|
||||
indentation
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
pub fn format_string(dir_name: &str, is_biggest: bool, size: &str, indentation: &str) -> String {
|
||||
pub fn format_string(
|
||||
dir_name: &str,
|
||||
is_biggest: bool,
|
||||
short_paths: bool,
|
||||
size: &str,
|
||||
indentation: &str,
|
||||
) -> String {
|
||||
let printable_name = {
|
||||
if short_paths && dir_name.contains('/') {
|
||||
dir_name.split('/').last().unwrap()
|
||||
} else {
|
||||
dir_name
|
||||
}
|
||||
};
|
||||
format!(
|
||||
"{} {} {}",
|
||||
if is_biggest {
|
||||
@@ -115,7 +148,7 @@ pub fn format_string(dir_name: &str, is_biggest: bool, size: &str, indentation:
|
||||
Fixed(7).paint(size)
|
||||
},
|
||||
indentation,
|
||||
dir_name,
|
||||
printable_name,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+9
-3
@@ -23,7 +23,12 @@ fn main() {
|
||||
.default_value(DEFAULT_NUMBER_OF_LINES),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("use_apparent_size")
|
||||
Arg::with_name("display_full_paths")
|
||||
.short("p")
|
||||
.help("If set sub directories will not have their path shortened"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("display_apparent_size")
|
||||
.short("s")
|
||||
.help("If set will use file length. Otherwise we use blocks"),
|
||||
)
|
||||
@@ -37,11 +42,12 @@ fn main() {
|
||||
}
|
||||
};
|
||||
let number_of_lines = value_t!(options.value_of("number_of_lines"), usize).unwrap();
|
||||
let use_apparent_size = options.is_present("use_apparent_size");
|
||||
let use_apparent_size = options.is_present("display_apparent_size");
|
||||
let use_full_path = options.is_present("display_full_paths");
|
||||
|
||||
let (permissions, nodes) = get_dir_tree(&filenames, use_apparent_size);
|
||||
let biggest_ones = find_big_ones(nodes, number_of_lines);
|
||||
draw_it(permissions, filenames, biggest_ones);
|
||||
draw_it(permissions, !use_full_path, filenames, biggest_ones);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+38
-31
@@ -15,35 +15,44 @@ pub fn test_main() {
|
||||
assert_cli::Assert::main_binary()
|
||||
.with_args(&["src/test_dir"])
|
||||
.stdout()
|
||||
.is(main_output())
|
||||
.is(main_output(true))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_main_long_paths() {
|
||||
assert_cli::Assert::main_binary()
|
||||
.with_args(&["-p", "src/test_dir"])
|
||||
.stdout()
|
||||
.is(main_output(false))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
fn main_output() -> String {
|
||||
fn main_output(short_paths: bool) -> String {
|
||||
format!(
|
||||
"{}
|
||||
{}
|
||||
{}
|
||||
{}",
|
||||
format_string("src/test_dir", true, " 4.0K", ""),
|
||||
format_string("src/test_dir/many", true, " 4.0K", "└─┬",),
|
||||
format_string("src/test_dir/many/hello_file", true, " 4.0K", " ├──",),
|
||||
format_string("src/test_dir/many/a_file", false, " 0B", " └──",),
|
||||
format_string("src/test_dir", true, short_paths, " 4.0K", ""),
|
||||
format_string("src/test_dir/many", true, short_paths, " 4.0K", "└─┬",),
|
||||
format_string("src/test_dir/many/hello_file", true, short_paths, " 4.0K", " ├──",),
|
||||
format_string("src/test_dir/many/a_file", false, short_paths, " 0B", " └──",),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn main_output() -> String {
|
||||
fn main_output(short_paths: bool) -> String {
|
||||
format!(
|
||||
"{}
|
||||
{}
|
||||
{}
|
||||
{}",
|
||||
format_string("src/test_dir", true, " 12K", ""),
|
||||
format_string("src/test_dir/many", true, " 8.0K", "└─┬",),
|
||||
format_string("src/test_dir/many/hello_file", true, " 4.0K", " ├──",),
|
||||
format_string("src/test_dir/many/a_file", false, " 0B", " └──",),
|
||||
format_string("src/test_dir", true, short_paths, " 12K", ""),
|
||||
format_string("src/test_dir/many", true, short_paths, " 8.0K", "└─┬",),
|
||||
format_string("src/test_dir/many/hello_file", true, short_paths, " 4.0K", " ├──",),
|
||||
format_string("src/test_dir/many/a_file", false, short_paths, " 0B", " └──",),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -51,7 +60,7 @@ fn main_output() -> String {
|
||||
pub fn test_apparent_size() {
|
||||
let r = format!(
|
||||
"{}",
|
||||
format_string("src/test_dir/many/hello_file", true, " 6B", " ├──",),
|
||||
format_string("src/test_dir/many/hello_file", true, true, " 6B", " ├──",),
|
||||
);
|
||||
|
||||
assert_cli::Assert::main_binary()
|
||||
@@ -97,9 +106,9 @@ fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
|
||||
"{}
|
||||
{}
|
||||
{}",
|
||||
format_string(dir, true, " 8.0K", ""),
|
||||
format_string(file_path, true, " 4.0K", "├──",),
|
||||
format_string(link_name, false, " 4.0K", "└──",),
|
||||
format_string(dir, true, true, " 8.0K", ""),
|
||||
format_string(file_path, true, true, " 4.0K", "├──",),
|
||||
format_string(link_name, false, true, " 4.0K", "└──",),
|
||||
)
|
||||
}
|
||||
#[cfg(target_os = "linux")]
|
||||
@@ -108,9 +117,9 @@ fn soft_sym_link_output(dir: &str, file_path: &str, link_name: &str) -> String {
|
||||
"{}
|
||||
{}
|
||||
{}",
|
||||
format_string(dir, true, " 8.0K", ""),
|
||||
format_string(file_path, true, " 4.0K", "├──",),
|
||||
format_string(link_name, false, " 0B", "└──",),
|
||||
format_string(dir, true, true, " 8.0K", ""),
|
||||
format_string(file_path, true, true, " 4.0K", "├──",),
|
||||
format_string(link_name, false, true, " 0B", "└──",),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -156,14 +165,14 @@ fn hard_link_output(dir_s: &str, file_path_s: &str, link_name_s: &str) -> (Strin
|
||||
let r = format!(
|
||||
"{}
|
||||
{}",
|
||||
format_string(dir_s, true, " 4.0K", ""),
|
||||
format_string(file_path_s, true, " 4.0K", "└──")
|
||||
format_string(dir_s, true, true, " 4.0K", ""),
|
||||
format_string(file_path_s, true, true, " 4.0K", "└──")
|
||||
);
|
||||
let r2 = format!(
|
||||
"{}
|
||||
{}",
|
||||
format_string(dir_s, true, " 4.0K", ""),
|
||||
format_string(link_name_s, true, " 4.0K", "└──")
|
||||
format_string(dir_s, true, true, " 4.0K", ""),
|
||||
format_string(link_name_s, true, true, " 4.0K", "└──")
|
||||
);
|
||||
(r, r2)
|
||||
}
|
||||
@@ -173,9 +182,8 @@ fn hard_link_output(dir_s: &str, file_path_s: &str, link_name_s: &str) -> (Strin
|
||||
let r = format!(
|
||||
"{}
|
||||
{}",
|
||||
|
||||
format_string(dir_s, true, " 8.0K", ""),
|
||||
format_string(file_path_s, true, " 4.0K", "└──")
|
||||
format_string(dir_s, true, true, " 8.0K", ""),
|
||||
format_string(file_path_s, true, true, " 4.0K", "└──")
|
||||
);
|
||||
let r2 = format!(
|
||||
"{}
|
||||
@@ -214,8 +222,8 @@ fn recursive_sym_link_output(dir: &str, link_name: &str) -> String {
|
||||
format!(
|
||||
"{}
|
||||
{}",
|
||||
format_string(dir, true, " 4.0K", ""),
|
||||
format_string(link_name, true, " 4.0K", "└──",),
|
||||
format_string(dir, true, true, " 4.0K", ""),
|
||||
format_string(link_name, true, true, " 4.0K", "└──",),
|
||||
)
|
||||
}
|
||||
#[cfg(target_os = "linux")]
|
||||
@@ -223,10 +231,9 @@ fn recursive_sym_link_output(dir: &str, link_name: &str) -> String {
|
||||
format!(
|
||||
"{}
|
||||
{}",
|
||||
format_string(dir, true, " 4.0K", ""),
|
||||
format_string(link_name, true, " 0B", "└──",),
|
||||
format_string(dir, true, true, " 4.0K", ""),
|
||||
format_string(link_name, true, true, " 0B", "└──",),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// TODO: add test for bad path
|
||||
// TODO: add test for bad path
|
||||
|
||||
Reference in New Issue
Block a user