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:
andy.boot
2018-04-23 13:59:29 +01:00
parent 24c97ef92f
commit 803934d84b
4 changed files with 104 additions and 57 deletions
+17 -16
View File
@@ -34,28 +34,29 @@ Dust assumes thats what you wanted to do in the first place, and takes care o
``` ```
Usage: dust <dir> Usage: dust <dir>
Usage: dust <dir> <another_dir> <and_more> Usage: dust <dir> <another_dir> <and_more>
Usage: dust -p <dir> (full-path - does not shorten the path of the subdirectories)
Usage: dust -s <dir> (apparent-size - shows the length of the file as opposed to the amount of disk space it uses) Usage: dust -s <dir> (apparent-size - shows the length of the file as opposed to the amount of disk space it uses)
Usage: dust -n 30 <dir> (Shows 30 directories not 15) Usage: dust -n 30 <dir> (Shows 30 directories not 15)
``` ```
``` ```
djin:git/dust> dust djin:git/dust> dust
65M . 1.2G target
65M ─┬ ./target 622M ─┬ debug
49M ├─┬ ./target/debug 445M ├── deps
26M │ ├─┬ ./target/debug/deps 70M │ ├── incremental
21M │ │ └── ./target/debug/deps/libclap-9e6625ac8ff074ad.rlib 56M │ └── build
13M │ ├── ./target/debug/dust 262M ├─┬ rls
8.9M │ └─┬ ./target/debug/incremental 262M │ └─┬ debug
6.7M │ ├─┬ ./target/debug/incremental/dust-2748eiei2tcnp 203M │ ├── deps
6.7M │ │ └─┬ ./target/debug/incremental/dust-2748eiei2tcnp/s-ezd6jnik5u-163pyem-1aab9ncf5glum 56M │ └── build
3.0M │ │ └── ./target/debug/incremental/dust-2748eiei2tcnp/s-ezd6jnik5u-163pyem-1aab9ncf5glum/dep-graph.bin 165M ├─┬ package
2.2M │ └─┬ ./target/debug/incremental/dust-1dlon65p8m3vl 165M │ └─┬ du-dust-0.2.4
2.2M │ └── ./target/debug/incremental/dust-1dlon65p8m3vl/s-ezd6jncecv-1xsnfd0-4dw9l1r2th2t 165M │ └─target
15M └─┬ ./target/release 165M └─┬ debug
9.2M ├─┬ ./target/release/deps 131M └── deps
6.7M │ └── ./target/release/deps/libclap-87bc2534ea57f044.rlib 165M └─┬ release
5.9M └── ./target/release/dust 124M └── deps
``` ```
## Performance ## Performance
+40 -7
View File
@@ -4,13 +4,18 @@ use self::ansi_term::Colour::Fixed;
static UNITS: [char; 4] = ['T', 'G', 'M', 'K']; 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 { if !permissions {
eprintln!("Did not have permissions for all directories"); eprintln!("Did not have permissions for all directories");
} }
for f in base_dirs { 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, node_to_print: &str,
to_display: &Vec<(String, u64)>, to_display: &Vec<(String, u64)>,
is_biggest: bool, is_biggest: bool,
short_paths: bool,
indentation_str: S, indentation_str: S,
) { ) {
let mut is = indentation_str.into(); let mut is = indentation_str.into();
@@ -34,7 +40,7 @@ fn display_node<S: Into<String>>(
match size { match size {
None => println!("Can not find path: {}", node_to_print), None => println!("Can not find path: {}", node_to_print),
Some(size) => { 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("└─┬", " ");
is = is.replace("└──", " "); is = is.replace("└──", " ");
@@ -74,6 +80,7 @@ fn display_node<S: Into<String>>(
&k, &k,
to_display, to_display,
is_biggest, is_biggest,
short_paths,
is.to_string() + get_tree_chars(num_siblings, has_children), is.to_string() + get_tree_chars(num_siblings, has_children),
); );
is_biggest = false; 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),); let pretty_size = format!("{:>5}", human_readable_number(size),);
println!( 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!( format!(
"{} {} {}", "{} {} {}",
if is_biggest { if is_biggest {
@@ -115,7 +148,7 @@ pub fn format_string(dir_name: &str, is_biggest: bool, size: &str, indentation:
Fixed(7).paint(size) Fixed(7).paint(size)
}, },
indentation, indentation,
dir_name, printable_name,
) )
} }
+9 -3
View File
@@ -23,7 +23,12 @@ fn main() {
.default_value(DEFAULT_NUMBER_OF_LINES), .default_value(DEFAULT_NUMBER_OF_LINES),
) )
.arg( .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") .short("s")
.help("If set will use file length. Otherwise we use blocks"), .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 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 (permissions, nodes) = get_dir_tree(&filenames, use_apparent_size);
let biggest_ones = find_big_ones(nodes, number_of_lines); 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)] #[cfg(test)]
+37 -30
View File
@@ -15,35 +15,44 @@ pub fn test_main() {
assert_cli::Assert::main_binary() assert_cli::Assert::main_binary()
.with_args(&["src/test_dir"]) .with_args(&["src/test_dir"])
.stdout() .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(); .unwrap();
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
fn main_output() -> String { fn main_output(short_paths: bool) -> String {
format!( format!(
"{} "{}
{} {}
{} {}
{}", {}",
format_string("src/test_dir", true, " 4.0K", ""), format_string("src/test_dir", true, short_paths, " 4.0K", ""),
format_string("src/test_dir/many", true, " 4.0K", "└─┬",), format_string("src/test_dir/many", true, short_paths, " 4.0K", "└─┬",),
format_string("src/test_dir/many/hello_file", true, " 4.0K", " ├──",), format_string("src/test_dir/many/hello_file", true, short_paths, " 4.0K", " ├──",),
format_string("src/test_dir/many/a_file", false, " 0B", " └──",), format_string("src/test_dir/many/a_file", false, short_paths, " 0B", " └──",),
) )
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn main_output() -> String { fn main_output(short_paths: bool) -> String {
format!( format!(
"{} "{}
{} {}
{} {}
{}", {}",
format_string("src/test_dir", true, " 12K", ""), format_string("src/test_dir", true, short_paths, " 12K", ""),
format_string("src/test_dir/many", true, " 8.0K", "└─┬",), format_string("src/test_dir/many", true, short_paths, " 8.0K", "└─┬",),
format_string("src/test_dir/many/hello_file", true, " 4.0K", " ├──",), format_string("src/test_dir/many/hello_file", true, short_paths, " 4.0K", " ├──",),
format_string("src/test_dir/many/a_file", false, " 0B", " └──",), 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() { pub fn test_apparent_size() {
let r = format!( 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() 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(dir, true, true, " 8.0K", ""),
format_string(file_path, true, " 4.0K", "├──",), format_string(file_path, true, true, " 4.0K", "├──",),
format_string(link_name, false, " 4.0K", "└──",), format_string(link_name, false, true, " 4.0K", "└──",),
) )
} }
#[cfg(target_os = "linux")] #[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(dir, true, true, " 8.0K", ""),
format_string(file_path, true, " 4.0K", "├──",), format_string(file_path, true, true, " 4.0K", "├──",),
format_string(link_name, false, " 0B", "└──",), 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!( let r = format!(
"{} "{}
{}", {}",
format_string(dir_s, true, " 4.0K", ""), format_string(dir_s, true, true, " 4.0K", ""),
format_string(file_path_s, true, " 4.0K", "└──") format_string(file_path_s, true, true, " 4.0K", "└──")
); );
let r2 = format!( let r2 = format!(
"{} "{}
{}", {}",
format_string(dir_s, true, " 4.0K", ""), format_string(dir_s, true, true, " 4.0K", ""),
format_string(link_name_s, true, " 4.0K", "└──") format_string(link_name_s, true, true, " 4.0K", "└──")
); );
(r, r2) (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!( let r = format!(
"{} "{}
{}", {}",
format_string(dir_s, true, true, " 8.0K", ""),
format_string(dir_s, true, " 8.0K", ""), format_string(file_path_s, true, true, " 4.0K", "└──")
format_string(file_path_s, true, " 4.0K", "└──")
); );
let r2 = format!( let r2 = format!(
"{} "{}
@@ -214,8 +222,8 @@ fn recursive_sym_link_output(dir: &str, link_name: &str) -> String {
format!( format!(
"{} "{}
{}", {}",
format_string(dir, true, " 4.0K", ""), format_string(dir, true, true, " 4.0K", ""),
format_string(link_name, true, " 4.0K", "└──",), format_string(link_name, true, true, " 4.0K", "└──",),
) )
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
@@ -223,10 +231,9 @@ fn recursive_sym_link_output(dir: &str, link_name: &str) -> String {
format!( format!(
"{} "{}
{}", {}",
format_string(dir, true, " 4.0K", ""), format_string(dir, true, true, " 4.0K", ""),
format_string(link_name, true, " 0B", "└──",), format_string(link_name, true, true, " 0B", "└──",),
) )
} }
// TODO: add test for bad path // TODO: add test for bad path