diff --git a/src/display.rs b/src/display.rs index 7ac7a8a6..0d4cbd64 100644 --- a/src/display.rs +++ b/src/display.rs @@ -130,7 +130,7 @@ pub fn draw_it( let max_size = biggest.size; max_size.separate_with_commas().chars().count() } else { - 5 // Under normal usage we need 5 chars to display the size of a directory + find_biggest_size_str(&root_node, iso) }; assert!( @@ -182,6 +182,14 @@ pub fn draw_it( } } +fn find_biggest_size_str(node: &DisplayNode, iso: bool) -> usize { + let mut mx = human_readable_number(node.size, iso).chars().count(); + for n in node.children.iter() { + mx = max(mx, find_biggest_size_str(n, iso)); + } + mx +} + fn find_longest_dir_name( node: &DisplayNode, indent: usize, @@ -344,13 +352,12 @@ fn get_name_percent( fn get_pretty_size(node: &DisplayNode, is_biggest: bool, display_data: &DisplayData) -> String { let output = if display_data.by_filecount { - let size_as_str = node.size.separate_with_commas(); - let spaces_to_add = - display_data.num_chars_needed_on_left_most - size_as_str.chars().count(); - size_as_str + " ".repeat(spaces_to_add).as_str() + node.size.separate_with_commas() } else { - format!("{:>5}", human_readable_number(node.size, display_data.iso)) + human_readable_number(node.size, display_data.iso) }; + let spaces_to_add = display_data.num_chars_needed_on_left_most - output.chars().count(); + let output = " ".repeat(spaces_to_add) + output.as_str(); if is_biggest && display_data.colors_on { format!("{}", Red.paint(output)) diff --git a/tests/test_exact_output.rs b/tests/test_exact_output.rs index 63a2796c..dbba837f 100644 --- a/tests/test_exact_output.rs +++ b/tests/test_exact_output.rs @@ -82,19 +82,19 @@ fn main_output() -> Vec { // Some linux currently thought to be Manjaro, Arch // Although probably depends on how drive is formatted let mac_and_some_linux = r#" - 0B ┌── a_file │░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0% - 4.0K ├── hello_file│████████████████████████████████████████████████ │ 100% - 4.0K ┌─┴ many │████████████████████████████████████████████████ │ 100% - 4.0K ┌─┴ test_dir │████████████████████████████████████████████████ │ 100% - "# + 0B ┌── a_file │░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0% +4.0K ├── hello_file│█████████████████████████████████████████████████ │ 100% +4.0K ┌─┴ many │█████████████████████████████████████████████████ │ 100% +4.0K ┌─┴ test_dir │█████████████████████████████████████████████████ │ 100% +"# .trim() .to_string(); let ubuntu = r#" - 0B ┌── a_file │ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0% - 4.0K ├── hello_file│ ░░░░░░░░░░░░░░░░█████████████████ │ 33% - 8.0K ┌─┴ many │ █████████████████████████████████ │ 67% - 12K ┌─┴ test_dir │████████████████████████████████████████████████ │ 100% + 0B ┌── a_file │ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0% +4.0K ├── hello_file│ ░░░░░░░░░░░░░░░░█████████████████ │ 33% +8.0K ┌─┴ many │ █████████████████████████████████ │ 67% + 12K ┌─┴ test_dir │█████████████████████████████████████████████████ │ 100% "# .trim() .to_string(); @@ -111,18 +111,18 @@ pub fn test_main_long_paths() { fn main_output_long_paths() -> Vec { let mac_and_some_linux = r#" - 0B ┌── /tmp/test_dir/many/a_file │░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0% - 4.0K ├── /tmp/test_dir/many/hello_file│█████████████████████████████ │ 100% - 4.0K ┌─┴ /tmp/test_dir/many │█████████████████████████████ │ 100% - 4.0K ┌─┴ /tmp/test_dir │█████████████████████████████ │ 100% + 0B ┌── /tmp/test_dir/many/a_file │░░░░░░░░░░░░░░░░░░░░░░░░░░░░░█ │ 0% +4.0K ├── /tmp/test_dir/many/hello_file│██████████████████████████████ │ 100% +4.0K ┌─┴ /tmp/test_dir/many │██████████████████████████████ │ 100% +4.0K ┌─┴ /tmp/test_dir │██████████████████████████████ │ 100% "# .trim() .to_string(); let ubuntu = r#" - 0B ┌── /tmp/test_dir/many/a_file │ ░░░░░░░░░░░░░░░░░░░█ │ 0% - 4.0K ├── /tmp/test_dir/many/hello_file│ ░░░░░░░░░░██████████ │ 33% - 8.0K ┌─┴ /tmp/test_dir/many │ ████████████████████ │ 67% - 12K ┌─┴ /tmp/test_dir │█████████████████████████████ │ 100% + 0B ┌── /tmp/test_dir/many/a_file │ ░░░░░░░░░░░░░░░░░░░░█ │ 0% +4.0K ├── /tmp/test_dir/many/hello_file│ ░░░░░░░░░░███████████ │ 33% +8.0K ┌─┴ /tmp/test_dir/many │ █████████████████████ │ 67% + 12K ┌─┴ /tmp/test_dir │██████████████████████████████ │ 100% "# .trim() .to_string(); @@ -139,25 +139,25 @@ pub fn test_substring_of_names_and_long_names() { fn no_substring_of_names_output() -> Vec { let ubuntu = " - 0B ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goe.. - 4.0K ├── dir_name_clash - 4.0K │ ┌── hello - 8.0K ├─┴ dir - 4.0K │ ┌── hello - 8.0K ├─┴ dir_substring - 24K ┌─┴ test_dir2 + 0B ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goes.. +4.0K ├── dir_name_clash +4.0K │ ┌── hello +8.0K ├─┴ dir +4.0K │ ┌── hello +8.0K ├─┴ dir_substring + 24K ┌─┴ test_dir2 " .trim() .into(); let mac_and_some_linux = " - 0B ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goe.. - 4.0K │ ┌── hello - 4.0K ├─┴ dir - 4.0K ├── dir_name_clash - 4.0K │ ┌── hello - 4.0K ├─┴ dir_substring - 12K ┌─┴ test_dir2 + 0B ┌── long_dir_name_what_a_very_long_dir_name_what_happens_when_this_goes.. +4.0K │ ┌── hello +4.0K ├─┴ dir +4.0K ├── dir_name_clash +4.0K │ ┌── hello +4.0K ├─┴ dir_substring + 12K ┌─┴ test_dir2 " .trim() .into(); @@ -174,17 +174,17 @@ pub fn test_unicode_directories() { fn unicode_dir() -> Vec { // The way unicode & asian characters are rendered on the terminal should make this line up let ubuntu = " - 0B ┌── ラウトは難しいです!.japan│ █ │ 0% - 0B ├── 👩.unicode │ █ │ 0% - 4.0K ┌─┴ test_dir_unicode │██████████████████████████████████ │ 100% + 0B ┌── ラウトは難しいです!.japan│ █ │ 0% + 0B ├── 👩.unicode │ █ │ 0% +4.0K ┌─┴ test_dir_unicode │███████████████████████████████████ │ 100% " .trim() .into(); let mac_and_some_linux = " - 0B ┌── ラウトは難しいです!.japan│ █ │ 0% - 0B ├── 👩.unicode │ █ │ 0% - 0B ┌─┴ test_dir_unicode │ █ │ 0% +0B ┌── ラウトは難しいです!.japan│ █ │ 0% +0B ├── 👩.unicode │ █ │ 0% +0B ┌─┴ test_dir_unicode │ █ │ 0% " .trim() .into(); @@ -201,8 +201,8 @@ pub fn test_apparent_size() { fn apparent_size_output() -> Vec { // The apparent directory sizes are too unpredictable and system dependant to try and match let files = r#" - 0B ┌── a_file - 6B ├── hello_file + 0B ┌── a_file + 6B ├── hello_file "# .trim() .to_string(); diff --git a/tests/test_flags.rs b/tests/test_flags.rs index ad8c571b..b791fa2b 100644 --- a/tests/test_flags.rs +++ b/tests/test_flags.rs @@ -73,8 +73,8 @@ pub fn test_ignore_dir() { #[test] pub fn test_with_bad_param() { let mut cmd = Command::cargo_bin("dust").unwrap(); - let stderr = cmd.arg("-").unwrap().stderr; - let stderr = str::from_utf8(&stderr).unwrap(); + let result = cmd.arg("bad_place").unwrap(); + let stderr = str::from_utf8(&result.stderr).unwrap(); assert!(stderr.contains("Did not have permissions for all directories")); } diff --git a/tests/tests_symlinks.rs b/tests/tests_symlinks.rs index 4a26d66b..92aca179 100644 --- a/tests/tests_symlinks.rs +++ b/tests/tests_symlinks.rs @@ -1,41 +1,15 @@ use assert_cmd::Command; -use std::cmp::max; use std::fs::File; use std::io::Write; use std::path::PathBuf; use std::str; -use terminal_size::{terminal_size, Height, Width}; -use unicode_width::UnicodeWidthStr; - use tempfile::Builder; use tempfile::TempDir; // File sizes differ on both platform and on the format of the disk. // Windows: `ln` is not usually an available command; creation of symbolic links requires special enhanced permissions -fn get_width_of_terminal() -> u16 { - if let Some((Width(w), Height(_h))) = terminal_size() { - max(w, 80) - } else { - 80 - } -} - -// Mac test runners create tmp files with very long names, hence it may be shortened in the output -fn get_file_name(name: String) -> String { - let terminal_plus_buffer = (get_width_of_terminal() - 12) as usize; - if UnicodeWidthStr::width(&*name) > terminal_plus_buffer { - let trimmed_name = name - .chars() - .take(terminal_plus_buffer - 2) - .collect::(); - trimmed_name + ".." - } else { - name - } -} - fn build_temp_file(dir: &TempDir) -> PathBuf { let file_path = dir.path().join("notes.txt"); let mut file = File::create(&file_path).unwrap(); @@ -60,12 +34,16 @@ pub fn test_soft_sym_link() { .output(); assert!(c.is_ok()); - let c = format!(" ├── {}", get_file_name(link_name_s.into())); - let b = format!(" ┌── {}", get_file_name(file_path_s.into())); + let c = format!(" ├── {}", link_name_s); + let b = format!(" ┌── {}", file_path_s); let a = format!("─┴ {}", dir_s); let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd.arg("-p").arg("-c").arg("-s").arg(dir_s).unwrap().stdout; + // Mac test runners create long filenames in tmp directories + let output = cmd + .args(["-p", "-c", "-s", "-w 999", dir_s]) + .unwrap() + .stdout; let output = str::from_utf8(&output).unwrap(); @@ -90,11 +68,12 @@ pub fn test_hard_sym_link() { .output(); assert!(c.is_ok()); - let file_output = format!(" ┌── {}", get_file_name(file_path_s.into())); + let file_output = format!(" ┌── {}", file_path_s); let dirs_output = format!("─┴ {}", dir_s); let mut cmd = Command::cargo_bin("dust").unwrap(); - let output = cmd.arg("-p").arg("-c").arg(dir_s).unwrap().stdout; + // Mac test runners create long filenames in tmp directories + let output = cmd.args(["-p", "-c", "-w 999", dir_s]).unwrap().stdout; // The link should not appear in the output because multiple inodes are now ordered // then filtered. @@ -120,7 +99,7 @@ pub fn test_recursive_sym_link() { assert!(c.is_ok()); let a = format!("─┬ {}", dir_s); - let b = format!(" └── {}", get_file_name(link_name_s.into())); + let b = format!(" └── {}", link_name_s); let mut cmd = Command::cargo_bin("dust").unwrap(); let output = cmd @@ -128,6 +107,7 @@ pub fn test_recursive_sym_link() { .arg("-c") .arg("-r") .arg("-s") + .arg("-w 999") .arg(dir_s) .unwrap() .stdout;