diff --git a/README.md b/README.md index e491ac2..bb0fbbe 100644 --- a/README.md +++ b/README.md @@ -34,28 +34,29 @@ Dust assumes that’s what you wanted to do in the first place, and takes care o ``` Usage: dust Usage: dust +Usage: dust -p (full-path - does not shorten the path of the subdirectories) Usage: dust -s (apparent-size - shows the length of the file as opposed to the amount of disk space it uses) Usage: dust -n 30 (Shows 30 directories not 15) ``` ``` djin:git/dust> dust - 65M . - 65M └─┬ ./target - 49M ├─┬ ./target/debug - 26M │ ├─┬ ./target/debug/deps - 21M │ │ └── ./target/debug/deps/libclap-9e6625ac8ff074ad.rlib - 13M │ ├── ./target/debug/dust - 8.9M │ └─┬ ./target/debug/incremental - 6.7M │ ├─┬ ./target/debug/incremental/dust-2748eiei2tcnp - 6.7M │ │ └─┬ ./target/debug/incremental/dust-2748eiei2tcnp/s-ezd6jnik5u-163pyem-1aab9ncf5glum - 3.0M │ │ └── ./target/debug/incremental/dust-2748eiei2tcnp/s-ezd6jnik5u-163pyem-1aab9ncf5glum/dep-graph.bin - 2.2M │ └─┬ ./target/debug/incremental/dust-1dlon65p8m3vl - 2.2M │ └── ./target/debug/incremental/dust-1dlon65p8m3vl/s-ezd6jncecv-1xsnfd0-4dw9l1r2th2t - 15M └─┬ ./target/release - 9.2M ├─┬ ./target/release/deps - 6.7M │ └── ./target/release/deps/libclap-87bc2534ea57f044.rlib - 5.9M └── ./target/release/dust + 1.2G target + 622M ├─┬ debug + 445M │ ├── deps + 70M │ ├── incremental + 56M │ └── build + 262M ├─┬ rls + 262M │ └─┬ debug + 203M │ ├── deps + 56M │ └── build + 165M ├─┬ package + 165M │ └─┬ du-dust-0.2.4 + 165M │ └─┬ target + 165M │ └─┬ debug + 131M │ └── deps + 165M └─┬ release + 124M └── deps ``` ## Performance diff --git a/src/display.rs b/src/display.rs index 789b664..d258873 100644 --- a/src/display.rs +++ b/src/display.rs @@ -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>( 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>( 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>( &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, ) } diff --git a/src/main.rs b/src/main.rs index 6b11f73..17841c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)] diff --git a/src/tests.rs b/src/tests.rs index 7fac869..835c0dc 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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 \ No newline at end of file +// TODO: add test for bad path